I am trying to update my child component based on parent state and similarly I am trying to update the parent state by clicking on a button in child component. Both is fine but while clicking on a button in child it’s updating the parent but instead of the only clicked child iits opening both the child how to solve the issue using react hook? I want to open only one child while clicking child but the Expand all button of parent should change to collapse all.
I am adding my codesandbox for better understanding
Parent to child update and child to parent by a event handler is happening but while clicking child i want to update the state of parent but not to open both the child only clicked child?
2
Answers
You can take a additional state in Parent.js a long with State handler.
Pass the handler in the Child component.
Call the handler from the Child.js
In the Parent button, you need to handle onclick event based on the
expandCount
state.Give that a try.
The problem is you put 2 child button inside an Child component and you pass the same open state to both of them.
The solution is split 2 button into 2 Child component.
There is your code
I would like to change into
So you can pass the buttonName and color props to each child to custom them. And remove handleCollapse()
from button() click event to prevent toggle both children.
About parent node.
Change isExpanded state to array to hold more child state. With this isExpanded state you can check did thay all expanded with every method.
};