我添加了一个新的联系对象并尝试在 SectionList 中显示它。但是当我试图将一个对象放入数组时,我收到一个错误:TypeError: undefined is not an object (evalating 'n.data.length')
我使用此链接中的说明来解决问题。 如何在反应状态下将新对象作为值添加到数组中
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>}
/>