【发布时间】:2018-09-06 09:21:43
【问题描述】:
我是 React 和 api 的新手。我正在尝试发出 2 个获取请求并将 2 个带有新值的键分配给“items”数组。这里来自第二个获取请求的“img”键不断覆盖整个对象。因此,它使第一个获取请求就好像它不存在一样。我只需要将第二个键与来自第一次提取的第一个键值相附加。希望这确实有意义。
fetch(url,{
method: 'GET'
})
.then((response)=> response.json())
.then((responseJson) => {
const newItems = responseJson.items.map(i => {
return{
name: i.name
};
})
const newState = Object.assign({}, this.state, {
items: newItems
});
console.log(newState);
this.setState(newState);
})
.catch((error) => {
console.log(error)
});
fetch(url2,{
method: 'GET'
})
.then((response)=> response.json())
.then((responseJson) => {
const newItems = responseJson.ebay.map(i => {
return{
img: i.picture.url[0]
};
})
const newState = Object.assign(this.state, {
items: newItems
});
console.log(newState);
this.setState(newState);
})
.catch((error) => {
console.log(error)
});
【问题讨论】:
标签: reactjs api ecmascript-6 get fetch