Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make key unique #2

Open
LawJolla opened this issue Jan 24, 2019 · 0 comments
Open

Make key unique #2

LawJolla opened this issue Jan 24, 2019 · 0 comments

Comments

@LawJolla
Copy link

const results = data.fetchRecipesFromSpoonacularInBulk.map((recipe,i) => <UserDisplayedRecipes props={recipe} key={i}>{recipe.title}</UserDisplayedRecipes>);

You'll want to make all React keys unique on the data and not the index. For instance, say you have a collection (array of objects) with 3 objects. Those objects give an accordion menu with states opened and closed.

const ourCollection = [{ name: "Fluffykins"}, {name: "Fido"}, {name: "Carl"}]

In the local component React state, Fluffykins is closed, Fido is open, and Carl is closed.

Now suppose that new data comes in and Fluffykins is removed. What happens to the Accordion?

Expected: Fido open, Carl closed.
What happens when you use indexes for keys: Fido closed, Carl open.

React knows that 1 and 2's keys are the same (0 and 1), so it will simply pass the data into those existing elements.

Does that make sense? There are a host of other bugs that can happen too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant