I added a new object of contact and trying to show it in SectionList. But when I trying to put an object to array I'm getting an error: TypeError: undefined is not an object (evaluating 'n.data.length')
I used the instructions in this link to solve the problem. how to add a new object as a value to an array in the react state
constructor(props) {
super(props);
this.state = {
contacts : [],
newContactDialogVisible: false,
contactName : '',
contactPhone : ''
}
}
refreshContactsList = () => {
const newContact = {'name': this.state.contactName, 'phone': this.state.contactPhone};
Alert.alert(newContact.name + " " + newContact.phone); // Alert working and shows the right data
this.setState({ contacts: [...this.state.contacts, newContact] });
}
<SectionList
sections={this.state.contacts}
renderItem={({item, index, section}) => <Text key={index}>{item}</Text>}
/>