-3

I have this json and I was wondering if there is a way to render a list without having to specify a key.

Here's what I tried:

import * as data from "./file.json";

const nameComponent = props => {
  const names = data.locale.names

  return(
    names.map = name => {
      <Item>
        <IdName>{name.id}</IdName>
        <FirstName>{name.firstName}</FirstName>
        <LastName>{name.lastName}</LastName>
      </Item>
  )
}

I can also just add the keys but I'm getting the following error:

Warning: Each child in a list should have a unique "key" prop.

Why am I getting this error even if I specified the keys?

Like this:

return(
  names.map = name => {
    <Item key={name.id}>
      <IdName>{name.id}</IdName>
      <FirstName>{name.firstName}</FirstName>
      <LastName>{name.lastName}</LastName>
    </Item>
)

EDIT: I have tried the answers below, none of them worked.

The json looks like this:

"items": [
  {
    "id": 1,
    "firstName": "Alan",
    "lastName": "Smith"
  },
  {
    "id": 2,
    "firstName": "John",
    "lastName": "Something"
  }
]
Cathy Tandey
  • 13
  • 1
  • 3
  • 2
    Does this answer your question? [Understanding unique keys for array children in React.js](https://stackoverflow.com/questions/28329382/understanding-unique-keys-for-array-children-in-react-js) – Raekh Void Feb 04 '20 at 11:11
  • 1
    your ids are unique ? – Nicolae Maties Feb 04 '20 at 11:12
  • I don't think you want to render without keys. They're used to help react perform better – evolutionxbox Feb 04 '20 at 11:14
  • you are returning a function !? Not a list. – Thomas Feb 04 '20 at 11:21
  • @Thomas I don't know about that, I have added the full code. Do you think this is a function? – Cathy Tandey Feb 04 '20 at 11:26
  • 1
    I think, `name => {...}` is a function, and `names.map = name => {...}` is assigning the function to `names.map`. No execution in sight. and `return a=b` is identical to `a=b; return b;` So, returning the function. – Thomas Feb 04 '20 at 11:29
  • Despite the question being closed as "duplicate", the problem was not in `key` (it was just a warning, not an error, after all), but in the `return` statement of `nameComponent` (and BTW your React components should be *PascalCased*, not *camelCased*). – mbojko Feb 04 '20 at 12:08

4 Answers4

0

Try this:

return(
      names.map( name => {
        <Item key={name.id + name}>
          <IdName>{name.id}</IdName>
          <FirstName>{name.firstName}</FirstName>
          <LastName>{name.lastName}</LastName>
        </Item>
    )}
Muhammad Haseeb
  • 1,269
  • 12
  • 22
0

Your syntax doesn't look right. Try

return(
  names.map(name => (
    <Item key={name.id}>the rest of the template...
  )
)
mbojko
  • 13,503
  • 1
  • 16
  • 26
0
return(
  names.map = (name, index) => {
    <Item key={index}>
      <IdName>{name.id}</IdName>
      <FirstName>{name.firstName}</FirstName>
      <LastName>{name.lastName}</LastName>
    </Item>
)
Vinay Kumar
  • 63
  • 1
  • 5
0

Keys need to be uniq, we should not use index as the key , I think your id of each profile is similar to index values like 1,2,3. So it is better user keys like below

return (names.map = name => {
<Item key={`NamesItem-${name.id}`}>
  <IdName>{name.id}</IdName>
  <FirstName>{name.firstName}</FirstName>
  <LastName>{name.lastName}</LastName>
</Item>)
Prasanth Ravi
  • 189
  • 2
  • 14