React native app shows an error saying that key will be undefined even after providing a key

Shashika

In my RN application, I have the following array.

const PDFItems = [
  { id: 1001, name: 'APPLICATION FORM.PDF', size: '2.77 MB' },
  { id: 1002, name: 'BENEFIT ILLUSTRATION.PDF', size: '368 KB' },
  { id: 1003, name: 'PRODUCT SUMMARY.PDF', size: '2.02 MB' },
  { id: 1004, name: 'TERMS AND CONDITIONS.PDF', size: '269 KB' },
];

I created the following function to render items.

renderPDFItems = () => {
    return PDFItems.map(item => (
      <ListItem
        key={item.id.toString()}
        icon={<Download />}
        title={item.name}
        label={item.size}
        onPress={() => {}}
      />
    ));
  }

This is my ListItem component.

import React, { Component } from 'react';
import { StyleSheet, TouchableOpacity, View } from 'react-native';
import colors from 'res/colors';
import SinglifeText from './text';

interface IProps {
  title: string;
  label: string;
  icon: Object;
  key: string;
  onPress?: Function;
}

class ListItem extends Component<IProps> {
  render() {
    const { title, label, icon, key, onPress } = this.props;

    return (
      <TouchableOpacity onPress={onPress} key={key}>
        <View style={styles.itemContainer}>
          <View style={styles.titleContainer}>
            <View style={styles.icon}>{icon}</View>
            <SinglifeText type={SinglifeText.Types.BUTTON_LBL} label={title} />
          </View>
          <SinglifeText type={SinglifeText.Types.BODY_SMALL} label={label} />
        </View>
      </TouchableOpacity>
    );
  }
}

const styles = StyleSheet.create({
  itemContainer: {
    borderColor: colors.gray,
    borderRadius: 4,
    borderWidth: 0.5,
    padding: 14,
    paddingVertical: 24,
    flexDirection: 'row',
    alignContent: 'center',
    alignItems: 'center',
    marginBottom: 10,
  },
  titleContainer: {
    flex: 1,
    flexDirection: 'row',
    alignContent: 'center',
    alignItems: 'center',
  },
  icon: {
    marginRight: 10,
  },
});

export default ListItem;

When I run the app, it shows a warning saying that, key is not a prop. Trying to access it will result in undefined being returned. If you need to access the same value within the child component, you should pass it as a different prop

What am I doing wrong here? My keys are unique and still gives this error.

Cool Guy

Just dont name the prop key. Something like id has the same syntactic meaning.

renderPDFItems = () => {
    return PDFItems.map(item => (
      <ListItem
        id={item.id.toString()}
        icon={<Download />}
        title={item.name}
        label={item.size}
        onPress={() => {}}
      />
    ));
  }

ListItem.js

class ListItem extends Component<IProps> {
  render() {
    const { title, label, icon, id, onPress } = this.props;

    return (
      <TouchableOpacity onPress={onPress} key={id}>
        <View style={styles.itemContainer}>
          <View style={styles.titleContainer}>
            <View style={styles.icon}>{icon}</View>
            <SinglifeText type={SinglifeText.Types.BUTTON_LBL} label={title} />
          </View>
          <SinglifeText type={SinglifeText.Types.BODY_SMALL} label={label} />
        </View>
      </TouchableOpacity>
    );
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

gMarker.key undefined and it is REQUIRED!! error in angular and google maps app

Why does the keyboard disappear after each key pressed in my react native app?

Check if nested key exists even if undefined

Undefined index even though the key is declared

Using React Native to Speak to AWS - Get item: key is federatedInfo with options undefined error

Got duplicate key error dup key: { : undefined }

key is undefined react

Masonite throw Invalid secret key error, even after secret key is created

React-Native FlatList- Warning: Each child in an array or iterator should have a unique "key" prop, even after giving key={index}

React native modal error evaluating _this5.state.error[key]

Providing IdentityServer4 with a key on Linux App Service

(React Native) How to uploading App Signing Key to Google Play Console?

React Native FlatList key

Panel not shows after key pressed

React-native - error about unique key

React Native: console.error: "No reducer provide for key "auth""

React key not working even after giving key to the returned value

The problems be Cannot read property 'key' of undefined in react native?

Why react shows TypeError: undefined has no properties even after passing it properties?

Key Error even though key IS in dictionary?

React Native key in AsyncStorage

Even though I check there exsist a data, the error message shows "Cannot read properties of undefined" in React

Showing unique value error even after giving the key value in map?

PHP CRUD Loading blank page or shows error undefined array key

Each child in a list should have a unique "key" prop, I am getting this error in my react native app

key should be unique react native error while mapping data array?

React key item list error, even after I added the required Key

Error message even after installing react-native on a clean project

Javascript saying a key does not exist in an object even though it does