I have parent component, which contains a global value in its state. Change of this value must trigger change of states of children components as in following example:
function Parent(props) {
const [parentState, setParentState] = useState(someState);
return (
<button onClick={() => setParentState(newState)>Button</button>
<ChildComponent parentState={parentState} />
}
function Child(props) {
const [childState, setChildState] = useState(getState(props.parentState));
}
Click on the button in this example should change trigger non-trivial change of the state in the Child component (which uses parentState as a parameter to generate its own state).
I guess the first answer to the problem would be to lift the state up - however I would like not to do that, because there are many Child generated through a .map function - I would need to retain their states in an array and propagate it to all the child components. The value in the parent component is a global value relevant for all the children and all the children must change their states when the parentState changes.
Is there any good way to achieve that without lifting the Child components' states? I hope I have described the problem well enough - if not I will be glad to update the information.