Sub topic 2
Quick refresher: state belongs to a component
This chapter assumes you can write function components, use JSX, pass props, and call useState with an onChange handler. It reviews one idea that the rest of the tutorial builds on: each component instance owns its own state.
State is private to the instance
useState gives a component a value that React remembers between renders, plus a function that updates it. React stores that value per component instance, not per component function. If you render the same component twice, you get two independent pieces of state.
import { useState } from 'react';
function Field({ label }) {
const [text, setText] = useState('');
return (
<label>
{label}: <input value={text} onChange={e => setText(e.target.value)} />
</label>
);
}
export default function App() {
return (
<>
<Field label="First" />
<Field label="Second" />
</>
);
}Type into the first input and the second stays empty. Each Field called useState('') on its own, so each has its own text. Calling setText in one instance never touches the other.
What a state update does
Calling a setter does not change the variable in place. It asks React to re-render that component with a new value. The sequence is:
- An event fires, such as
onChange. - The handler calls
setText(newValue). - React re-renders the component that owns the state.
useStatereturns the new value, and the JSX shows it.
The diagram shows the two states side by side with no link between them. That separation is useful when the data really is local, such as whether a dropdown is open. It becomes a problem when two components need to show the same data.
Where state lives
State belongs to the position of a component in the tree. Two things follow from this:
- A parent cannot read a child's state directly.
- A sibling cannot read it either.
Data in React flows down through props. A component can only receive values from above, so a value held inside one child is invisible to everything else.
| Question | Answer |
|---|---|
| Who can read a component's state? | Only that component, and its children if it passes the value as props. |
| Who can change it? | Only that component, through its setter. A child can ask for a change if it receives a callback. |
| What happens when two instances render? | Each gets its own copy. |
| What resets it? | Removing the instance from the tree, or giving it a different `key`. |
The last row matters in practice. If a component unmounts and later mounts again, its state starts over from the initial value.
A common mistake at this stage
A frequent error is to assume two inputs "share" text because they come from the same component. They do not. The component function is shared, but the state is not.
You can spot the situation by asking: "If I change this value here, must something elsewhere change too?" If yes, local state in each place is the wrong tool. The next chapters cover why that fails and how to fix it by moving the state to a common parent.
Check yourself
Exercise
App renders <Field label="A" /> and <Field label="B" />, and each Field calls useState(''). You type "hi" into A. What does B show, and why?
Show answer
B stays empty. Each Field instance has its own state, so setText in A re-renders only A and never changes B's text.
Exercise
A parent component wants to display the text typed into its child's input. Why can it not read the child's text state, and what direction does data normally flow in React?
Show answer
State is private to the component that declares it, so the parent has no access to it. Data flows down from parent to child through props, so a value held only in the child cannot reach the parent without being moved up.