react - ReactJS Keys - react js - reactjs
What is Keys in ReactJS?
- Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity
- React keys are useful when working with dynamically created components or when your lists are altered by users.
- Setting the key value will keep your components uniquely identified after the change.
Using Keys
- Let's dynamically create Content elements with unique index (i).
- The mapfunction will create three elements from our data array. Since key value needs to be unique for every element, we will assign i as a key for each created element.
Article tag : react , react native , react js tutorial , create react app , react tutorial , learn react
App.jsx
Article tag : react , react native , react js tutorial , create react app , react tutorial , learn react
main.js
Article tag : react , react native , react js tutorial , create react app , react tutorial , learn react
Output
- You will get the following result for the Key values of each element.
learn reactjs tutorial -
reactjs keys
- reactjs example - react tutorial - reactjs - react
- If we add or remove some elements in the future or change the order of the dynamically created elements, React will use the key values to keep track of each element.