redux saga 可以立即发送 return api 结果而不是 returning 操作吗
can redux saga dispatch return api result immediately instead of returning action
在 Promise 中给出下面的调度
Promise.resolve(this.props.dispatch(getAPI(item))) // dispatch
.then(function (result) { //result is the action passed in
Linking.openURL(result.url);
})
一旦调度执行并调用一个 api,然后 returns 一个像 url 的值,我可以立即访问 then() 中的 api 结果吗?这样我就可以立即使用结果并将其传递给 Linking.openUrl()
我问的原因是当 api 结果发送到 mapToProps 时,我似乎无法获得最新的道具。如果我尝试使用像 componentWillUpdate 这样的生命周期方法来强制更新 prop,它会多次调用 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)
}
}
在 Promise 中给出下面的调度
Promise.resolve(this.props.dispatch(getAPI(item))) // dispatch
.then(function (result) { //result is the action passed in
Linking.openURL(result.url);
})
一旦调度执行并调用一个 api,然后 returns 一个像 url 的值,我可以立即访问 then() 中的 api 结果吗?这样我就可以立即使用结果并将其传递给 Linking.openUrl()
我问的原因是当 api 结果发送到 mapToProps 时,我似乎无法获得最新的道具。如果我尝试使用像 componentWillUpdate 这样的生命周期方法来强制更新 prop,它会多次调用 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)
}
}