【发布时间】:2020-11-09 09:07:37
【问题描述】:
这里是代码示例:
class App extends Component {
constructor() {
super();
this.state = {
currentColor: "red"
};
while(1){
await this.changeColor();
}
}
changeColor = async () => {
console.log("123")
setTimeout(() => {
this.setState({
currentColor: "yellow"
});
setTimeout(() => {
this.setState({
currentColor: "green"
});
setTimeout(() => {
this.setState({
currentColor: "red"
});
}, 100);
}, 200);
}, 300);
};
render() {
return (
<div>
<div
className={this.state.currentColor}
style={{ width: "100px", height: "100px" }}
/>
</div>
);
}
}
当我在 changeColor() 前面添加 await 时,出现“意外的严格模式保留字”错误。在线代码:https://stackblitz.com/edit/react-cm4jdq。 (我想纠正一个红绿灯演示)
【问题讨论】:
标签: reactjs