Let's say you are dynamically creating children of an array and in this case you have to tell React that each child is different for React to know about it. Assigning 'key' property helps the React to keep the state and identity of each element maintained through multiple renders.
For Example, I have a tags array and I am creating a span element for each tag in teh array and adding it to another array as shown below.
tags.map(tag => { tagSpan = <span className="tag">{tag}</span>; tagsArray.push(tagSpan); });
This code will likely throw an error "Each child in an array or iterator should have a unique key prop" because we have not given the key property to the span element and all the span has the same className i.e. "tag".
To fix we have to change our code as shown below
tags.map(tag => { tagSpan = <span key={tag} className="tag">{tag}</span>; tagsArray.push(tagSpan); });
Please like and share the CodingDefined.com blog if you find it useful and helpful.
Related articles
- How to create Simple Twitter Bot in Node.js
- Legacy Browser Support for using Babel
- Getting Started with Node.js Koa 2 Framework
- Web Scraping Libraries for Node.js Developers
- How to Create Simple RSS Reader in Ionic Application
- Simple RESTful API in Nodejs
- How to Get Latest Tweets of a particular HashTag using Nodejs
- How to Intercept HTTP requests in Node.js
- Node for Android
- Capture Screen of Web Pages through URL in Nodejs
I do agree with ɑll the ideas you've presented to yoᥙr post.
ReplyDeleteТhey are very convincing and will definitеly woгk.
Nonetheless, the posts ɑre too short for starters. May jᥙst you
pleaze extend them a llittle from next time?Thɑnks for the post.