null不是一个对象(评估'this.state.noteArray') - 反应原生

问题描述:

为什么即时通讯接收到这个错误,但我已经添加了我的类的构造函数。 错误只是出现在我执行Android运行开始和即时通讯获取此错误。null不是一个对象(评估'this.state.noteArray') - 反应原生

null不是(评估 'this.state.noteArray')的对象

export default class testPoject extends Component { 
constructor(props) { 
    super(props); 
    this.state = {noteArray: []} 
} 

render() { 

    let notes = this.state.noteArray.map((val, key) => { 
     return <Note key={key} keyval={key} val={val} deleteMethod={() => this.deleteNote(key)} /> 
    }); 

    return (
    <View style={styles.container}> 

      <View style={styles.header}> 
       <Text style={styles.headerText}>TODO LIST </Text> 
      </View> 

      <ScrollView style={styles.scrollConainer}> 

      </ScrollView> 

      <View style={styles.footer}> 

       <TouchableOpacity style={styles.addButton}> 
        <Text style={styles.addButtonText}> 
         + 
        </Text> 
       </TouchableOpacity> 

       <TextInput style={styles.noteInputText} 
        placeholder="> Note" 
        placeholderTextColor="#FFF" 
        underlineColorAndroid="transparent" 
        numberOfLines = {3} 
       /> 

      </View> 

    </View> 
    ); 
} 

}

你为什么要传递一个VAL和关键是你的地图功能? noteArray是一个数组,不是具有键值对的对象。 Ether将noteArray写成noteArray:{}或更改地图函数以使用单个元素。

那么, 做一件事情,它会工作。添加一个构造函数并在该构造函数中声明该状态。

constructor() { 
super(); 
this.state = { 
    noteArray:[{ 'note': 'Bingo'}], 
    noteText:'', 
} 

}