gpt4 book ai didi

javascript - 无法在 React 组件的 let/var 中存储箭头函数

转载 作者:行者123 更新时间:2023-12-02 14:30:56 28 4
gpt4 key购买 nike

我在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)。使用 letconst 不是 ES6 类定义可能前缀的一部分

关于javascript - 无法在 React 组件的 let/var 中存储箭头函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37838756/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com