在React js中显示过滤器方法的警告

问题描述 投票:0回答:1

我收到一个警告,因为Expected在箭头函数array-callback-return结束时返回一个值

我正在使用axios和json服务器删除一个选定的帖子,删除每个帖子后,我只是返回新的数组

.then(() => {
        const remainingPosts = this.props.posts.filter((post) => {
          if (post.id !== this.state.loadedPost.id) {
            return post
          }
        })

如果我注释掉if条件我没有收到警告,否则我收到警告

reactjs ecmascript-6 eslint arrow-functions
1个回答
3
投票

您只需要在过滤器中提供true的错误条件

.then(() => {
    const remainingPosts = this.props.posts.filter((post) => {
      return post.id !== this.state.loadedPost.id

    })

甚至更简单

.then(() => {
    const remainingPosts = this.props.posts.filter((post) => (
        post.id !== this.state.loadedPost.id   
    ))
© www.soinside.com 2019 - 2024. All rights reserved.