in Education by
I am new to React but I know main concept of unique key. However, I am getting a warning. Below I have an item component: class Item extends Component { state = {} render() { return ( {this.props.item.todo} ); } } And below is my items component and where i have unique keys: render() { const { items } = this.props; return ( items.map(item=> ) ); } With all of this I am getting warning! JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
As others have said you need to set the key on the top element, in your case being Fragment. But I would change the key value. I don't know what kind of data you have in your item.todo but just setting the key to the value of item.todo could be problematic. I will explain. A key should be unique only among its siblings The react.org document on lists and keys sums this up perfectly, so I won't explain in another way. It says this below. Keys used within arrays should be unique among their siblings. However, they don’t need to be globally unique. We can use the same keys when we produce two different arrays: (Note: it does not need to be meaningful data). A key should be stable This means that between renders the key should not change, so don't use Math.random() which some people think would be good to use. Why is the above important? In your data, if two items.todo are the same value then it would break the above. Your key would not be unique. Which could cause performance issues from unnecessary re-renders. I would recommend using a key with the value of the items.todo and the index of the map. This way if you do have the same value for items.todo adding an index would make the key unique. With this in mind I would write your snippet. render() { const { items } = this.props; return ( items.map((item, index) => ( )) ); } Here is link to the react.org documentation on list and keys and is a link to the react.org documentation regarding fragments. Both provide examples and useful information. They are a good read and I would highly recommend. Also something I noticed is that you're using React.Fragment but then you declare your class with just Component. Your could do what I'm assuming you've done for Component and destructure the Fragement. Something like below: import React, { Component, Fragment } from 'react'; So you're snippet is a bit more clean, like below: items.map((item, index) => ( ))

Related questions

0 votes
    I am looking to deny the use of IE for my reactjs application ... At the moment here is my App. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am looking to deny the use of IE for my reactjs application ... At the moment here is my App. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I'm working to set up Panda on an Amazon EC2 instance. I set up my account and tools last night and had no ... would be a great help! Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    I am trying to implement the Hateoas using spring boot. In my UserController class i have used the below ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    i am in hurry , i want to sleep early; but i have a test for tomorrow, i have not study what can ido Select the correct answer from above options...
asked Dec 29, 2021 in Education by JackTerrance
0 votes
    question – i have many keys and i am used for typing plz answer this question Select the correct answer from above options...
asked Dec 22, 2021 in Education by JackTerrance
0 votes
    Question- i have many keys and i am used for typing? plz answer this question Select the correct answer from above options...
asked Dec 22, 2021 in Education by JackTerrance
0 votes
    question – have many keys and i am used for typing? Answer plz Select the correct answer from above options...
asked Dec 22, 2021 in Education by JackTerrance
0 votes
    Heyy mates..Am an icse student can anyone tell me any importance for java please its urgent I have my exam ... please for icse java. Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    Identify me: I am metamerically segmented, blood sucking, ectoparasite. I have suckers. Who am I ... ,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
0 votes
    I have chitinous exoskeleton, I have four pairs of walking appendages. I can sting you. Who am ... ,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
0 votes
    Assume that I am using Tableau Desktop and have a live connection to Cloudera Hadoop data. I need to press ... the visualization every x minutes instead of pressing F5 everytime?...
asked Oct 30, 2020 in Technology by JackTerrance
0 votes
    I tried to implement Ngrx into my Angular Application, but sadly haven't been able to retrieve any data. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have the following table relationship in my database: Parent / \ Child1 Child2 \ / GrandChild I am ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    6. A primary key is a . of unique values. 7. Linking tables are created to facilitate DBMS uses the ... . among two tables. 8 Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
...