1

I am trying to pass several arrays through a for loop in the render method and render that data with View. I keep getting an error in sentry.io "undefined is not an object (evaluating 't.number[o]')"

I am pretty new to this and any help would be appreciated!

render() {
  const userInfo = this.state.userInfo;
  const thisArray = this.state.thisArray;
  var myloop = [];
  for (let i = 0; i < 2; i++) {
    const totals = thisArray[i].number;
    const yourNumber = userInfo.number[i].enrolledNumber;
    myloop.push(
      <View key={i}>
        <Separator />
        <Text style={[styles.content, { fontSize: 20 }]}>
          {thisArray[i].name}
        </Text>
      </View>
    )
  }

  return (
    <SafeAreaView style={styles.container}>
      <ScrollView>
        <View style={styles.container}>
          <Text style={styles.txtColor}>Data</Text>
          {myloop}
        </View>
      </ScrollView>
    </SafeAreaView>
  );
}

I am getting the following error

undefined is not an object (evaluating 'thisArray[i].number')

Does anyone have thoughts?

4
  • add full component code Commented Feb 24, 2020 at 5:00
  • Check the thisArray.length. Your thisArray doesn't have an index 1 or 2. Commented Feb 24, 2020 at 5:24
  • It has an index at 0,1,2. Commented Feb 24, 2020 at 12:40
  • I've added the full component code. It works in development mode in Expo but fails in Expo Production mode. Commented Feb 24, 2020 at 12:45

1 Answer 1

1

I figured it out.

I passed the items through a flat list as such:

        <FlatList
            data={this.state.thisArray}
            ItemSeparatorComponent={this.FlatListItemSeparator}
            renderItem={this.renderItem}
            keyExtractor={item => item.name}
          />

then I called it through a renderItem method

renderItem = ({ item}) => {

    return (
        <View>
          <Text style={{ color: "white" }}>{item.name}</Text>
          <Text style={{ color: "white" }}>{item.id}</Text>
        </View>
)
}

Thanks guys,

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.