简体   繁体   English

在评估this.state时,null不是对象。

[英]null is not an object when evaluating this.state

I have a list of items and I would like only one of them to be highlighted at any given time. 我有一个项目清单,我希望在任何给定时间仅突出其中的一个。 I wrote the following code: 我写了以下代码:

constructor(props) {
    super(props);
    this.setState({indexHighlighted: 0});
}

selectItem = (i) => {
    this.setState({indexHighlighted: i});
}

checkIfHighlighted = (i) => {
    i == this.state.indexHighlighted;
}

render() {
    return ( 
        <FlatList
            data={myData} 
            renderItem={ ({item}) =>
                <TouchableHighlight onPress={this.selectItem(item["indexNr"])}>
                    <ShoppingListItem
                        key={item["indexNr"]} 
                        highlighted = {this.checkIfHighlighted(item["indexNr"])}
                        orderInList={item["indexNr"]} />
                </TouchableHighlight>
            }
            keyExtractor = {(item, index) => index}
        /> 
    );
}

When I run this code, I get null is not an object (evaluating '_this.state.indexHighlighted') . 当我运行此代码时,我得到的null is not an object (evaluating '_this.state.indexHighlighted')

Why would this.state be null ? 为什么this.statenull Can it be null if I set it from the constructor? 如果我通过构造函数设置它,可以为null吗?

If yes, then how can I initialise it in due time such that error doesn't appear? 如果是,那么如何在适当的时候初始化它,从而不会出现错误?

You have 2 syntax mistake. 您有2个语法错误。

One is that you are not settings state correctly. 一是您没有正确设置状态。 Second you are executing the function rather than passing it as a property. 其次,您正在执行函数,而不是将其作为属性传递。

constructor(props) {
    super(props);
    this.state = {indexHighlighted: 0};
}

 <TouchableHighlight onPress={() => this.selectItem(item["indexNr"])}>
   // ...
 <TouchableHighlight />

you must bind your function in the constructor and if you want send an argument to your function you should define an outer function in jsx. 您必须将函数绑定到constructor函数中,如果要向函数发送参数,则应在jsx中定义一个外部函数。 Also you shoul use return in checkIfHighlighted fucn. 您还应该在checkIfHighlighted fucn中使用return。

constructor(props) {
    super(props);
     this.state = {
       indexHighlighted: 0
     };
     this.checkIfHighlighted = this.checkIfHighlighted.bind(this);
     this.selectItem = this.selectItem.bind(this);
        }

...
checkIfHighlighted (){
  return i == this.state.indexHighlighted;

}
...

jsx : jsx:

<TouchableHighlight onPress={()=>{this.selectItem(item["indexNr"])}}>
                    <ShoppingListItem
                        key={item["indexNr"]} 
                        highlighted = {this.checkIfHighlighted(item["indexNr"])}
                        orderInList={item["indexNr"]} />
                </TouchableHighlight>

In constructor you should use this syntax: 在构造函数中,应使用以下语法:

    constructor(props) {
    super(props);
     this.state = {
       indexHighlighted: 0
     };
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM