简体   繁体   English

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

[英]No unused expressions on function inside componentDidMount

How can I fix or disable ESLint to ignore 如何修复或禁用ESLint以使其忽略

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

on this 在这个

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

Whilst, I understand the reason for the error and could just ignore it - as it's doing everything that it's is intended for. 同时,我了解错误的原因,可以忽略它-因为它正在执行它打算做的所有事情。

Is there a way to remove this error from this document only, not globally? 有没有一种方法可以仅从本文档中删除此错误,而不能从全局中删除此错误? Or perhaps, is there a better way for me to rewrite this to avoid the error whilst achieving the same outcome? 或者,也许有一种更好的方法让我重写此代码以避免在获得相同结果的同时出错?

Full Component 全部成分

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;

How about trying if then else? 如果再尝试怎么办?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM