【问题标题】:Getting an error "Type 'void' is not assignable to type 'ReactNode'"收到错误“类型'void'不可分配给类型'ReactNode'”
【发布时间】:2019-10-06 09:56:02
【问题描述】:

我正在尝试调用这个函数

    compatiblelist() {
         CompatibleDevicesAPI().then(
           response => {this.getCompatList(response)}
       );

   }

每当我打电话给{this.compatiblelist()}时都会出错

CompatibleDevicesAPI() 函数如下所示

export default function CompatibleDevicesAPI() {
    return fetch("https://abc/jjklsd/jlasd").then((res) => {
        let results = [];
        for(var i=0;i<res.response.docs.length;i++){
            let obj ={
                'compManufacturer': res.response.docs[i].comanufacturer,
                'comDisplayName': res.response.docs[i].comDisplayName ,
                'id' : res.response.docs[i].id,
            }
            results.push(obj);
        }
        return results;
    }).catch((error) => {
        let results = [];
        results.push({
            'comManufacturer': 'error',
            'comDisplayName': error
        })
        return results;
    });
}

【问题讨论】:

  • 你想让这个函数做什么?请贴出调用函数的代码
  • this.getCompatList(response) -> 我希望将响应传递给这个函数 this.getCompatList 。在这里,我正在处理响应并将其显示在 UI 上。我有一个渲染函数,我在其中调用
    {this.getcompatList()}
  • 我刚刚添加了其余的代码
  • 不要在渲染方法中执行副作用,改用componentDidUpdate

标签: javascript reactjs typescript


【解决方案1】:

我认为你只需要添加返回

.then( response =&gt; {return this.getCompatList(response)} )

【讨论】:

  • 或者去掉括号:.then(response =&gt; this.getCompatList(response))
猜你喜欢
  • 2020-07-22
  • 2022-11-11
  • 2016-06-24
  • 2021-02-06
  • 2018-01-04
  • 2020-06-02
  • 2022-12-09
  • 2021-09-23
  • 2017-09-21
相关资源
最近更新 更多