ESLint 在构造函数变量上抛出 no-unused-vars 错误
ES Lint throwing no-unused-vars error on constructor variable
我有一个看起来像这样的组件:
class NavPane extends Component {
constructor(props) {
super(props);
let lastX = 0;
}
NavOnTouchStart = (e) => {
this.lastX = e.touches[0].clientX;
}
NavOnTouchMove = (e) => {
const currentX = e.touches[0].clientX;
if (currentX > this.lastX + 10) {
this.props.toggle();
}
this.lastX = currentX;
}
render() {
return (
<Collapse
...
</Collapse>
);
}
}
export default onClickOutside(NavPane);
这在执行时运行良好,但 ES Lint 似乎不同意。它不断在变量 lastX:
上抛出以下错误
21:9 error 'lastX' is assigned a value but never used
no-unused-vars
有人可以帮助我了解这里发生了什么吗?我确实意识到 let 和 const 的范围仅限于定义变量的块,这就是它抛出错误的原因,因为我不是 在构造函数中使用它。但是,当我改用 var 时,ES Lint 也会出错。
试试这个:
constructor(props) {
super(props);
this.lastX = 0;
}
我有一个看起来像这样的组件:
class NavPane extends Component {
constructor(props) {
super(props);
let lastX = 0;
}
NavOnTouchStart = (e) => {
this.lastX = e.touches[0].clientX;
}
NavOnTouchMove = (e) => {
const currentX = e.touches[0].clientX;
if (currentX > this.lastX + 10) {
this.props.toggle();
}
this.lastX = currentX;
}
render() {
return (
<Collapse
...
</Collapse>
);
}
}
export default onClickOutside(NavPane);
这在执行时运行良好,但 ES Lint 似乎不同意。它不断在变量 lastX:
上抛出以下错误21:9 error 'lastX' is assigned a value but never used no-unused-vars
有人可以帮助我了解这里发生了什么吗?我确实意识到 let 和 const 的范围仅限于定义变量的块,这就是它抛出错误的原因,因为我不是 在构造函数中使用它。但是,当我改用 var 时,ES Lint 也会出错。
试试这个:
constructor(props) {
super(props);
this.lastX = 0;
}