【问题标题】:ReactJs function call and instead saw an expression no-unused-expressionsReactJs 函数调用,而是看到一个表达式 no-unused-expressions
【发布时间】:2019-08-20 20:11:27
【问题描述】:

我正在尝试在 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>&nbsp;
            <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>
);``

【问题讨论】:

  • 第 21 行到底是哪一行?

标签: javascript reactjs frontend


【解决方案1】:

试试这个:

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>
          &nbsp;
          <Button color="danger" size="sm">
            Sil
          </Button>
        </td>
      </tr>
    );
  });

  return <tbody>{books}</tbody>;
}

根据this answer,这是渲染函数中的常见错误。

【讨论】:

  • 我按照你说的修好了,但还是有问题。
  • 尝试复制我的渲染方法并用它替换你的。在您的原始帖子中,您有很多语法错误,即从渲染方法返回,但我认为这是由于复制粘贴部分。如果您使用整个 React 组件(整个文件)更新帖子,我们可能会捕获其他内容。另外,您的编辑器中的第 21 行是哪一行?
【解决方案2】:

您的render 方法中缺少return 语句。

render() {
     ....
     ...

   return (
      <tbody>
        {books}
      </tbody>
   )
}

【讨论】:

  • 我按照你给我的方式改变了它。但同样的问题摆在眼前
  • 你只需要正确关闭渲染方法
猜你喜欢
  • 2020-04-21
  • 2020-11-20
  • 2021-11-17
  • 2021-08-23
  • 1970-01-01
  • 2019-08-15
  • 2020-12-10
  • 2019-11-23
  • 1970-01-01
相关资源
最近更新 更多