Am试图输出存储在数组中的值,但出现以下错误:"Line 40: 'found'
未定义no-undef
搜索关键字以了解有关每个错误的更多信息。”
代码:
class App extends Component {
constructor() {
super();
this.state = {
array1: [],
}
}
componentDidMount(props) {
this.setState({ array1: [5, 12, 8, 130, 44] })
}
found = (array1) => {
return array1.find((element) => {
return element > 10;
});
}
render() {
console.log(found(this.state.array1));
return (
<div className="App">
</div>
);
}
}
export default App;
该错误发生在以下行:
console.log(found(this.state.array1));
...请给我一些帮助吗?
答案 0 :(得分:3)
您必须首先从found
获得this
:
render() {
console.log(this.found(this.state.array1));
return (
<div className="App">
</div>
);
}
答案 1 :(得分:1)
为了在render中使用类函数,您需要像this.found()
class App extends Component {
constructor() {
super();
this.state = {
array1: [],
}
}
componentDidMount(props) {
this.setState({ array1: [5, 12, 8, 130, 44] })
}
found = (array1) => {
return array1.find((element) => {
return element > 10;
});
}
render() {
console.log(this.found(this.state.array1));
return (
<div className="App">
</div>
);
}
}
export default App;