Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`The 'async' modifier can only be used in TypeScript files.ts(8009)` error when using async in react.js

So when I try to add async/await in the react file, I got this error:

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

Does that mean I cannot use async/await in React or that is a problem of vs code or what can I do to enable it?

Here is the whole file:

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)

Thanks!

like image 827
Yingqi Avatar asked Mar 29 '20 17:03

Yingqi


2 Answers

You can try either

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

OR

handleClick = async(dish) => {
  this.props.addOrder(dish)
  localStorage.setItem("order", this.props.order)
  alert(`${dish.name} added to your order!`)
}
like image 55
Abdul Mateen Shaikh Avatar answered Sep 28 '22 02:09

Abdul Mateen Shaikh


H\ve you tried this in settings.json

"javascript.validate.enable": true

like image 34
Jon Jones Avatar answered Sep 28 '22 03:09

Jon Jones