Home > Enterprise >  What's the usage of "key" property inside navlink component in react router
What's the usage of "key" property inside navlink component in react router

Time:03-30

There seems no API includes the key property in the NavLink component, react-router. The usage would be like below (in a JSX map loop).

<NavLink key={index} to={`${url}/staff/${staff.id}`}>{staff.name}</NavLink>

My question is: what's the intention of designing such a property? Is there any other use case? Thank you.

CodePudding user response:

In React, when you create a list of elements, you need to identify these elements uniquely. So you use keys.

Using keys allows React to identify which items in the list are changed or updated.

https://reactjs.org/docs/lists-and-keys.html

CodePudding user response:

It's for when you are trying to map an array, but not related to the NavLink.

  • Related