ProCodebaseProCodebase
  • ModusA Growth OS for your business, on WhatsAppKiosqSell more. Chase less.AI InterviewerAutomated screening & AI-led interviewsXperto AIAI prep companion for candidatesAI Tools HubResume builder, learning paths & more
  • Pre-Vetted DevelopersScreened, scored and ready to interviewAI-Native DevelopersSenior engineers on an hourly basis
  • Services
  • Features
  • Jobs
  • FAQs
Sign inBook a demo
  • Services
  • Features
  • Jobs
  • FAQs
Sign inBook a demo
ProCodebaseProCodebase

ProCodebase Technologies builds AI products for hiring and growth, and ships software for clients as a technical consultancy. We source, screen and deliver pre-vetted developers — so you only interview high-signal candidates.

Products

  • Modus
  • Kiosq
  • AI Interviewer
  • Xperto AI
  • AI Tools Hub

Hire & build

  • Pre-Vetted Developers
  • AI-Native Developers
  • Technical Consultancy
  • MVP Development
  • Features

Resources

  • Articles
  • Topics
  • Certifications
  • Collections
  • Jobs

Company

  • About Us
  • Contact Us
  • Book a Demo
  • FAQs

© 2026 ProCodebase Technologies. All rights reserved.

  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Props: Passing Data Between Components in React

author
Generated by
Abhishek Goyan

24/08/2024

React

Sign in to read full article

React is a powerful JavaScript library that allows developers to create interactive UIs with ease. One of its core principles revolves around modularity; instead of building a monolithic application, you can break it down into smaller, reusable parts known as components.

Understanding Props

Props are a mechanism for passing data and event handlers down from parent components to child components in React. They help you create dynamic and interactive user interfaces by ensuring that components can share the information they need to render properly.

The Basics of Props

The term “props” stands for properties, and as the name suggests, they are a way of passing properties from one component to another. Props are immutable, which means that a child component cannot modify the props it receives. This immutability helps maintain a predictable data flow through your application.

How to Use Props: A Simple Example

Let’s explore how props work in practice with a simple example. Suppose we have a parent component (App) and a child component (Greeting). The parent component will pass a name as a prop to the child component, which will then display a greeting message.

Here is the basic structure of our components:

// Greeting.js import React from 'react'; const Greeting = (props) => { return <h1>Hello, {props.name}!</h1>; }; export default Greeting; // App.js import React from 'react'; import Greeting from './Greeting'; const App = () => { const userName = 'Alice'; return ( <div> <Greeting name={userName} /> </div> ); }; export default App;

Breakdown of the Example

  1. Greeting Component: This is our child component that takes props as an argument. Inside this component, we access props.name to display a personalized greeting.

  2. App Component: This is the parent component that holds the state or data (userName in this case). We pass this userName as a prop to the Greeting component using the JSX-like syntax: <Greeting name={userName} />.

  3. Rendering: When the App component renders, React creates an instance of the Greeting component, passing the value of userName to it. The Greeting then renders the message "Hello, Alice!".

Why Use Props?

Props offer several benefits:

  • Reusability: They make your components reusable. You can render the Greeting component with different names without modifying its internal code.
  • Organization: They help organize data flow within your application, making it easier to manage and understand.
  • Separation of Concerns: Props encourage a clear separation between different parts of your application, maintaining a unidirectional data flow.

Handling Events with Props

Props are not just limited to passing data; you can also pass functions (event handlers) to child components. Let’s enhance our previous example by adding a button that allows the user to change the greeting.

Here’s how to do it:

// Greeting.js import React from 'react'; const Greeting = (props) => { return ( <div> <h1>Hello, {props.name}!</h1> <button onClick={props.onChangeName}>Change Name</button> </div> ); }; export default Greeting; // App.js import React, { useState } from 'react'; import Greeting from './Greeting'; const App = () => { const [userName, setUserName] = useState('Alice'); const changeName = () => { setUserName('Bob'); }; return ( <div> <Greeting name={userName} onChangeName={changeName} /> </div> ); }; export default App;

How It Works

  1. State Management: We’ve introduced a state in the App component using the useState hook to manage the userName.

  2. Passing Functions: We pass a function changeName as a prop to the Greeting component. This function changes the current user’s name when called.

  3. Event Handling: In Greeting, we set up an onClick event listener on the button. When clicked, it will invoke the changeName function, updating the userName.

Conclusion

By effectively using props, you can create dynamic and reusable components that enhance your application’s structure and logic. Each child component receives the data it needs directly through props, resulting in a clean and maintainable architecture.

Popular tags

ReactPropsComponents

Share now!

Like & bookmark

Related collections

  • Mastering React Concepts

    24/08/2024 · ReactJS

  • React Interview Challenges: Essential Coding Problems

    14/09/2024 · ReactJS

Related articles

  • Building a Simple React Application: Putting It All Together

    24/08/2024 · ReactJS

  • Styling React Components: CSS Styled-Components

    24/08/2024 · ReactJS

  • Testing React Components with Jest and React Testing Library

    24/08/2024 · ReactJS

  • Getting Started with React Storybook and Cypress

    03/09/2024 · ReactJS

  • Performance Optimization in React: Techniques and Best Practices

    24/08/2024 · ReactJS

  • Understanding State Management in React: A Comprehensive Guide

    24/08/2024 · ReactJS

  • Introduction to React Hooks: useState and useEffect

    24/08/2024 · ReactJS

Popular category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design