javascript ESLint - ReactJS 中的非未使用表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52274829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ESLint - no-unused-expressions in ReactJS
提问by user5377005
Getting a ESLint error when copiling with babel:
使用 babel 编译时出现 ESLint 错误:
Line 28: Expected an assignment or function call and instead saw an expression no-unused-expressions
Line 29: Expected an assignment or function call and instead saw an expression no-unused-expressions
第 28 行:期望赋值或函数调用,却看到了一个表达式 no-unused-expressions
第 29 行:期望赋值或函数调用,却看到了一个表达式 no-unused-expressions
Any idea how to get rid of these whilst making my timer still work as intended? Or have you got a better way of me doing my timer?
知道如何摆脱这些同时让我的计时器仍然按预期工作吗?或者你有更好的方法让我做我的计时器吗?
class RequestTimer extends Component {
constructor(props) {
super(props);
this.state = {
seconds: 0,
minutes: 0,
hours: 0
}
this.getTime = this.getTime.bind(this);
}
getTime() {
let second = this.state.seconds
let minute = this.state.minutes;
let hour = this.state.hours;
this.state.seconds % 59 === 0 && this.state.seconds !== 0 ? minute += 1:null;
this.state.minutes % 59 === 0 && this.state.seconds % 59 === 0 && this.state.minutes !== 0 ? (hour += 1, minute = 0):null;
this.setState({
seconds: second +=1,
minutes: minute,
hours: hour
})
}
componentDidMount() {
this.timer = setInterval(this.getTime, 1000)
}
render() {
return (
<TimerContainer>
<h2>Last Request:</h2>
<p>{this.state.hours}h {this.state.minutes}m {this.state.seconds % 60}s</p>
</TimerContainer>
)
}
}
回答by Bergi
You want to use a proper ifstatement:
您想使用正确的if语句:
getTime() {
let second = this.state.seconds
let minute = this.state.minutes;
let hour = this.state.hours;
if (this.state.seconds % 59 === 0 && this.state.seconds !== 0) {
minute += 1;
}
if (this.state.minutes % 59 === 0 && this.state.seconds % 59 === 0 && this.state.minutes !== 0) {
hour += 1;
minute = 0;
}
this.setState({
seconds: second +=1,
minutes: minute,
hours: hour
});
}
Don't use the ternary operator if you don't want to do anything with the result value. And especially you should not use it when you have no elsecase or when you have to use the comma operator to do multiple things.
如果您不想对结果值执行任何操作,请不要使用三元运算符。尤其是当您没有else大小写或必须使用逗号运算符来执行多项操作时,您不应该使用它。
回答by Dhara Babariya
There are couple of solution.
有几个解决方案。
1.) Just Disable the rule for the entire file using eslint-disable at the top of the file.
1.) 只需在文件顶部使用 eslint-disable 禁用整个文件的规则。
/* eslint-disable no-unused-expressions */
2.) You should write below code at the end off all lines(28,29) so It's disabled.
2.)您应该在所有行(28,29)的末尾写下以下代码,以便它被禁用。
/* eslint-disable-line */
回答by Amitesh
Alternatively modify the ternary operation to have L.H.S expression
或者修改三元运算以具有 LHS 表达式
this.state.seconds % 59 === 0 && this.state.seconds !== 0 ? minute += 1:null;
this.state.seconds % 59 === 0 && this.state.seconds !== 0 ? minute += 1:null;
Change by
更改者
minute = this.state.seconds % 59 === 0 && this.state.seconds !== 0 ? minute + 1:null;
minute = this.state.seconds % 59 === 0 && this.state.seconds !== 0 ? minute + 1:null;

