Skip to content
Advertisement

Array of components doesn’t update when props change

Codesandbox link

I have two components, a Parent and a Child. The Parent components keeps a list of Children in state and renders it using the map method.

The parent:

JavaScript

The Child:

JavaScript

Clicking the “add child” button adds an child to the children array.

Clicking the “increment” button updates the counter on the parent, but the counter in the children does not change.

Is there a way to re-render the children when the counter increments? (while preferably keeping the children in an array)

Codesandbox link

Advertisement

Answer

JavaScript

Don’t put elements into state. It makes it really easy to have bugs like the one you’re having right now. You’ve effectively “locked in” what the props to the child are, so changes to the counter will not take effect.

Instead, just store the minimal data that’s needed to create the elements, and then create those elements during rendering. In your case, i think you just need a number saying how many children to render. Conveniently, you already have that in childKey, so i recommend:

JavaScript

Maybe childKey could be renamed to childCount.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement