【问题标题】:React JS - Error - Expected an assignment or function call and instead saw an expression no-unused-expressionsReact JS - 错误 - 期望一个赋值或函数调用,而是看到一个表达式 no-unused-expressions
【发布时间】:2021-04-01 09:48:15
【问题描述】:

我正在使用 Axios 函数,将 API 调用返回到响应数据,如下所示。

componentDidMount(){
 axios.get("https://api.covid19india.org/state_district_wise.json").then(response => {
      response.data 
      this.setState({stateData:response.data});
   });
}

但是编译时出现以下错误。 Expected an assignment or function call and instead saw an expression no-unused-expressions。 当我删除respose.data 行时,它正在工作。但不返回结果。

请帮忙

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    改为:

    axios.get("https://api.covid19india.org/state_district_wise.json").then(response=>{
        this.setState({stateData:response.data});
        return response.data ;
    });
    

    then(response => {/* function body */}){} 中的代码是函数的主体,then 返回该函数的结果。

    【讨论】:

      猜你喜欢
      • 2019-08-15
      • 2021-11-17
      • 2021-08-23
      • 2019-05-16
      • 1970-01-01
      • 2021-05-20
      • 2020-12-10
      • 2020-04-21
      • 2019-11-23
      相关资源
      最近更新 更多