Mapagam
  • JavaScript 
  • Web APIs 
  • TypeScript 
  • React 

React Components Explained: The Ultimate Beginner’s Guide

Posted on March 30, 2025 • 6 min read • 1,267 words
Share via
Mapagam
Link copied to clipboard

Learn about React components in this beginner's guide. Explore functional vs. class components, props, state, JSX, and more for efficient React development.

On this page
1. What Are React Components? 1.1. Definition of React Components 1.2. Why Are Components Important? 2. Types of React Components 2.1. Functional Components 2.2. Class Components 3. JSX: The Syntax Used in React Components 3.1. What is JSX? 3.2. Why JSX? 4. Props: Passing Data Between Components 4.1. What are Props? 4.2. Using Props in React Components 5. State: Managing Component Data 5.1. What is State? 5.2. Using State in Functional Components 5.3. Using State in Class Components 6. Component Lifecycle (For Class Components) 6.1. What is Component Lifecycle? 6.2. React Hooks and Lifecycle 7. Advanced Component Patterns 7.1. Higher-Order Components (HOCs) 7.2. Render Props 8. Conclusion

React is one of the most popular JavaScript libraries for building user interfaces. Its component-based architecture allows developers to create reusable, maintainable, and efficient UI elements. Understanding components is the first step to mastering the library.

1. What Are React Components?

1.1. Definition of React Components

In React, components are the building blocks of the user interface. Each component is a JavaScript function or class that returns a part of the UI, which React can render to the DOM. Think of a component as a small, reusable unit of the UI that you can combine to create complex interfaces.

React allows developers to break down complex UIs into smaller, isolated components that manage their own state and behavior.

1.2. Why Are Components Important?

The primary advantage of React components lies in their reusability and modularity. By breaking down an application into smaller components, you can:

  • Reuse components across different parts of your application
  • Make the code more maintainable and scalable
  • Improve collaboration between developers working on different components
  • Make debugging and testing easier

2. Types of React Components

React components come in two primary types: Functional Components and Class Components.

2.1. Functional Components

Functional components are the simpler of the two types. They are JavaScript functions that return JSX (JavaScript XML), which is a syntax extension for JavaScript that looks similar to HTML. In modern React, functional components are the preferred way to write components.

2.1.1. Example of a Functional Component

import React from 'react';

function Greeting() {
  return <h1>Hello, welcome to React!</h1>;
}

export default Greeting;

In the above example, the Greeting component is a function that returns a simple heading element. Notice how the function doesn’t have any internal state or lifecycle methods.

2.2. Class Components

Class components are a more traditional way to define React components. They use ES6 classes and must extend React.Component. Class components are more powerful because they can have internal state and lifecycle methods, but they are less commonly used in modern React development in favor of functional components and hooks.

2.2.1. Example of a Class Component

import React, { Component } from 'react';

class Greeting extends Component {
  render() {
    return <h1>Hello, welcome to React!</h1>;
  }
}

export default Greeting;

While class components were once the norm, React’s move towards functional components with hooks has significantly reduced their use.

3. JSX: The Syntax Used in React Components

3.1. What is JSX?

JSX (JavaScript XML) is a syntax extension for JavaScript used in React components. It allows you to write HTML-like code within JavaScript, making it easier to visualize the component’s structure.

3.1.1. JSX in Action

import React from 'react';

function HelloWorld() {
  return (
    <div>
      <h1>Hello, World!</h1>
      <p>This is a JSX example.</p>
    </div>
  );
}

export default HelloWorld;

In the above example, we used JSX to return a <div> element containing an <h1> and a <p> tag. Notice that JSX tags are written similarly to HTML, but they are ultimately transformed into JavaScript function calls by React’s build tools.

3.2. Why JSX?

JSX is not mandatory in React, but it greatly enhances readability and development efficiency. It allows you to combine the logic (JavaScript) and the view (HTML-like structure) in one place. This makes React code more intuitive and easier to maintain.

4. Props: Passing Data Between Components

4.1. What are Props?

Props (short for “properties”) are used to pass data from a parent component to a child component. In React, props are read-only and cannot be modified by the child component. They allow for dynamic rendering of UI elements based on the data passed down from the parent.

4.2. Using Props in React Components

4.2.1. Example of Passing Props

import React from 'react';

function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}

function App() {
  return <Greeting name="John" />;
}

export default App;

In this example, the Greeting component receives a name prop from its parent component, App, and uses that prop to render a personalized greeting message. The props object holds all the data passed into a component, and you can access individual values using dot notation (props.name).

5. State: Managing Component Data

5.1. What is State?

State is a special JavaScript object that stores data that can change over time and trigger a re-render of the component. While props are immutable and passed from parent to child, state is local to a component and can be updated by that component itself.

