React.js / React Components and Props

Passing and Managing Props in React

This tutorial will teach you how to pass and manage props in React. You'll learn how to pass data from parent components to child components and how to use this data in the child …

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Explores functional and class components, props, and component reusability.

React Props Tutorial

1. Introduction

Goal of the tutorial: This tutorial aims to provide a comprehensive understanding of how to pass and manage props in React. Props are a way of passing data from parent components to child components in React.

What you'll learn:

  • The concept of props in React.
  • How to pass props from parent to child components.
  • How to access and manage props in child components.

Prerequisites: Basic understanding of React, JavaScript, and JSX.

2. Step-by-Step Guide

Understanding Props

In React, "props" is a special keyword that stands for properties. Props are used to pass data from one component to another. Particularly, they are always passed from parent component to child component.

Passing Props

To pass props to a child component, you add them as attributes when you use the child component inside the parent component.

<ChildComponent someProp={value} />

Accessing Props

Inside the child component, you can access the props using this.props in a class component or as an argument in a functional component.

function ChildComponent(props) {
  return <p>{props.someProp}</p>;
}

3. Code Examples

Example 1: Passing and Accessing Props

// Parent Component
function ParentComponent() {
  return <ChildComponent someProp="Hello, World!" />;
}

// Child Component
function ChildComponent(props) {
  return <p>{props.someProp}</p>; // Outputs: Hello, World!
}

In the above example, "Hello, World!" is passed as a prop from ParentComponent to ChildComponent. Inside ChildComponent, we access someProp and render it inside a paragraph.

Example 2: Passing Multiple Props

// Parent Component
function ParentComponent() {
  return <ChildComponent prop1="Hello" prop2="World!" />;
}

// Child Component
function ChildComponent(props) {
  return <p>{props.prop1} {props.prop2}</p>; // Outputs: Hello World!
}

Here, we pass two props from ParentComponent to ChildComponent. Both are accessed and rendered inside a paragraph.

4. Summary

In this tutorial, you've learned about props in React and how to pass and access them in child components. As a next step, you could learn about prop types and how to use them to validate the data being passed as props.

5. Practice Exercises

  1. Create a ParentComponent that passes a prop name to a ChildComponent. The ChildComponent should render "Hello, name!".

  2. Create a ParentComponent that passes two props firstName and lastName to a ChildComponent. The ChildComponent should render "Hello, firstName lastName!".

Solutions:

// Parent Component
function ParentComponent() {
  return <ChildComponent name="John" />;
}

// Child Component
function ChildComponent(props) {
  return <p>Hello, {props.name}!</p>; // Outputs: Hello, John!
}

2.

// Parent Component
function ParentComponent() {
  return <ChildComponent firstName="John" lastName="Doe" />;
}

// Child Component
function ChildComponent(props) {
  return <p>Hello, {props.firstName} {props.lastName}!</p>; // Outputs: Hello, John Doe!
}

Happy coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

MD5/SHA Hash Generator

Generate MD5, SHA-1, SHA-256, or SHA-512 hashes.

Use tool

Backlink Checker

Analyze and validate backlinks.

Use tool

Age Calculator

Calculate age from date of birth.

Use tool

AES Encryption/Decryption

Encrypt and decrypt text using AES encryption.

Use tool

Open Graph Preview Tool

Preview and test Open Graph meta tags for social media.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help