Skip to content

Listbox render props clearing when parent component state changes #854

Discussion options

You must be logged in to vote

Hey! Thank you for your question!
Much appreciated! 🙏

The issue is that your const items inside the ParentComponent will be re-created every render, therefore all the objects inside the array will also be recreated every render and therefore items.includes(knownItemInTheComponent) will always be false.

You can apply a few fixes here:

  • Make sure your items array is stable over multiple renders. You can use a useState or useRef
  • Another solution is to use the item.id as the value instead of the item itself. Because your id is a primitive and therefore will be stable over time.

With "stable" I mean the reference:

1 === 1 // true
{ id: 1 } === { id: 1 } // false

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@AustinKostreba
Comment options

Answer selected by AustinKostreba
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants