Discover how to make your app parts talk to each other effortlessly!
Why Sharing state between components in React? - Purpose & Use Cases
Imagine you have two separate parts of your app showing related information, like a shopping cart icon and a product list. You want both to update instantly when you add an item.
Manually syncing data between these parts means writing lots of code to pass info back and forth, which is confusing and easy to break. It's like trying to keep two walkie-talkies perfectly in sync by shouting messages constantly.
Sharing state between components lets React manage the data in one place. When the data changes, all parts that use it update automatically, keeping everything in sync without extra effort.
const [count, setCount] = useState(0);
<ProductList count={count} />
<CartIcon count={count} />const count = useSharedState('cartCount');
<ProductList />
<CartIcon />This makes your app feel smooth and connected, like all parts are talking naturally without you managing every detail.
Think of a music app where the play button and the song title both update instantly when you change tracks, no matter where you are in the app.
Manual syncing is hard and error-prone.
Shared state keeps data in one place for easy updates.
Components stay in sync automatically and smoothly.
Practice
Solution
Step 1: Understand the purpose of shared state
Sharing state means keeping data in one place so multiple components can access and update it.Step 2: Identify the benefit of shared state
This helps keep the app consistent and easier to manage because all components see the same data.Final Answer:
To keep data consistent and allow multiple components to use the same information -> Option AQuick Check:
Sharing state = data consistency [OK]
- Thinking each component should have its own separate state always
- Confusing props with state sharing
- Believing sharing state prevents rendering
Solution
Step 1: Recall how props are passed in JSX
Props are passed as attributes with values, likepropName={value}.Step 2: Identify correct syntax for passing state and updater
The parent passes both state and setState as props:<Child state={state} setState={setState} />.Final Answer:
<Child state={state} setState={setState} /> -> Option BQuick Check:
Props syntax = attribute=value [OK]
- Using children tags incorrectly to pass props
- Passing updater as a child instead of prop
- Missing curly braces around JavaScript expressions
function Parent() {
const [count, setCount] = React.useState(0);
return <Child count={count} setCount={setCount} />;
}
function Child({ count, setCount }) {
return <>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</>;
}Solution
Step 1: Understand initial state and props
The parent sets count to 0 initially and passes count and setCount to Child.Step 2: Analyze button click behavior
Clicking the button calls setCount(count + 1), increasing count by 1 each time.Final Answer:
Count: 0 initially, then increases by 1 each click -> Option CQuick Check:
useState count increments on click [OK]
- Thinking count starts at 1
- Assuming button does not update state
- Confusing setCount usage or scope
function Parent() {
const [value, setValue] = React.useState('');
return <Child value={value} setValue={value} />;
}
function Child({ value, setValue }) {
return <input value={value} onChange={e => setValue(e.target.value)} />;
}Solution
Step 1: Check props passed from Parent
Parent passessetValue={value}instead ofsetValue={setValue}, so setValue is a string, not a function.Step 2: Understand effect on Child component
Child tries to call setValue as a function on input change, but it is a string, causing an error.Final Answer:
setValue prop is passed incorrectly as value instead of the updater function -> Option DQuick Check:
Updater function must be passed, not state value [OK]
- Passing state value instead of updater function
- Ignoring prop names mismatch
- Assuming onChange syntax is wrong
function Parent() {
const [count, setCount] = React.useState(0);
return (
<>
<SiblingA count={count} setCount={setCount} />
<SiblingB count={count} setCount={setCount} />
</>
);
}What is the key reason this works?
Solution
Step 1: Understand state lifting
The parent holds the state and updater, passing them as props to both siblings.Step 2: Explain shared state effect
Because both siblings receive the same state and updater, changes in one update the parent's state, causing both to re-render with the new value.Final Answer:
State is lifted to the common parent and passed down, so both siblings share the same source -> Option AQuick Check:
Lift state up for shared data [OK]
- Thinking siblings can share state without lifting or context
- Believing context API is always required
- Assuming state sync happens automatically without shared source
Ready to prove it?
Pass the test and this pattern moves unknown
