【问题标题】:ES6 Fat arrow in react this.setState ESLint error [duplicate]ES6 Fat箭头反应this.setState ESLint错误[重复]
【发布时间】:2018-07-24 22:23:54
【问题描述】:

我在反应中有一个小代码

this.setState((prevState) => {
  return { userAnnotations: [...prevState.userAnnotations, selText] };
});

当 ES Lint 给出错误 [eslint] Unexpected block statement surrounding arrow body; parenthesize the returned value and move it immediately after the=>. (arrow-body-style)

时,我如何将其转换为单行

我试图删除 return 周围的花括号,但随后又出现了一些错误。

请帮忙。

【问题讨论】:

  • this.setState(prevState => ({ userAnnotations: [...prevState.userAnnotations, selText] }));

标签: javascript reactjs ecmascript-6


【解决方案1】:

你需要做的就是摆脱return

    this.setState(prevState => ({ userAnnotations: 
          [...prevState.userAnnotations, selText]
    }))

【讨论】:

  • this.setState(prevState => ({ userAnnotations: [...prevState.userAnnotations, selText], }));
  • 也需要去掉外括号
  • @raju 是的,那是return以外的事情之一
猜你喜欢
  • 2018-06-15
  • 1970-01-01
  • 2016-03-25
  • 2017-05-18
  • 2016-11-08
  • 2018-08-02
  • 2018-07-18
  • 2017-04-26
  • 1970-01-01
相关资源
最近更新 更多