question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: How to remove dynamic children from Parent State?

See original GitHub issue

I’m using react hook useContext. I have two identical components (siblings), each that use the same context that is a list.

Scenario

  1. The first sibling is created, calls useContext, and then pushes something into the list.
  2. 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:closed
  • Created 4 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
sophiebitscommented, Feb 22, 2020

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, the state in that function is actually the state at the time the child was created.

A couple ways to fix this:

  1. 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.

  2. You can rewrite removeChild to use the functional setState form, which always receives the latest state. That would look something like:

const removeChild = id => {
  setState(oldState => {
    return oldState.filter(child => child.id !== id);
  });
};

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

0reactions
IanChokcommented, Feb 23, 2020

Thanks @sophiebits! 🙇

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found