尝试从数组输出值,但收到错误:array1 未定义 no-undef
代码片段:
constructor() {
super();
this.state = {
array1: [],
}
}
//代码片段componentDidMount:
componentDidMount(props) {
this.setState({array1: [5, 12, 8, 130, 44] })
}
//代码片段,函数:
found = this.state.array1.find((element) => {
return element > 10;
});
代码片段:
render(){
console.log(found);
}
页面在箭头功能中出现错误,请问我可以寻求帮助吗?
最佳答案
您的构造函数应更改为
constructor() {
super();
this.state = {
array1: []
}
}
您尚未将 array1
定义为状态。
更新(根据评论更新答案)
constructor() {
super();
this.state = {
array1: []
}
this.found = this.found.bind(this);
}
found() {
return this.state.array1.find((element) => {
return element > 10;
});
}
render(){
console.log(this.found());
}
关于javascript - React JS,尝试从数组获取第一个值时未定义错误 no-undef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51026815/