Access React Child Component’s state in parent component
Jun 2, 2022
You have a parent component, in which you want to keep control of child component’s state, it is possible by replicating the child’s data in parent.
By passing a ref from parent component to child component.
For ref to work, child component should be wrapped in forwardRef.
Main Part
Whenever you update the child state, update that ref also at the same time.
With this done.
You have access to all the child states as it is also saved on the ref.