Understanding Props in React

Introduction:
Props in React are a way to pass data from one component to another. They allow you to create reusable components that can receive different values and behave accordingly. In this tutorial, we will use a simple analogy to understand props and see examples of how they can be used in React applications.

Analogy:
Imagine a factory that produces customized cars. The factory has a blueprint for a standard car, but you can also request special features or colors for your car. In this analogy, the blueprint is a React component, and the special features or colors are the props.

Example 1: Creating a simple React component with props

Let’s create a simple Car component that accepts the color and features as props.

  1. Create a new React component called Car.js:
import React from 'react';

const Car = (props) => {
  return (
    <div>
      <h3>This car is {props.color}.</h3>
      <p>It has the following features:</p>
      <ul>
        {props.features.map((feature) => (
          <li key={feature}>{feature}</li>
        ))}
      </ul>
    </div>
  );
};

export default Car;

Now, let’s use the Car component in our App.js:

import React from 'react';
import Car from './Car';

const App = () => {
  return (
    <div>
      <Car color="red" features={['Air conditioning', 'Power steering']} />
      <Car color="blue" features={['Leather seats', 'Bluetooth']} />
    </div>
  );
};

export default App;

In this example, the Car component accepts color and features as props. The App component renders two Car components with different colors and features.

Example 2: Using props with default values

You can also provide default values for props using the defaultProps property on the component.

  1. Update the Car component to have default values for color and features
import React from 'react';

const Car = (props) => {
  return (
    <div>
      <h3>This car is {props.color}.</h3>
      <p>It has the following features:</p>
      <ul>
        {props.features.map((feature) => (
          <li key={feature}>{feature}</li>
        ))}
      </ul>
    </div>
  );
};

Car.defaultProps = {
  color: 'black',
  features: ['Standard features'],
};

export default Car;

Now, let’s use the Car component without passing any props in our App.js:

import React from 'react';
import Car from './Car';

const App = () => {
  return (
    <div>
      <Car />
    </div>
  );
};

export default App;

In this example, the Car component will use the default values for color and features when no props are passed.

Conclusion: Props are an essential part of React that allows you to create reusable components with different data and behaviors. By using props, you can create more flexible and maintainable applications. Remember the car factory analogy as you work with props, and you’ll have a better understanding of how they function in your React applications.

Hello, I’m Anuj. I make and teach software.

My website is free of advertisements, affiliate links, tracking or analytics, sponsored posts, and paywalls.
Follow me on LinkedIn, X (twitter) to get timely updates when I post new articles.
My students are the reason this website exists. ❤️

Feedback Display