React Props Explained: Pass Data Between Components

Resumen

Pixel shares a beginner-friendly guide to React props, explaining how components pass data using examples like Profile and Student. It covers multiple props, supported data types, and destructuring, helping the community build clearer, reusable React applications.

DEV Community

Props in React

When building a React application, we usually divide our application into small components. For example, an e-commerce website may have: Navbar component Product component Cart component Footer component Sometimes, one component needs to send data to another component. This is where Props come in. 1. What are Props? Props stands for Properties. Props allow us to pass data from one component to another in React. In simple words, props help components share data with other components. For example, we can send a username from the App component to the Profile component. 2. How to Pass Props? Let's understand with a simple example. App.jsx function App() {
return (

);
}

export default App;
Here, we are passing name="Lokesh" to the Profile component. Profile.jsx function Profile(props) {
return (

Hello, {props.name}


);
}

export default Profile;
Output: Hello, Lokesh
Let's understand what happened: App sends the name Lokesh to Profile. Profile receives the data through props. We display the name using props.name. That's how props work! 3. Passing Multiple Props We can pass more than one value to a component. function App() {
return (

);
}
Now, we can receive these values in the Student component. function Student(props) {
return (


Name: {props.name}


Age: {props.age}


Course: {props.course}



);
}
We can pass different types of data through props, such as: Strings Numbers Arrays Objects Functions Boolean values 4. What is Props Destructuring? Instead of writing props.name, props.age, and props.course every time, we can use destructuring. Example: function Student({ name, age, course }) {
return (

Name: {name}


Age: {age}


Course: {course}



);
}
Here, we directly get the values from props. This makes our code shorter and easier to read. 5. Passing Props to Reusable Components One of the biggest advantages of props is that we can reuse the same component with different data. Imagine we are creating an e-commerce website with multiple product cards. Instead of creating a separate component for every product, we can create one reusable Product component. App.jsx function App() {
return (

  <Product name="Mobile" price={20000} />

  <Product name="Headphones" price={2000} />
</div>

);
}
Product.jsx function Product({ name, price }) {
return (


{name}


Price: ₹{price}



);
}
The same Product component displays different products based on the props it receives. This helps us avoid writing the same code again and again. 6. Important Rules About Props There are a few important things to remember: Props are used to pass data between components. Data usually flows from parent components to child components. Props can contain different types of values. Props are read-only. A child component should not directly modify the props it receives. We can use destructuring to make props easier to read. 7. Props vs State Props and State are both used to work with data in React, but they have different purposes. Props State Used to receive data from a parent component Used to manage data inside a component Read-only for the receiving component Can be updated using a state setter Passed from parent to child Managed by the component that owns it For example: Props: Passing a product name from App to Product. State: Updating the quantity of a product when the user clicks the + button.
React Props Explained: Pass Data Between Components
Powered by Readybot.io

Lo último de Pixel

Stop Paying for AI Renders in Test Orders

Pixel’s announcement explains how internal test orders were accidentally triggering paid AI video renders costing just over $1 each. It details a server-side dry-run guard …

Sandblasting Grit Guide: 120 vs 220 vs 320

Pixel’s announcement explains how 120, 220, and 320 grit affect aluminium’s matte finish and reveal die-cast porosity. It also covers how pressure, blast distance, and …

NVIDIA Cosmos 3 Edge: Physical AI on Jetson

Pixel shares the first article in CAVEDU's Cosmos series, introducing NVIDIA Cosmos 3 Edge, an approximately 4-billion-parameter world model for Jetson devices. It explores multimodal …

Laravel Semantic Search with OpenAI and Pinecone

Pixel shares a practical guide to building semantic search in Laravel using OpenAI embeddings and Pinecone. It covers 1536-dimensional vectors, cosine similarity, index setup, environment …