【问题标题】:null is not an object (evaluating 'this.state.noteArray') - React Nativenull 不是对象(评估'this.state.noteArray') - React Native
【发布时间】:2017-04-13 22:06:17
【问题描述】:

为什么我收到这个错误,但我已经在我的班级添加了构造函数。 当我执行 android-run start 并收到此错误时,该错误才会出现。

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>
    );
}

}

【问题讨论】:

    标签: javascript android reactjs react-native react-redux


    【解决方案1】:

    嗯, 做一件事,它就会奏效。添加一个构造函数并在该构造函数中声明状态。

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

    }

    【讨论】:

      【解决方案2】:

      为什么要向 map 函数传递 val 和 key? noteArray 是一个数组,而不是具有键值对的对象。以太将 noteArray 写为 noteArray: {} 或更改您的 map 函数以使用单个元素。

      【讨论】:

        猜你喜欢
        • 2021-08-30
        • 2017-09-19
        • 2021-10-27
        • 2019-10-18
        • 1970-01-01
        • 2016-10-14
        • 2022-01-23
        • 2021-07-05
        • 2017-11-05
        相关资源
        最近更新 更多