Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React native each key should have a unique

Why I get this error Message?

Warning: Each child in a list should have a unique "key" prop.
import * as React from 'react';
import { StyleSheet, Text, View, TouchableOpacity, Image, Dimensions, Animated, FlatList } from 'react-native';

const StoryProfile = () => {
  const dataStory = [
    { 
      type: 'NORMAL',
      item: {
        id: '1',
        image: 'https://picsum.photos/200',
      }
    },
    { 
      type: 'NORMAL',
      item: {
        id: '2',
        image: 'https://picsum.photos/200',
      }
    },
    { 
      type: 'NORMAL',
      item: {
        id: '3',
        image: 'https://picsum.photos/200',
      }
    },
    { 
      type: 'NORMAL',
      item: {
        id: '4',
        image: 'https://picsum.photos/200',
      }
    },
    { 
      type: 'NORMAL',
      item: {
        id: '5',
        image: 'https://picsum.photos/200',
      }
    },
    { 
      type: 'NORMAL',
      item: {
        id: '6',
        image: 'https://picsum.photos/200',
      }
    },
  ];

  const Item = React.memo(({ image }) => {
    return (
      <View>
        <Image source={{uri: image}} height={48} width={48} resizeMode="contain" />
      </View>
    )
  });

  const renderItem = ({ item }) => {
    return (
      <Item image={item.image} />
    )
  };

  return (
    <View style={styles.container}>
      <FlatList 
        data={dataStory}
        keyExtractor={item => item.id}
        key={Math.random(1000)}
        renderItem={renderItem}
      />
    </View>
  )
};

const styles = StyleSheet.create({
  container: {
    flex: 1
  }
})

export default StoryProfile;

I have tried a key prop but thats not working. What I am doing wrong ?
………………………………………………………………………………………………………………………………………………………………………………………………

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

  • No need to have a key prop on a FlatList component:
<FlatList 
        data={dataStory}
        keyExtractor={item => item.id}
        key={Math.random(1000)} {/* No need to have a key attribute on FlatList */}
        renderItem={renderItem}
      />
  • When you add a keyExtractor={item => item.id}, it will search the id on each object of your dataStory, which is undefined in your case, so the key in your case is the same undefined.

So, the first solution is:

  • Instead of having:
{ 
  type: 'NORMAL',
  item: {
    id: '1',
    image: 'https://picsum.photos/200',
  }
}

You should have this:

{ 
  id: '1',
  type: 'NORMAL',
  item: {
    image: 'https://picsum.photos/200',
  }
}
  • The second solution is to change the keyExtractor to be:
keyExtractor={item => item.item.id}

or:

keyExtractor={({item}) => item.id}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading