Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

eslint: Missing "key" prop for element in iterator (react/jsx-key)

Tags:

reactjs

eslint

I am new to React, and I'm trying to output a table containing the information of users. But Eslint is keep giving me the following error:

[eslint] Missing "key" prop for element in iterator [react/jsx-key]

I am not sure if I did this properly, but I have assigned a unique ID number for each person in the user list, but not sure why the error is persistent.

So in my PeopleCard.js I have the following:

import React from "react"; import {   Card,   CardImg,   CardText,   CardBody,   CardTitle,   CardSubtitle,   Button } from "reactstrap"; import PropTypes from "prop-types";  class PeopleCard extends React.Component {   static propTypes = {     person: PropTypes.object,     id: PropTypes.number,     name: PropTypes.string,     company: PropTypes.string,     description: PropTypes.string   };   render() {     return (       <div>         <Card>           <CardImg             top             width="100%"             src="https://via.placeholder.com/318x180.png"             alt="Card image cap"           />           <CardBody>             <CardTitle>{this.props.person.name}</CardTitle>             <CardSubtitle>{this.props.person.company}</CardSubtitle>             <CardText>{this.props.person.description}</CardText>             <Button>Button</Button>           </CardBody>         </Card>       </div>     );   } }  export default PeopleCard; 

And in my MainArea.js, I have the following:

import React from "react"; import { Container, Row, Col } from "reactstrap"; import PeopleCard from "./PeopleCard";  class MainArea extends React.Component {   constructor() {     super();     this.state = {       people: [         {           id: 1,           name: "John",           company: "Some Company, Inc",           description: "Met at a party."         },         {           id: 2,           name: "Mary",           company: "Some Company, Inc",           description: "Met at a party."         },         {           id: 3,           name: "Jane",           company: "Some Company, Inc",           description: "Met at a party."         }       ]     };   }   render() {     let peopleCard = this.state.people.map(person => {       return (         <Col sm="4">           <PeopleCard key={person.id} person={person} />         </Col>       );     });     return (       <Container fluid>         <Row>{peopleCard}</Row>       </Container>     );   } }  export default MainArea; 

The following line is throwing the error, and I cannot figure out why:

<Col sm="4">    <PeopleCard key={person.id} person={person} /> </Col> 

How can I prevent this error from appearing?

like image 329
termlim Avatar asked Jan 28 '19 11:01

termlim


People also ask

How do you iterate key value pairs in react?

Use the Object. keys() method to get an array of the object's keys. Use the map() method to iterate over the array of keys.

How add key value pair in array react?

You create your object (including its id property) and then you add it to the result array. You add the id property to the last object in your result array (since you talk about "newly created data" I am assuming it is always the last item).

Do not use array index in keys react?

Disallow usage of Array index in keys ( react/no-array-index-key ) Warn if an element uses an Array index in its key . The key is used by React to identify which items have changed, are added, or are removed and should be stable. It's a bad idea to use the array index since it doesn't uniquely identify your elements.

How do you get data from array of objects in react JS?

How we will render an Array of Objects? We will use the map function to access each object from the array. The map() method in JavaScript creates an array by calling a specific function on each element present in the parent array. It is a non-mutating method.


1 Answers

You should put the key on the outer element:

const peopleCard = this.state.people.map(person => (   <Col key={person.id} sm="4">     <PeopleCard person={person} />   </Col> )); 
like image 146
Dominic Avatar answered Oct 04 '22 02:10

Dominic