【发布时间】: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