![](/img/trans.png)
[英]Getting the error that Expected an assignment or function call and instead saw an expression no-unused-expressions?
[英]ReactJs function call and instead saw an expression no-unused-expressions
我正在尝试在reactjs上准备restfulpi。 但是因为我是ReactJS的新手,而且我的英语不太流利,所以遇到了一个问题。 我申请的目的是列出出版商的书籍。 您可以从下面访问我的代码和错误。 如果您能帮助我,我可以做到。 谢谢。
错误:
第21行:预期分配或函数调用,而是看到一个表达式no-unused-expressions
我的代码:
`` `
class App extends Component {
state = {
books:[]
}
componentWillMount(){
axios.get('http://localhost:3000/books').then(( response)=>{
this.setState({
books: response.data
})
});
}``
`` `
render() {
let books = this.state.books.map((book) =>
{
return
(
<tr key={book.id}>
<td>{book.id}</td>
<td>{book.title}</td>
<td>{book.rating}</td>
<td>
<Button color="success" size="sm" className="mr-2">Edit </Button>
<Button color="danger" size="sm">Sil</Button>
</td>
</tr>
)
});``
`` `
return (
<div className="App container">
<h1>Book List</h1><br></br>
<Table>
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th>Rating</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{books}
</tbody>
</Table>
</div>
);``
您在render
方法中缺少return
语句。
render() {
....
...
return (
<tbody>
{books}
</tbody>
)
}
尝试这个:
render() {
const books = this.state.books.map(book => {
return (
<tr key={book.id}>
<td>{book.id}</td>
<td>{book.title}</td>
<td>{book.rating}</td>
<td>
<Button color="success" size="sm" className="mr-2">
Edit{' '}
</Button>
<Button color="danger" size="sm">
Sil
</Button>
</td>
</tr>
);
});
return <tbody>{books}</tbody>;
}
根据此答案,这是渲染功能中的常见错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.