Props in React explained with examples

props in react

Props in React pass data from one part to another part. They let you build parts that reuse data and logic with ease.

What are Props in React?

Props act as input for React parts. A parent part sends them to a child part. They let you pass text, numbers, or objects from one part to another part.

You pass props inside a tag like this:

<MyComponent name="John" age={25} />

Inside the part, you read props like this:

function MyComponent(props) {
  return <p>{props.name} is {props.age}</p>;
}

Props work as read-only data. A part cannot change its own props. A parent sends them and the child only reads them.

How Props Work in React Components

Props flow from parent to child parts. A parent gives a value, and a child shows that value. This pattern helps you write small parts and reuse them.

You can set a default value when a prop has no value from the parent:

function Welcome(props) {
  return <h1>Hello {props.name}</h1>;
}

Welcome.defaultProps = {
  name: 'Guest'
};

Here is another example that shows you how to pass props to functional components:

function Greet({ name }) {
  return <p>Hello {name}</p>;
}

Pass props to class components:

class Greet extends React.Component {
  render() {
    return <p>Hello {this.props.name}</p>;
  }
}

Prop types and validation:

import PropTypes from 'prop-types';

function Greet({ name }) {
  return <p>Hello {name}</p>;
}

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

The Difference Between Props and State

Props hold data that a parent gives to a child. A child cannot change them. State holds data inside the part. A part can change its own state.

Here is a table that shows you the key differences:

AspectPropsState
ChangeOnly parent can give new valuesThe part can change its own values
UsePass data from parent to childStore data inside the part

Use props to send data across parts. Use state to track data inside one part.

Nested Components and Props Flow

A parent part can send props down many levels. Each child passes props to the next child. This flow keeps data in one direction from top to bottom.

For example:

function Child({ text }) {
  return <p>{text}</p>;
}

function Parent() {
  return <Child text="Hello from parent" />;
}

This pattern keeps your app simple and easy to read.

Examples

Passing simple props to a function part:

function User({ name, age }) {
  return <p>{name} is {age} years old</p>;
}

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

This part shows a user name and age. A parent sends both values. The child shows them without change or logic.

Passing objects as props:

function Profile({ user }) {
  return <h2>{user.name} from {user.city}</h2>;
}

function App() {
  const person = { name: 'Maya', city: 'Riyadh' };
  return <Profile user={person} />;
}

This part shows how to send an object as one prop. The child reads the object fields and shows them.

Default props with class part:

class Welcome extends React.Component {
  render() {
    return <h1>Hello {this.props.name}</h1>;
  }
}

Welcome.defaultProps = {
  name: 'Visitor'
};

Nested props flow:

function Title({ text }) {
  return <h3>{text}</h3>;
}

function Header({ title }) {
  return <Title text={title} />;
}

function App() {
  return <Header title="Welcome to React" />;
}

This part shows props flow from parent to child to another child. Each part reads the prop and passes it to the next.

Wrapping Up

You learned what props are and how they move data in React. Here is a quick recap:

  • props pass data from parent to child, state stores data inside a part, and props flow only downward.

FAQs

What are props in React?

Props in React pass data from parent to child components. Props act as read-only values. They make components reusable and dynamic.

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

<Welcome name="John" />

How do you use props in functional components?

Props are passed as function parameters. You access them with props.propertyName inside the component.

function User(props) {
  return <p>User: {props.username}</p>;
}

<User username="Alex" />

What is the difference between props and state in React?

  • Props: Passed from parent, read-only, used for component data.
  • State: Managed inside component, can change, used for dynamic data.

function Counter() {
  const [count, setCount] = React.useState(0);
  return <button onClick={() => setCount(count + 1)}>{count}</button>;
}

Can props have default values in React?

Yes. You define default props with defaultProps in class components or by assigning directly in function components.

function Button(props) {
  return <button>{props.label}</button>;
}

Button.defaultProps = {
  label: "Click Me"
};

Similar Reads

React Lists and Keys: Manage Dynamic Elements

React Lists and Keys help you show many items and keep each item tracked for updates. What do Lists and…

Forms in React: How to Create and Manage User Inputs

Forms in React allow a page to take data from a user. A developer uses them to show fields, take…

React JS Introduction: What is React? the JavaScript Library

In this introduction, you will understand what React JS is and learn how to use NPM commands to get started…

Events in React: A Complete Guide for Beginners

Events in React are actions that a user does with a page. A user can click a button or type…

How to Render HTML Elements in React.js

Rendering HTML in React might sound like a complicated task, but it’s actually quite simple once you understand the basics.…

How to Install React on Ubuntu, Windows and MacOS

Through this article, you will learn how to install React on common operating systems such as Windows, macOS, and Ubuntu.…

React Components: A Complete Tutorial for Beginners

In this tutorial, you will learn what components are in React and how to build them. Also, how to pass…

Learn JSX in React: Syntax, Rules, and Examples

JSX helps write React UI with HTML-like syntax. It links JavaScript logic with markup in one view. What is JSX…

State in React Step by Step with Examples

State in React lets a component hold data that can change over time. You can use it to track values…

React Conditional Rendering Examples

React conditional rendering is a way to show different parts of a page. Click here to see how it works…

Previous Article

HTML meta http-equiv Attribute for Web SEO with Examples

Next Article

JavaScript toSorted Function Guide with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.