Props
You'll learn to pass values into a component with props, so one component can show different data.
Watch & read
Start with the required ones. The others are there when you need them.
See it step by step
Watch props flow from the parent into each child.
Explanation
You made a ScoreCard component, but it always shows Bangladesh and 245. You want the same card for
every team, with different values. A function solves this with parameters. A component solves it with
props (short for properties).
You pass props like HTML attributes, and the component receives them as one object:
<Student name="Rafi" year={2} />function Student({ name, year }) {return <li>{name}, year {year}</li>;}
React calls Student with { name: "Rafi", year: 2 }. The { name, year } in the brackets is
destructuring: it takes those two values out of the object. The component that writes <Student ... />
is the parent. Student is the child. Data flows one way, from parent to child.
Text goes in quotes: name="Rafi". Anything else goes in { }: numbers, true, arrays, objects,
functions. year="2" is the string "2", not the number 2. People often write numbers in quotes and
then get "200" + 60 = "20060".
A child must not change its props. They belong to the parent. If a value has to change, that is state, the next topic.
People also forget the { } in function Student({ name }). Without them, name is the whole props
object, and React shows an error about an object.
When you read a component, look at its brackets first: they list every value it expects from its parent.
Code tour
Click a line number with a ring to read the note for that line.
Practice
Short drills. Take your time and read the code before you answer.
1function Total({ price, delivery }) {2 return <p>Total: {price + delivery}</p>;3}45export default function App() {6 return <Total price="200" delivery={60} />;7}
Quick check
3 questions. Each one explains itself after you answer.
- 1 / 3What does
Studentreceive from<Student name="Rafi" year={2} />? - 2 / 3Can a child component change a prop it received?
- 3 / 3What is the type of
yearin<Student year="2" />?
Explain it back
How confident are you with Props?
Done
<Student name="Rafi" year={2} /> and say what the child component receives and shows.