React
React Components
Web Development
JavaScript
Frontend Development
UI Components
Software Engineering

What Are React Components and How Do They Work?

Listen to article
Deepak Tewatia
September 20, 2025
4 min read

What Are React Components?

React components are small pieces of code that help build user interfaces. Think of them like building blocks. Each block has a specific job. For example, a button component shows a button on the screen, while a list component displays a list of items. This way, each part of your webpage or app can be managed separately.

Why Use React Components?

Using components in React makes it easier to create and manage complex user interfaces. Here's the thing: when you change one component, it can update the whole app without needing to reload the page. This fast updating is great for user experience. It also makes your code cleaner and easier to understand.

How Do React Components Work?

Components in React can be grouped into two main types: class components and functional components.

Class Components

Class components are part of JavaScript's class system. They can hold their own state, which means they can remember information. Here’s a simple example:

class MyButton extends React.Component {
  constructor(props) {
    super(props);
    this.state = { clicked: false };
  }

  handleClick = () => {
    this.setState({ clicked: true });
  }

  render() {
    return (
      <button onClick={this.handleClick}>
        {this.state.clicked ? 'Clicked!' : 'Click me'}
      </button>
    );
  }
}

In this example, we create a button that changes text when clicked. The component uses state to track whether the button has been clicked.

Functional Components

Functional components are simpler and are usually written as plain JavaScript functions. They can use hooks to manage state and effects. Here’s how a functional component looks:

import React, { useState } from 'react';

function MyButton() {
  const [clicked, setClicked] = useState(false);

  return (
    <button onClick={() => setClicked(true)}>
      {clicked ? 'Clicked!' : 'Click me'}
    </button>
  );
}

In this version, we use the useState hook to handle the button's clicked state. It's shorter and easier to read than the class component.

Props: Passing Data to Components

Props, short for properties, allow you to pass data to components. This is how components can share information. Here’s how to use props:

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

 // Renders: Hello, Alice!

In this example, we create a Greeting component that takes a name as a prop and displays a greeting message. You can use different names each time you call the component.

State vs. Props

It’s important to understand the difference between state and props. State is internal. It can change within a component. Props are external and are set by the parent component. They don’t change unless the parent changes them. Here's a simple chart to summarize:

  • State: Managed within the component
  • Props: Received from parent components

Creating a Simple App with Components

Let’s put everything together and create a small app using components. We’ll create an app that displays a list of names and allows users to add a new name.

import React, { useState } from 'react';

function NameList() {
  const [names, setNames] = useState(['Alice', 'Bob']);
  const [newName, setNewName] = useState('');

  const addName = () => {
    setNames([...names, newName]);
    setNewName('');
  }

  return (
    <div>
      <h1>Name List</h1>
      <ul>
        {names.map((name, index) => <li key={index}>{name}</li>)}
      </ul>
      <input 
        type="text" 
        value={newName} 
        onChange={(e) => setNewName(e.target.value)} 
      />
      <button onClick={addName}>Add Name</button>
    </div>
  );
}

In this app, we use the NameList component to show a list of names. Users can type a name into an input box and click a button to add it to the list. The state manages the list of names and the new name.

Conclusion

React components are powerful tools for web development. They help create clean, organized, and interactive user interfaces. By understanding how components work, how to use props, and how to manage state, you can build amazing web apps. Remember, each component is like a small puzzle piece. When you put them together, they create a complete picture.

Comments

Y
You
Commenting
0/2000
Loading comments…