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
| Props | State | |
|---|---|---|
| Owner | parent | the component itself |
| Mutable by the component | no | yes, via setState/dispatch |
| Purpose | configuration, data from outside | interactive data that changes |
| Changes cause re-render | when the parent passes new values | when the setter is called |
| Declared with | function parameters | useState, 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 akey. - 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";23// 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}1112// Parent: owns state and passes it down as props13export function CounterPage() {14 const [count, setCount] = useState(0);15 const doubled = count * 2; // derived: compute, don't store1617 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.