我已经尝试使这项工作工作几个小时了。每当我运行代码时,它都会不断给我一个“第28行:期望赋值或函数调用,而看到一个表达式”错误。
import React from "react";
class Brewery extends React.Component {
constructor(props) {
super(props);
this.state = {
breweries: []
};
}
componentDidMount() {
fetch("https://api.openbrewerydb.org/breweries")
.then(response => response.json())
.then((data) => {
this.setState({
breweries: data,
})
})
}
render() {
const { breweries } = this.state;
return(
<ul>
{breweries.map((brewery) => {
<li key={brewery.id}>
{brewery.name}
</li>
})}
</ul>
)
}
}
export default Brewery;
答案 0 :(得分:1)
您的.map
方法中可能未返回任何内容。
<ul>
{breweries.map(brewery => {
<li key={brewery.id}>{brewery.name}</li>;
})}
</ul>
在这里,您正在使用带有正文块的粗箭头功能,但未使用return
,因此将其添加:
<ul>
{breweries.map(brewery => {
return <li key={brewery.id}>{brewery.name}</li>;
})}
</ul>
或使用implicit return:
<ul>
{breweries.map(brewery => (
<li key={brewery.id}>{brewery.name}</li>
))}
</ul>