Question: How to remove dynamic children from Parent State?
See original GitHub issueI’m using react hook useContext
. I have two identical components (siblings), each that use the same context that is a list.
Scenario
- The first sibling is created, calls
useContext
, and then pushes something into the list. - The second sibling is then created, using the same
useContext
, and then pushes something into the list.
Issue The second sibling has the current state of list, which has two items, but the first sibling state is not updated with the second item that was pushed in by the second sibling
Expected
That each component that is using the same useContext
will be updated amongst all components that use the same context.
Is this a bug or am I misusing this? Any help or guidance is appreciated 🙇
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Reactjs: how to modify dynamic child component state or ...
Attempt: I attempted to pass some specific state of the parent to the children, so I can just update the parent state and...
Read more >How to set Parent State from Children Component in ...
Step 4: Create function setStateOfParent to set state of Parent in Parent component, also pass setStateOfParent function in children. Filename- ...
Read more >Learn how to use parent pages in InDesign
Learn how to create, apply, copy, delete, and import parent pages in InDesign.
Read more >About Dynamic Members
If a parent member is enabled for adding dynamic children, users can create ... Performing the Delete dynamic members operation does not remove...
Read more >Complete Guide to Managing Behavior Problems
Learn how to improve the parent-child relationship when it becomes strained with CMI's Parents Guide to Problem Behavior.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The issue is that your addChild function is referring to a copy of the removeChild function that is using an older copy of
state
. So when you click and removeChild runs, thestate
in that function is actually the state at the time the child was created.A couple ways to fix this:
In most cases you would want to construct the React elements (
<DynamicChild />
) at render time rather than storing them in state. For example, you can store just{id}
in state, then render{state.map(child => <DynamicChild id={child.id} key={child.id} removeChild={removeChild} />)}
instead of{state.map(child => child.child)}
below.You can rewrite removeChild to use the functional setState form, which always receives the latest state. That would look something like:
I verified that both of these would fix your issue.
This is a subtle issue. If you don’t have a full understanding based on what I just wrote, these blog posts may help – they cover some similar questions:
https://overreacted.io/how-are-function-components-different-from-classes/ https://overreacted.io/making-setinterval-declarative-with-react-hooks/
I’m going to close this, as it isn’t a bug in React. Next time, if you need help with your code, please see our community resources at: https://reactjs.org/community/support.html
Thanks @sophiebits! 🙇