I am currently learning RN and I right now I am trying to display data dynamically from a JSON file I created locally. What I would like to do is display the list of characters and give the user the chance to press on a particular character's button to take them to their profile screen. However, I keep getting a message saying "Cannot convert undefined or null to object". Can someone explain to me why I am getting that? How can I fix that? Any help or leads are appreciated.
This is the JSON file I created:
//Characters.js
const characters = [
{ id: "1", name: "Homer Simpson", occupation: "Nuclear Safety Inspector" },
{ id: "2", name: "Marge Simpson", occupation: "Stay-at-home mom" },
{ id: "3", name: "Bart Simpson", occupation: "Student" },
{ id: "4", name: "Lisa Simpson", occupation: "Student" },
{ id: "5", name: "Maggie Simpson", occupation: "Baby" },
{ id: "6", name: "Barney Gumble", occupation: "Homer's BFF" },
{ id: "7", name: "Kent Brockman", occupation: "TV Anchor" },
{ id: "8", name: "Mr. Burns", occupation: "Nuclear Plant Owner" },
{ id: "9", name: "Ralph Wiggum", occupation: "Police Officer" },
{ id: "10", name: "Otto Mann", occupation: "School Bus Driver" },
{ id: "11", name: "Santa's Little Helper", occupation: "Family Pet" },
{ id: "12", name: "Scratchy", occupation: "Cat" }
];
export default characters;
This is how I am trying to bring it in:
import React, { Component } from "react";
import { Text, View } from "react-native";
import { withNavigation } from "react-navigation";
import {
createStackNavigator,
createAppContainer,
createBottomTabNavigator
} from "react-navigation";
import characters from "../Data/Characters";
class CharacterDirectory extends React.Component {
static navigationOptions = {
title: "The Simpsons",
headerStyle: {
backgroundColor: "#53b4e6"
},
headerTintColor: "#f6c945",
headerTitleStyle: {
fontWeight: "bold"
},
};
render() {
const { navigation } = this.props;
const type = navigation.getParam('type');
const typeData = characters[type];
return (
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
{Object.keys(typeData).map(id => (
<Button
title={'Go to${typeData[id].name}'}
onPress={() =>
this.props.navigation.push('Details', {
type,
item: typeData[id],
})}
/>
))}
</View>
)
}
}
export default CharacterDirectory;