我正在尝试为从 API 获得的数据创建一个随机化器;但是我得到了这个“第 5 行:'props' 未定义 no-undef”错误并且看不出它有什么问题。
import React from "react";
class Random extends React.Component {
constructor() {
super(props);
this.state = {
breweries: []
};
}
componentDidMount() {
fetch("https://api.openbrewerydb.org/breweries")
.then(response => response.json())
.then((data) => {
this.setState({
breweries: data,
})
})
}
render() {
const brewery = this.state.breweries[Math.floor(Math.random()*this.state.breweries.length)];
return(
<div>{brewery.id}</div>
)
}
}
export default Random;
最佳答案
您缺少 constructor(props) {
。 props
首先需要作为参数被constructor
函数接收,然后只有你可以使用它。你错过了那部分。否则,您将在运行时收到 Uncaught ReferenceError: props is not defined
错误。
关于javascript - 第 5 行 : 'props' is not defined no-undef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53803189/