5.2. Using State in Functional Components

In functional components, state is managed using the useState hook.

5.2.1. Example of Using useState Hook

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
    </div>
  );
}

export default Counter;

In this example, the Counter component manages a count state variable. When the button is clicked, the setCount function updates the state, and React re-renders the component with the updated count.

5.3. Using State in Class Components

In class components, state is initialized in the constructor and updated with this.setState().

5.3.1. Example of Using State in a Class Component

import React, { Component } from 'react';

class Counter extends Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
  }

  increment = () => {
    this.setState({ count: this.state.count + 1 });
  };

  render() {
    return (
      <div>
        <p>You clicked {this.state.count} times</p>
        <button onClick={this.increment}>Click me</button>
      </div>
    );
  }
}

export default Counter;

In this class-based example, we manage the count state by initializing it in the constructor and updating it with this.setState.

6. Component Lifecycle (For Class Components)

6.1. What is Component Lifecycle?

Class components in React have a lifecycle, which consists of a series of methods that get called at different points in a component’s existence. These methods allow you to perform specific actions, such as fetching data or cleaning up resources.

6.1.1. Common Lifecycle Methods

  • componentDidMount(): Called once the component has been rendered to the screen.
  • componentDidUpdate(): Called after the component’s updates are applied.
  • componentWillUnmount(): Called just before the component is removed from the DOM.

6.2. React Hooks and Lifecycle

In functional components, lifecycle methods are handled with React Hooks, such as useEffect. This allows you to perform side effects (e.g., data fetching or subscriptions) in functional components.

6.2.1. Example of useEffect

import React, { useState, useEffect } from 'react';

function FetchData() {
  const [data, setData] = useState(null);

  useEffect(() => {
    fetch('https://api.example.com/data')
      .then(response => response.json())
      .then(data => setData(data));
  }, []); // Empty dependency array means this effect runs only once, similar to componentDidMount

  return <div>{data ? JSON.stringify(data) : 'Loading...'}</div>;
}

export default FetchData;

In this example, the useEffect hook fetches data when the component mounts. This is similar to the componentDidMount method in class components.

7. Advanced Component Patterns

7.1. Higher-Order Components (HOCs)

A Higher-Order Component (HOC) is a function that takes a component and returns a new component with additional props or functionality. HOCs are useful for reusing component logic.

7.2. Render Props

A render prop is a function prop that a component uses to know what to render. This pattern allows you to pass a function to a component, which gives it control over how to render the UI.

8. Conclusion

React components are the foundation of any React application. Understanding the different types of components, how to manage props and state, and how to utilize React’s lifecycle methods will make you a more effective React developer. While the React ecosystem continues to evolve, the principles of components, hooks, and JSX remain central to React development.

By breaking your app down into components and leveraging the power of React’s tools, you can create dynamic, scalable applications that are easy to maintain and extend.

React Components   React Tutorial   JavaScript   Functional Components   State and Props  
React Components   React Tutorial   JavaScript   Functional Components   State and Props  
 How React Works Behind the Scenes (Simple Explanation)
React vs. Vanilla JavaScript: Which One Should You Learn? 

More Reading!

  1. How TypeScript’s Type Inference Works (And Why It’s a Game-Changer)
  2. React State vs. Props: Understanding the Key Differences
  3. React Event Handling: A Beginner’s Guide to onClick, onChange & More
  4. What Is the Nullish Coalescing Operator (??) in JavaScript?
  5. Short-Circuiting in JavaScript: Master Logical Operators Like a Pro
On this page:
1. What Are React Components? 1.1. Definition of React Components 1.2. Why Are Components Important? 2. Types of React Components 2.1. Functional Components 2.2. Class Components 3. JSX: The Syntax Used in React Components 3.1. What is JSX? 3.2. Why JSX? 4. Props: Passing Data Between Components 4.1. What are Props? 4.2. Using Props in React Components 5. State: Managing Component Data 5.1. What is State? 5.2. Using State in Functional Components 5.3. Using State in Class Components 6. Component Lifecycle (For Class Components) 6.1. What is Component Lifecycle? 6.2. React Hooks and Lifecycle 7. Advanced Component Patterns 7.1. Higher-Order Components (HOCs) 7.2. Render Props 8. Conclusion
Follow me

I work on everything coding and technology

   
Mapagam
Mapagam is your go-to resource for all things related to frontend development. From the latest frameworks and libraries to tips, tutorials, and best practices, we dive deep into the ever-evolving world of web technologies.
Licensed under Creative Commons (CC BY-NC-SA 4.0).
 
Frontend
JavaScript 
Web Api 
TypeScript 
React 
Social
Linkedin 
Github 
Mapagam
Code copied to clipboard