react-native - redux saga dispatch 可以立即返回 api 结果而不是返回 action
问题描述
在 Promise 中给出下面的调度
Promise.resolve(this.props.dispatch(getAPI(item))) // dispatch
.then(function (result) { //result is the action passed in
Linking.openURL(result.url);
})
一旦调度执行并调用一个 api,然后返回一个类似 url 的值,我可以立即访问 then() 中的 api 结果吗?这样我就可以立即使用结果并将其传递给 Linking.openUrl()
我问的原因是因为当 api 结果发送到 mapToProps 时,我似乎无法获得最新的道具。如果我尝试使用诸如 componentWillUpdate 之类的生命周期方法来强制更新道具,它会多次调用 componentWillUpdate 并且当它打开 url 时,您可以在屏幕上看到它多次刷新
解决方案
考虑到您要调用的 api 包含在一个promise中,
Sagas.js
function * defaultSaga (action) {
try {
... Other Code
const result = yield call(YourApi, params) // Check the docs for redux-saga call
// On api Success
if(result && result.ok) {
yield call(Linking.openUrl, result.url)
}
} catch(ex) {
yield put(// Any error action here)
}
}