-1

I am creating an app with React Native and I've used the map method to show two items in an array.
But the warning keeps showing:

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

Here is my code:

<View>
  {arrayType.map((item, key)=> {
    return(
      <>
        <View key={item + key} style={customStyle.main}>
          <Text>{item}</Text>
        </View>
      </>
    )
  })}
</View>

Why is it still showing that warning?

0stone0
  • 34,288
  • 4
  • 39
  • 64

2 Answers2

2

You are using a React.Fragment: <></>.

try this instead:

<View>
  {arrayType.map((item, key)=> {
    return(
      <View key={item + key} style={customStyle.main}>
        <Text>{item}</Text>
      </View>
    )
  })}
</View>
devpolo
  • 2,487
  • 3
  • 12
  • 28
1

The parent component should consist key prop when rendering list in loop.

so code should look like this.

<View>
   {arrayType.map((item, key) => {
      return(
         <View key={item + key} style={customStyle.main}>
            <Text>
               {item}
            </Text>
         </View>
      )})
   }
</View>
Vicky Ahuja
  • 906
  • 3
  • 9