gpt4 book ai didi

javascript - 在react.js中使用异步时出现`The '异步' modifier can only be used in TypeScript files.ts(8009)`错误

转载 作者:行者123 更新时间:2023-12-02 21:18:01 24 4
gpt4 key购买 nike

因此,当我尝试在 react 文件中添加 async/await 时,出现以下错误:

The 'async' modifier can only be used in TypeScript files.ts(8009)

这是否意味着我无法在 React 中使用 async/await 或者这是 VS Code 的问题,或者我该怎么做才能启用它?

这是整个文件:

import React from 'react'
import Button from 'react-bootstrap/Button'
import {addOrder} from '../actions/orderAction'
import {connect} from 'react-redux'

class Dish extends React.Component{

async handleClick = (dish) =>{
this.props.addOrder(dish)
localStorage.setItem("order", this.props.order)
alert(`${dish.name} added to your order!`)
}


render(){
let dish = this.props.dish
return(
<div className="col-4">
<img />
<h5>{dish.name} ${dish.price}</h5>
<Button onClick = {()=>this.handleClick(dish)} size="sm" variant="primary">Order</Button>
</div>
)
}
}

const mapDispatchToProps = dispatch =>{
return {
addOrder: (dish) =>dispatch(addOrder(dish))
}
}

const mapStateToProps = state =>{
return{
order: state.order
}
}


export default connect(null,mapDispatchToProps)(Dish)

谢谢!

最佳答案

你可以尝试一下

async handleClick(dish) {
this.props.addOrder(dish)
localStorage.setItem("order", this.props.order)
alert(`${dish.name} added to your order!`)
}

或者

handleClick = async(dish) => {
this.props.addOrder(dish)
localStorage.setItem("order", this.props.order)
alert(`${dish.name} added to your order!`)
}

关于javascript - 在react.js中使用异步时出现`The '异步' modifier can only be used in TypeScript files.ts(8009)`错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60918187/

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