javascript - componentDidMount 内的函数上没有未使用的表达式
问题描述
如何修复或禁用 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;
解决方案
if then else 试试呢?
firebase.auth.onAuthStateChanged(authUser => {
if (authUser)
this.setState(() => ({ authUser }))
else
this.setState(() => ({ authUser: null }));
});
推荐阅读
- python - Errno 13 Permission denied.... 我无法访问此 JSON 文件
- swift - TestRail 与 XCUITest 的集成
- database - 将参数传递给报表中使用的查询
- reactjs - 如何使用 create-react-app 登录到服务器端控制台?
- java - 从另一个 Jenkins 构建中查找工件 URL
- javascript - MongoDB 触发器文件中的代码是 Producing a Customer.findOne() is not a function 错误
- gradle - 使用 gradle java pugin 构建类包结构不一致
- angular - 如何使用 Material Angular Sort 对多个表格进行排序?
- javascript - 从不同的 Spotfire 服务器动态加载不同的 Spotfire 报告
- android - 如何将原始日期字符串转换为年龄?