What is the difference between props and state in React?
- Technology:
- React
- Topics:
- State Management
Quick answer
Props are read-only inputs passed from a parent component, while state is data a component owns and can change over time, which triggers a re-render when updated.
Detailed explanation
Props make a component reusable: the parent decides the values, and the child must not modify them. State is private memory for a component, created with useState or useReducer. Updating state tells React to re-render that component with the new value.
A good rule is to keep state as low as possible in the tree, and lift it up to the closest common parent only when two components need to share it. Data that can be calculated from props or state should be computed during render and not stored in extra state.
Example
function Counter({ step }) { // step is a prop (read-only)
const [count, setCount] = useState(0); // count is state (owned here)
return <button onClick={() => setCount(count + step)}>{count}</button>;
}
<Counter step={5} />How to explain it in an interview
Props are inputs passed from a parent and are read-only inside the child. State is data a component owns and changes over time with a setter, which triggers a re-render. If two components need the same data, I lift the state to their closest common parent and pass it down as props.
Key points
- Props flow down, are read-only and come from the parent
- State is owned by the component and changes cause re-renders
- Derived values should be calculated, not stored in state
Common mistakes
- Mutating props or state directly instead of creating a new value.
- Copying props into state without a reason, which leaves the two out of sync when the prop changes.