If you're learning React, you might have seen the word "props" and wondered what it means. Don't worry! In this blog, weβll understand props in the easiest way possible. Think of it like this:
π§ What are Props?
π Props means properties.
π They are used to pass data from one component to another in React.
π Just like we pass ingredients to a recipe, we pass data (props) to a React component.
π§ Simple Analogy: Juice Shop
Imagine you own a juice shop. When someone places an order, they tell you the flavor they want β like mango, orange, or apple. You then prepare the juice using that flavor.
In React, the juice shop is your component and the flavor is the prop.
π οΈ Simple Example: Passing a Name
Letβs create a small app that says hello to different people.
Step 1: Create a Welcome component:
src>components>Welcome.jsx
import React from 'react'
function Welcome(props) {
return <h2>Hello, {props.name}!</h2>;
}
export default Welcome
Here:
π Welcome
is a component
π It receives props as an argument
π We access the name using props.name
Step 2: Use Welcome component and pass props:
src>App.jsx
import React from 'react'
function App() {
return (
<div>
<Welcome name="Himanay" />
<Welcome name="Aarav" />
<Welcome name="Emma" />
</div>
);
}
export default App
π We are using <Welcome />
three times
π Each time, we pass a different name
π props.name
will be "Himanay", "Aarav", "Emma" in each one
Output:
Hello, Himanay!
Hello, Aarav!
Hello, Emma!
π You just used props to pass data to a component!
π¦ Little Complex Example: Passing an Object
Now letβs say we want to show a userβs profile with a name and an age.
Step 1: Create a Profile component:
src>components>Profile.jsx
import React from 'react'
function Profile(props) {
return (
<div>
<h3>Name: {props.user.name}</h3>
<p>Age: {props.user.age}</p>
</div>
);
}
export default Profile
π props.user
is an object
π We use props.user.name
and props.user.age
to show the data
Step 2: Use the Profile component:
src>App.jsx
import React from 'react'
function App() {
const user1 = { name: "Himanay", age: 28 };
const user2 = { name: "Sophia", age: 22 };
return (
<div>
<Profile user={user1} />
<Profile user={user2} />
</div>
);
}
export default App
π§ We created two user objects and passed them as props
π§ Inside the Profile
component, we used that data to show profile info
Output:
Name: Himanay
Age: 28
Name: Sophia
Age: 22
π§Ό Clean Tip: Destructuring Props
To make your code cleaner, you can also destructure props like this:
function Welcome({ name }) {
return <h2>Hello, {name}!</h2>;
}
And:
function Profile({ user }) {
return (
<div>
<h3>Name: {user.name}</h3>
<p>Age: {user.age}</p>
</div>
);
}
π This helps avoid writing props.
again and again.
π Recap
β
Props help us pass data from one component to another
β
Props are read-only, we cannot change them inside the component
β
You can pass strings, numbers, arrays, objects, even functions as props
β
Use destructuring to write cleaner code
β Final Thoughts
Learning props is a big step in React. It helps you build reusable and dynamic components. Start small like we did here, and then build more complex UIs using the same idea.
If you understood this, give yourself a high five! ποΈ
Now go ahead and try making your own components using props in CodeAcademix or any React app.
Happy Coding! π»β¨
Written by Himanay Khajuria
Top comments (0)