【发布时间】:2018-09-30 21:23:38
【问题描述】:
我正在尝试一个接一个地进行两次 ajax 调用,即,根据第一次调用数据的结果,我正在进行第二次调用。我正在尝试使用 thunk,但它没有发生,我收到错误。
actions.js
const fetchedStateNameSucess = (json) => {
return {
type: 'FETCH_STATE_NAME_SUCCESS',
json
}
}
const fetchProvidersDetailsSuccess = (providersData) => {
return {
type: 'FETCH_PROVIDER_DETAILS_SUCCESS',
providersData
}
}
export const fetchProvidersDetails = (providerId) => {
return (dispatch) => {
const providerUrl = `http://someUrl`;
const stateClientCountUrl = `http://someUrl/${state}`;
fetch(providerUrl)
.then(response => response.json())
.then(json => dispatch(fetchProvidersDetailsSuccess(json)))
.then((stateValue = json[0].stateVal)
fetch(stateClientCountUrl)
dispatch(fetchedStateNameSucess(response)));
};
}
在上述调用 fetch(providerUrl) 中,我在获取 stateval 的地方得到响应,如何在第二次调用以 stateval 作为参数的 fetch(stateClientCountUrl) 时使用它。
【问题讨论】:
-
你能把确切的错误贴在这里吗?
-
我假设您的代码中有一些拼写错误。尝试查看和编辑您的问题。您多次分派同一个动作创建者。
标签: ajax reactjs redux fetch redux-thunk