Props vs state
A shopping cart with one lamp in it shows what a component is given, what it owns, why both can redraw the screen, and where a value should live when two components need it.
In 30 seconds
A React component gets its data from two places. Props are handed in by the parent, like arguments to a function, and the component must not change them.
State is data the component owns and remembers between renders. It changes only through its setter, and each change makes React render the component again. A parent that renders again and passes new props has the same effect.
When two components need the same value, the state moves up to their closest shared parent and comes back down as props.
Props vs state, step by step
Shown with React function components and hooks. React can be told to skip re-rendering a child whose props did not change (memo); that optimisation is not drawn.
- Step 1: Props come from the parent. Cart renders Product and hands it two values, written like HTML attributes. Inside Product they arrive as props, the arguments of the component function. Product uses them to work out what to show: a lamp at $20.
- Step 2: Props are read-only. Props belong to whoever passed them. Product may read name and price as often as it likes but must never change them, just as a well-behaved function does not alter its arguments. If the price is to change, Cart has to pass a different one.
- Step 3: State is owned inside. Some data belongs to the component itself: here, how many lamps the user wants. useState(1) asks React to remember a value for this component, starting at 1, and returns it together with a function for changing it. That remembered value is state.
- Step 4: Setting state re-renders. Clicking + calls setQty(2). React stores the new value and calls Product again, which now reads qty = 2 and returns an updated screen. Changing state through its setter is what triggers the re-render; assigning qty = 2 directly would change nothing on screen.
- Step 5: New props re-render too. A sale starts: price is state inside Cart, and it changes to 15. Cart renders again, and so does its child Product, which receives the new prop. A component therefore re-renders for two reasons: its own state changed, or its parent rendered again. Product keeps its own state through this: qty is still 2.
- Step 6: Two components, one value. A new component, Total, should show quantity × price. But qty lives inside Product, and state is private: a sibling cannot reach into it. Data in React flows one way, from parent to child through props, so Total has no path to the number it needs.
- Step 7: Lift state up. The fix is to lift the state up: move qty into Cart, the closest parent of both. Cart passes qty down to Product and Total as a prop, and gives Product a function, onAdd, to call when + is clicked. With a single owner the two can never disagree: 2 × 15 = 30.
Try it yourself
Learn this first
Nothing. This concept starts from scratch.
Learn next
- Component lifecycleA component appears, updates and is removed: what runs at each moment, and why. Beginner
- One-way vs two-way data bindingWhich way data moves between a program and the screen, and what each direction costs. Beginner
- useEffect lifecycleWhen React effects run, when they clean up, and what the dependency array decides. Intermediate
Spotted a mistake on this page?