Languages
[Edit]
EN

React - why not to use key as custom property

0 points
Created by:
starcraf35
754

In this article, we would like to tell you why you shouldn't use key as a custom property in React.

Wrong use example

Edit

Below we present a situation where we assign key property to a component using map() method and we want to display the key property inside the component.

Practical example:

Solution

Edit

The solution is to create a new property and assign to it the same value as the key property.

In below example, we add new id property to the ShoppingListItem - id={item.id} and use {props.id} instead of {props.keys} inside the component.

Practical example:

Note:

The post was based on a question - Why can't I use key property in React?

References

Edit

Alternative titles

  1. React - why not to read key property
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

ReactJS

React - why not to use key as custom property
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join