React
Props
JavaScript
Web Development
Frontend
Coding
Software Engineering

How to Use Props in React

Listen to article
Deepak Tewatia
September 19, 2025
3 min read

Introduction

Props in React are a key part of building components. They allow you to pass data to your components and help make your application more flexible. When you use props, you can change what a component shows without having to rewrite it. Let’s break down how to use props effectively in your React projects.

What Are Props?

Props, short for properties, are used to send data from one component to another in React. Think of props like the ingredients you use in cooking. Each component can receive different props to produce a unique output. This makes your components reusable. Instead of creating new components for different situations, you can just pass in different props.

How to Pass Props

Passing props is simple. You add them to the component tag when you use the component. Here’s a straightforward example:

<code class="javascript">
function Greeting(props) {
    return <h1>Hello, {props.name}!</h1>;
}

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

In this example, the Greeting component takes a prop called name. When the App component renders Greeting, it passes "Alice" as the value for the name prop. So, the output will be:

<code class="plaintext">
Hello, Alice!

Accessing Props Inside a Component

You can access props inside a component through the function's parameters. In the previous example, we used props to access the name. You can also destructure props for clearer code. Here’s how:

<code class="javascript">
function Greeting({ name }) {
    return <h1>Hello, {name}!</h1>;
}

This version is cleaner and easier to read. You directly extract the name prop from props using destructuring.

Using Default Props

Sometimes, you might want your component to have a default prop value in case none is provided. You can set default props like this:

<code class="javascript">
Greeting.defaultProps = {
    name: "Guest"
};

Now, if you use Greeting without passing a name, it will display "Hello, Guest!"

Prop Types for Validation

To ensure your components get the right types of props, you can use prop types for validation. This is helpful for catching errors early. Here’s how to use prop types:

<code class="javascript">
import PropTypes from 'prop-types';

Greeting.propTypes = {
    name: PropTypes.string
};

Now, if someone passes a prop that is not a string, React will give a warning. This makes your code more reliable.

Passing Multiple Props

You can pass multiple props to a component, too. Here’s an example with more than one prop:

<code class="javascript">
function UserProfile({ name, age }) {
    return <p>{name} is {age} years old.</p>;
}

function App() {
    return <UserProfile name="Bob" age={30} />;
}

This component now displays both the name and age. It shows how versatile props can be in creating dynamic content.

Children Props

Sometimes, you might want to pass components or HTML elements as props. React allows you to do this with children. Here's how it works:

<code class="javascript">
function Container({ children }) {
    return <div>{children}</div>;
}

function App() {
    return <Container><p>This is inside the container.</p></Container>;
}

In this case, whatever you place between the Container tags will be rendered inside the div of the Container component.

Conclusion

Using props in React is a powerful way to make your components flexible and reusable. By passing data to your components, you can easily change how they behave and what they display. Always remember to validate your props to catch issues early. With these simple steps, you can take full advantage of props in your React applications. Happy coding!

Comments

Y
You
Commenting
0/2000
Loading comments…