{"id":74418,"date":"2020-01-17T12:43:35","date_gmt":"2020-01-17T07:13:35","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=74418"},"modified":"2024-04-12T14:23:28","modified_gmt":"2024-04-12T08:53:28","slug":"extracting-components-with-keys","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/extracting-components-with-keys\/","title":{"rendered":"Extracting Components with Keys"},"content":{"rendered":"<p>Consider a situation where you have created a separate component for list items and you are extracting list items from that component. In that case you will have to assign keys to the component you are returning from the iterator and not to the list items. That is you should assign keys to &lt;Component \/&gt; and not to &lt;li&gt; A good practice to avoid mistake is to keep in mind that anything you are returning from inside of map() function is needed to be assigned key.<\/p>\n<p>Keys only make sense in the context of the surrounding array. For example, if you extract a ListItem component, you should keep the key on the &lt;ListItem \/&gt; elements in the array rather than on the &lt;li&gt; element in the ListItem itself.<\/p>\n<p><u>Example: Incorrect Key Usage<\/u><\/p>\n<p>function ListItem(props) {<\/p>\n<p>const value = props.value;<\/p>\n<p>return (<\/p>\n<p>\/\/ Wrong! There is no need to specify the key here:<\/p>\n<p>&lt;li key={value.toString()}&gt;<\/p>\n<p>{value}<\/p>\n<p>&lt;\/li&gt;<\/p>\n<p>);<\/p>\n<p>}<\/p>\n<p>function NumberList(props) {<\/p>\n<p>const numbers = props.numbers;<\/p>\n<p>const listItems = numbers.map((number) =&gt;<\/p>\n<p>\/\/ Wrong! The key should have been specified here:<\/p>\n<p>&lt;ListItem value={number} \/&gt;<\/p>\n<p>);<\/p>\n<p>return (<\/p>\n<p>&lt;ul&gt;<\/p>\n<p>{listItems}<\/p>\n<p>&lt;\/ul&gt;<\/p>\n<p>);<\/p>\n<p>}<\/p>\n<p>const numbers = [1, 2, 3, 4, 5];<\/p>\n<p>ReactDOM.render(<\/p>\n<p>&lt;NumberList numbers={numbers} \/&gt;,<\/p>\n<p>document.getElementById(&#8216;root&#8217;)<\/p>\n<p>);<\/p>\n<p><u>Example: Correct Key Usage<\/u><\/p>\n<p>function ListItem(props) {<\/p>\n<p>\/\/ Correct! There is no need to specify the key here:<\/p>\n<p>return &lt;li&gt;{props.value}&lt;\/li&gt;;<\/p>\n<p>}<\/p>\n<p>function NumberList(props) {<\/p>\n<p>const numbers = props.numbers;<\/p>\n<p>const listItems = numbers.map((number) =&gt;<\/p>\n<p>\/\/ Correct! Key should be specified inside the array.<\/p>\n<p>&lt;ListItem key={number.toString()}<\/p>\n<p>value={number} \/&gt;<\/p>\n<p>);<\/p>\n<p>return (<\/p>\n<p>&lt;ul&gt;<\/p>\n<p>{listItems}<\/p>\n<p>&lt;\/ul&gt;<\/p>\n<p>);<\/p>\n<p>}<\/p>\n<p>const numbers = [1, 2, 3, 4, 5];<\/p>\n<p>ReactDOM.render(<\/p>\n<p>&lt;NumberList numbers={numbers} \/&gt;,<\/p>\n<p>document.getElementById(&#8216;root&#8217;)<\/p>\n<p>);<\/p>\n<p>A good rule of thumb is that elements inside the map() call need keys.<\/p>\n<p><strong>Keys Must Only Be Unique Among Siblings<\/strong><\/p>\n<p>Keys used within arrays should be unique among their siblings. However they don\u2019t need to be globally unique. We can use the same keys when we produce two different arrays:<\/p>\n<p>function Blog(props) {<\/p>\n<p>const sidebar = (<\/p>\n<p>&lt;ul&gt;<\/p>\n<p>{props.posts.map((post) =&gt;<\/p>\n<p>&lt;li key={post.id}&gt;<\/p>\n<p>{post.title}<\/p>\n<p>&lt;\/li&gt;<\/p>\n<p>)}<\/p>\n<p>&lt;\/ul&gt;<\/p>\n<p>);<\/p>\n<p>const content = props.posts.map((post) =&gt;<\/p>\n<p>&lt;div key={post.id}&gt;<\/p>\n<p>&lt;h3&gt;{post.title}&lt;\/h3&gt;<\/p>\n<p>&lt;p&gt;{post.content}&lt;\/p&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>);<\/p>\n<p>return (<\/p>\n<p>&lt;div&gt;<\/p>\n<p>{sidebar}<\/p>\n<p>&lt;hr \/&gt;<\/p>\n<p>{content}<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>);<\/p>\n<p>}<\/p>\n<p>const posts = [<\/p>\n<p>{id: 1, title: &#8216;Hello World&#8217;, content: &#8216;Welcome to learning React!&#8217;},<\/p>\n<p>{id: 2, title: &#8216;Installation&#8217;, content: &#8216;You can install React from npm.&#8217;}<\/p>\n<p>];<\/p>\n<p>ReactDOM.render(<\/p>\n<p>&lt;Blog posts={posts} \/&gt;,<\/p>\n<p>document.getElementById(&#8216;root&#8217;)<\/p>\n<p>);<\/p>\n<p>Keys serve as a hint to React but they don\u2019t get passed to your components. If you need the same value in your component, pass it explicitly as a prop with a different name:<\/p>\n<p>const content = posts.map((post) =&gt;<\/p>\n<p>&lt;Post<\/p>\n<p>key={post.id}<\/p>\n<p>id={post.id}<\/p>\n<p>title={post.title} \/&gt;<\/p>\n<p>);<\/p>\n<p>With the example above, the Post component can read props.id, but not props.key.<\/p>\n<p><strong>Embedding map() in JSX<\/strong><\/p>\n<p>In the examples above we declared a separate listItems variable and included it in JSX:<\/p>\n<p>function NumberList(props) {<\/p>\n<p>const numbers = props.numbers;<\/p>\n<p>const listItems = numbers.map((number) =&gt;<\/p>\n<p>&lt;ListItem key={number.toString()}<\/p>\n<p>value={number} \/&gt;<\/p>\n<p>);<\/p>\n<p>return (<\/p>\n<p>&lt;ul&gt;<\/p>\n<p>{listItems}<\/p>\n<p>&lt;\/ul&gt;<\/p>\n<p>);<\/p>\n<p>}<\/p>\n<p>JSX allows embedding any expression in curly braces so we could inline the map() result:<\/p>\n<p>function NumberList(props) {<\/p>\n<p>const numbers = props.numbers;<\/p>\n<p>return (<\/p>\n<p>&lt;ul&gt;<\/p>\n<p>{numbers.map((number) =&gt;<\/p>\n<p>&lt;ListItem key={number.toString()}<\/p>\n<p>value={number} \/&gt;<\/p>\n<p>)}<\/p>\n<p>&lt;\/ul&gt;<\/p>\n<p>);<\/p>\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Consider a situation where you have created a separate component for list items and you are extracting list items from that component. In that case you will have to assign keys to the component you are returning from the iterator and not to the list items. That is you should assign keys to &lt;Component \/&gt;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8528],"tags":[8613],"class_list":["post-74418","page","type-page","status-publish","hentry","category-reactjs","tag-extracting-components-with-keys"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Extracting Components with Keys - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/extracting-components-with-keys\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Extracting Components with Keys - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Consider a situation where you have created a separate component for list items and you are extracting list items from that component. In that case you will have to assign keys to the component you are returning from the iterator and not to the list items. That is you should assign keys to &lt;Component \/&gt;...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/extracting-components-with-keys\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:53:28+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/extracting-components-with-keys\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/extracting-components-with-keys\/\",\"name\":\"Extracting Components with Keys - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2020-01-17T07:13:35+00:00\",\"dateModified\":\"2024-04-12T08:53:28+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/extracting-components-with-keys\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/extracting-components-with-keys\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/extracting-components-with-keys\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Extracting Components with Keys\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Extracting Components with Keys - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/extracting-components-with-keys\/","og_locale":"en_US","og_type":"article","og_title":"Extracting Components with Keys - Tutorial","og_description":"Consider a situation where you have created a separate component for list items and you are extracting list items from that component. In that case you will have to assign keys to the component you are returning from the iterator and not to the list items. That is you should assign keys to &lt;Component \/&gt;...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/extracting-components-with-keys\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:53:28+00:00","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/extracting-components-with-keys\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/extracting-components-with-keys\/","name":"Extracting Components with Keys - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2020-01-17T07:13:35+00:00","dateModified":"2024-04-12T08:53:28+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/extracting-components-with-keys\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/extracting-components-with-keys\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/extracting-components-with-keys\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Extracting Components with Keys"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/74418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=74418"}],"version-history":[{"count":3,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/74418\/revisions"}],"predecessor-version":[{"id":75350,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/74418\/revisions\/75350"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=74418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=74418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=74418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}