Frontend / Props vs state

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.

Beginner7 steps~6 min
Component — a function that returns what to show
Prop — a value passed in by the parent
State — a value the component owns and can change
// THE SHORT VERSION

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

STEPS01 / 07

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 01 / 07 Props come from the parent
THE MATH
<Product name="Lamp" price={20} />
  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
VALUES SO FAR
name'Lamp'prop
price20prop
props····rule
qty····state
click····setState
sale····new prop
Total····problem
owner····lifted
total····result
// BEFORE

Learn this first

Nothing. This concept starts from scratch.

Spotted a mistake on this page?