ReactJS Components: A Comprehensive Guide

ReactJS components are the building blocks of React applications, allowing developers to create reusable and modular UI elements. In this comprehensive guide, we'll dive deep into React components, covering everything from the basics to advanced concepts.

Understanding React Components

link to this section

What are React Components?

React components are small, self-contained pieces of code that represent a part of the user interface. They encapsulate both the structure (HTML) and behavior (JavaScript) of a UI element, making it easier to manage and maintain code. Components can be simple, like a button or a text input, or complex, like an entire page or application.

Types of React Components

1. Functional Components:

Functional components, also known as stateless components, are defined as JavaScript functions that return JSX (JavaScript XML). They are primarily used for presentational components that don't manage state or use lifecycle methods.

Key Points:

  • Defined as JavaScript functions.
  • Take props as input and return JSX elements as output.
  • Cannot manage state or access lifecycle methods.

Example:

const Greeting = (props) => { 
    return <div>Hello, {props.name}!</div>; 
}; 

2. Class Components:

Class components, also known as stateful components, are defined as ES6 classes that extend the React.Component class. They can manage state, have access to lifecycle methods, and are suitable for complex UI logic and state management.

Key Points:

  • Defined as ES6 classes.
  • Extend the React.Component class.
  • Can manage state and access lifecycle methods.

Example:

class Counter extends React.Component { 
    constructor(props) { 
        super(props); 
        this.state = { 
            count: 0, 
        }; 
    } 
    
    render() { 
        return ( 
            <div> 
                <p>Count: {this.state.count}</p> 
                <button onClick={() => this.setState({ count: this.state.count + 1 })}> Increment </button> 
            </div> 
        ); 
    } 
} 

How to Run Component in ReactJS

link to this section
  1. Create a React Component: Create a new file for your custom component in the src directory of your React app. For example, let's create a simple functional component called HelloWorld in a file named HelloWorld.js :

    // src/HelloWorld.js 
    import React from 'react'; 
    
    const HelloWorld = () => { 
        return <div>Hello, World!</div>; 
    }; 
    
    export default HelloWorld; 
  2. Import and Use the Component: Import the custom component into the file where you want to use it. For example, let's import and use the HelloWorld component in the App.js file:

    // src/App.js 
    import React from 'react'; 
    import HelloWorld from './HelloWorld'; 
    
    // Import the custom component 
    function App() { 
        return ( 
            <div className="App"> 
                <HelloWorld /> {/* Use the custom component */} 
            </div> 
        ); 
    } 
    
    export default App; 
  3. Run the Development Server: Start the development server by running the following command in your terminal:

    npm start 

    This will start the development server and automatically open your default web browser to display your React app.

  4. View Your React App: Open your web browser and navigate to http://localhost:3000/ to view your React app. You should see "Hello, World!" rendered on the page.

Conclusion

link to this section

React components are the heart of React applications, allowing developers to build complex UIs by composing small, reusable pieces of code. By understanding the basics of React components, you'll be well-equipped to create powerful and maintainable React applications.

In this guide, we covered the different types of React components, props, state, and how to create functional and class components. With this knowledge, you're ready to start building your own React applications with confidence!