【发布时间】:2019-06-29 19:35:26
【问题描述】:
- 第一次调用 (Storage.list('s3 bucket path')) 返回一个键列表。
- 对于每个键,我都会进行第二次调用 (Storage.get('key')),它会返回一个 url
- 在第一个和第二个 API 调用中,我使用 console.log 并获取每个所需的正确数据。
- this.setState 在第一个 API 调用中完美运行,但在第二个 API 调用中无法正常工作。
我的代码是:
state = { keyItems: [], urlItems: [] }
componentDidMount() {
Storage.list('')
.then(keyItems => {
console.log("keyItems: ", keyItems)
this.setState({ keyItems: keyItems })
/*for each photo key in the folder..*/
keyItems.forEach(function(keyItem) {
/*get the URL*/
Storage.get(keyItem.key)
.then(urlItem => {
console.log("urlItem: ", urlItem)
/*add URL item to state*/
/*ANY CALL TO this.setState HERE DOESN'T WORK, i've tried
even simple tests*/
this.setState(prevState => ({ urlItems:[...prevState.urlItems, { key: keyItem.key, source: urlItem } ]}))
})
.catch(err => {
console.log("error fetching photo URL", err)
})
})
})
.catch(err => {
console.log("error fetching photos keys", err)
})
}
- 我尝试从 componentDidMount 外部的箭头函数进行调用,但仍然无法访问 this.setState
谢谢!
【问题讨论】:
标签: react-native amazon-s3 this setstate aws-amplify