我在react中有一个由extends定义的组件React.Component。它的内部以及渲染方法是:
constructor(props, context) {
super(props, context);
this.state = {
open: false,
};
}
handleClose = () => { //this works
this.setState({open: false});
};
handleOpen = () => { //this works
this.setState({open: true});
};
let empty = () => {}; // does not work, error Parsing error: Unexpected token
如果我在任何箭头函数前面加上 let 或 const 前缀,我似乎会遇到意外的标记错误。我在这里遗漏了什么吗?
谢谢
最佳答案
阅读babel documentation关于类(class)。使用 let
或 const
不是 ES6 类定义可能前缀的一部分
关于javascript - 无法在 React 组件的 let/var 中存储箭头函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37838756/