Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React JS,错误未定义。尝试从数组中获取第一个值时没有未定义_Javascript_Reactjs - Fatal编程技术网

Javascript React JS,错误未定义。尝试从数组中获取第一个值时没有未定义

Javascript React JS,错误未定义。尝试从数组中获取第一个值时没有未定义,javascript,reactjs,Javascript,Reactjs,我试图从数组中输出一个值,但出现错误:array1未定义no undef 代码片段: constructor() { super(); this.state = { array1: [], } } componentDidMount(props) { this.setState({array1: [5, 12, 8, 130, 44] }) } found = this.state.array1

我试图从数组中输出一个值,但出现错误:array1未定义no undef

代码片段

 constructor() {
        super();
        this.state = {  
          array1: [],
        }
      }
componentDidMount(props) {
    this.setState({array1: [5, 12, 8, 130, 44] })
  }
found = this.state.array1.find((element) => {
    return element > 10;
  }); 
//代码段组件didmount

 constructor() {
        super();
        this.state = {  
          array1: [],
        }
      }
componentDidMount(props) {
    this.setState({array1: [5, 12, 8, 130, 44] })
  }
found = this.state.array1.find((element) => {
    return element > 10;
  }); 
//代码段,函数

 constructor() {
        super();
        this.state = {  
          array1: [],
        }
      }
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: []
    }
}
您尚未将
数组1
定义为状态

更新(根据评论更新答案)


没有构造函数,没有绑定函数(由于类字段),下面是呈现(而不是console.log)数组元素的方式

类Foo扩展了React.Component{
状态={array:[]};
componentDidMount(){
this.setState({array:[5,12,8130,44]})
}
//有点作弊。
找到=()=>
此为.state.array
.map(el=>el<10?未定义:{el}

); //也许是更好的? foundAlternative=()=> 此为.state.array .过滤器(el=>el>10) .map(el=>{el}

); render(){ 返回( {this.found()} ) } } ReactDOM.render( , document.getElementById(“根”) );


Vikas-我在发布问题时意外删除了“this.state=”行;在我的函数中,我将“this.state”添加到“array1”,它现在反映以下内容:this.state.array1.find…但我得到以下错误:TypeError:无法读取未定义的属性“array1”。如果您有任何其他建议,请告诉我…谢谢
array1
被定义为一种状态,唯一的问题是
不在其上下文范围内。解决这个问题的一种方法是
found=(array1)=>{return array1.find((element)=>{return element>10;});}render(){console.log(find(this.state.array1));}
P.S-我还没有测试代码…我得到的错误“found”没有定义没有定义没有定义没有未定义。。。在console.log(找到(this.state.array1))。。。如果我删除发现,它返回什么…请让我知道,如果你有任何其他建议。谢谢,应该是
console.log(this.found(this.state.array1))