Sub topic 4
The problem: two components, two copies of the truth
This chapter assumes you can write function components, pass props, and use useState with an onChange handler. It shows what goes wrong when two sibling components each keep their own state for what should be one value.
Each instance owns its own state
Every time a component renders in the tree, React gives that instance its own state. Calling useState inside Field does not create one shared variable. It creates a separate slot for each Field on the screen.
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 "hello" into the first input. The second input stays empty. Each Field calls useState('') on its own, so there are two strings in memory and no link between them.
Why the siblings cannot sync
The diagram shows where each copy lives.
Two rules of React data flow cause this:
- Data moves down through props, from parent to child.
- A child cannot read the state of a sibling, and it cannot reach into its parent's state.
Field has no way to say "my text changed, update the other field." The information stays inside the component that created it.
Why this is a bug, not just a limitation
If the two inputs are meant to show the same value, you now have two copies of what should be one fact. Copies drift apart. In a real application this looks like:
- A search box and a results header that show different queries.
- A filter sidebar and a product list that disagree about the active category.
- Two views of one record where only one reflects the latest edit.
You could try to patch the drift. For example, you could add an effect in each Field that tries to copy its text to the other. This makes things worse. The effect needs a handle on the other component, so you add more state to track the copies, and you can end up with render loops or a stale value in one of the inputs. The fix is to stop having two copies.
Spotting the pattern
You are probably facing this problem when:
- Two or more components display or edit the same piece of information.
- Each one calls
useStatefor it. - A change in one should be visible in the other, but it is not.
A related variant is a rule that spans components. In an accordion, opening one panel must close the others. A single panel cannot enforce this. Each panel's own isOpen state knows nothing about its neighbours.
What the fix requires
The solution needs three things, which later chapters build step by step:
- One component holds the value. It must be an ancestor of every component that uses the value.
- That component passes the value down as a prop.
- That component also passes down a function, so children can ask for a change.
For now, the point is the diagnosis. The sync problem is not caused by React being unable to share data. It happens because the state was placed in the wrong component. Putting the state in a component that is above both fields, and not inside either one, is the fix.
Common errors in this situation
- Copying a prop into state. Writing
useState(props.value)captures the value once, on the first render. Later prop changes are ignored, so the child is again out of sync with its parent. - Reaching for a module-level variable. A plain
let shared = ''outside the component changes without causing a re-render. The screen does not update. - Syncing with effects. An effect that copies one state into another adds an extra render pass and a window where the two values disagree.
Exercise
In the two-Field example, you type "abc" into the first input. Why does the second input stay empty, and what is the underlying cause?
Show answer
Each Field instance has its own useState slot, so there are two separate text values. Typing updates only the first instance's state and re-renders only that component. The second Field cannot see that state, because React passes data down through props and siblings do not share state. The cause is that state which should be shared is owned by the wrong components.