REACT / LESSON 06
Props & Data Flow.
Learn how to pass data between components using props and understand React's one-way data flow

Loading the lesson…
Loading activities…
REACT / LESSON 06
Learn how to pass data between components using props and understand React's one-way data flow

Loading the lesson…
Loading activities…
Your components are pretty good at handling their own state and events at this point. Real apps are built from LOTS of components working together, though. A parent component has to hand data down to its children, and those children need some way to talk back up. That's where props come in.
Props (short for "properties") are how a parent component passes data to a child component. They work a lot like function arguments. You pass values in and the child uses them.
The parent decides what data to pass. The child decides how to show it. Keeping those two jobs separate is what makes components reusable, so the same UserCard works for any user you throw at it.
These two trip up a lot of beginners, but they've got different jobs.
| Feature | Props | State |
|---|---|---|
| Who controls it? | Parent component | The component itself |
| Can the component change it? | No (read-only) | Yes (with setter function) |
| Purpose | Configure a child from outside | Track changing data inside |
| Triggers re-render? | Yes, when parent passes new values | Yes, when setter is called |
You'll almost always destructure props right in the function parameter. It's a lot cleaner than typing props.name, props.age and so on over and over.
You can give props default values using plain old JavaScript default parameters.
There's one special prop called children, and it holds whatever you put between a component's opening and closing tags.
The children prop is what makes wrapper components possible. Those are components that handle styling or layout without needing to know (or care) what's inside them.
React enforces one-way data flow, which means data flows down from parent to child through props. That keeps your app predictable, because you always know where data came from... just look at the parent.
Parents pass data to children through props. The parent is the "source of truth" for that data.
A child can't reach up and change its parent's state. It's a one-way street (and honestly that's a good thing).
To talk upward, parents pass functions down as props. Children call those functions with some data, and the parent's function updates the parent's state.
When a child needs to update something in the parent, the parent hands it a callback function.
When somebody clicks "React" in the child, it calls onSelect('React'). That's secretly the parent's setSelectedItem wearing a different name tag. The parent's state updates and the new selection shows up on screen.
Here's props and data flow working together in a mini todo app.
Data flows down (the todos prop goes from TodoApp to TodoList to TodoItem) and actions flow up (clicking an item calls onToggle, which reaches all the way back up to TodoApp to update state). Read that one twice if you need to. It's THE pattern.
You can build components that talk to each other through props and callbacks now. So what about stuff that happens outside your component, like fetching data from an API, setting timers or updating the page title? Next lesson covers useEffect and component lifecycle, which is how React deals with side effects...