← BYTES
React.js·Intermediate·Ref: 4.2.2

Two sibling components both need access to the same piece of state, but there's no existing parent holding it. What is the standard React pattern for this?

Have a question about this video? Mention reference 4.2.2 when you contact us.

a.Each sibling keeps its own independent copy of the state and they are kept in sync via setTimeout polling
b.Use global mutable variables outside of React entirely
c."Lift state up" -- move the shared state into their nearest common parent, and pass it down to both as props
d.This situation is impossible to solve in React without a third-party library
The idiomatic fix is to "lift state up": move the state to the closest common ancestor of the components that need it, then pass the value and an updater function down as props to both children.

Find this on YouTube & Instagram

Every BYTES video is tagged with its reference number and topic — search any of these on YouTube or Instagram to find this one:

#BYTES#BYTES422#Reactjs