componentDidMount内部的函数上没有未使用的表达式

时间:2018-07-31 19:02:00

标签: javascript reactjs eslint

如何修复或禁用ESLint以忽略

[eslint] Expected an assignment or function call and instead saw an expression. (no-unused-expressions)

对此

authUser
  ? this.setState(() => ({ authUser }))
  : this.setState(() => ({ authUser: null }));

虽然我了解错误的原因,但可以忽略它-因为它正在做它打算做的所有事情。

是否有一种方法只能从此文档中删除此错误,而不能从全局中删除此错误?或者,也许有一种更好的方法让我重写此代码以避免在获得相同结果的同时出现错误?

完整组件

const withAuthentication = Component =>
  class WithAuthentication extends React.Component {
    constructor(props) {
      super(props);

      this.state = {
        authUser: null
      };
    }

    componentDidMount() {
      firebase.auth.onAuthStateChanged(authUser => {
        authUser
          ? this.setState(() => ({ authUser }))
          : this.setState(() => ({ authUser: null }));
      });
    }

    render() {
      const { authUser } = this.state;

      return (
        <AuthUserContext.Provider value={authUser}>
          <Component {...this.props} />
        </AuthUserContext.Provider>
      );
    }
  };

export default withAuthentication;

1 个答案:

答案 0 :(得分:2)

如果再尝试呢?

firebase.auth.onAuthStateChanged(authUser => {
  if (authUser)
    this.setState(() => ({ authUser })) 
  else
    this.setState(() => ({ authUser: null })); 
});