ReactEasy

Difference between props and state?

Props are data passed into a component by its parent and are read-only inside it. State is data a component owns and can change over time with a setter, which causes it to re-render. Props configure a component; state remembers things.

Side by side

PropsState
Ownerparentthe component itself
Mutable by the componentnoyes, via setState/dispatch
Purposeconfiguration, data from outsideinteractive data that changes
Changes cause re-renderwhen the parent passes new valueswhen the setter is called
Declared withfunction parametersuseState, useReducer

How they work together

A parent's state often becomes a child's props. The child reads the value and calls a callback prop to request a change; the parent updates its state; the new value flows down again. This is "lifting state up".

Common mistakes

  • Copying props into state (useState(props.value)) and expecting it to update when the prop changes; the initial value is used only on mount. Use the prop directly, or reset with a key.
  • Storing derived data in state. If it can be computed from props or other state (fullName, filtered lists), compute it during render.
  • Mutating either one. Always create new objects or arrays when updating state.

Code example

tsx
1import { useState } from "react";
2
3// Child: reads props, never owns `count`
4function CounterDisplay({ count, onIncrement }: { count: number; onIncrement: () => void }) {
5 return (
6 <p>
7 Count: {count} <button onClick={onIncrement}>+1</button>
8 </p>
9 );
10}
11
12// Parent: owns state and passes it down as props
13export function CounterPage() {
14 const [count, setCount] = useState(0);
15 const doubled = count * 2; // derived: compute, don't store
16
17 return (
18 <>
19 <CounterDisplay count={count} onIncrement={() => setCount((c) => c + 1)} />
20 <p>Doubled: {doubled}</p>
21 </>
22 );
23}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app