javascript - 将 Promise.all 与 React-Redux-Thunk 一起使用的更好/正确方法是什么?
问题描述
export const FETCH_DB_BEGIN = 'FETCH_DB_BEGIN'
export const FETCH_DB_SUCCESS = 'FETCH_DB_SUCCESS'
export const FETCH_DB_FAILURE = 'FETCH_DB_FAILURE'
export const fetchDatabase = () => {
return dispatch => {
const profile_url = 'localhost:5000/profiles'
const release_url = 'localhost:5000/releases'
const emp_url = 'localhost:5000/users'
let promises = []
let options = {
headers: header,
method: 'get',
mode: 'cors',
body: null,
}
dispatch(fetchDbBegin());
// run the script async. change state when it's done.
let profile_promise = new Promise((resolve, reject) => {
fetch(profile_url, options)
.then(res => res.json())
.then(resText => {
// Use Dispatch Here?
})
}).catch(err => {
console.log(err)
})
promises.push(profile_promise)
// run the script async. change state when it's done.
let release_promise = new Promise((resolve, reject) => {
fetch(release_url, options)
.then(res => res.json())
.then(resText => {
})
}).catch(err => {
console.log(err)
})
promises.push(release_promise)
// run the script async. change state when it's done.
let emp_promise = new Promise((resolve, reject) => {
fetch(emp_url, options)
.then(res => res.json())
.then(resText => {
})
}).catch(err => {
console.log(err)
})
promises.push(emp_promise)
Promise.all(promises).then(values => {
console.log(values)
})
}
}
export const fetchDbBegin = () => ({
type: FETCH_DB_BEGIN
});
export const fetchDbSuccess = (data) => ({
type: FETCH_DB_SUCCESS,
payload: { data }
});
export const fetchDbFailure = (err) => ({
type: FETCH_DB_FAILURE,
payload: { err }
});
我正在重构一个 React 类组件以使用Redux
. 它最初在内部有所有 API 调用,componentDidMount
而且非常混乱。
我正在使用redux-thunk
将其从类组件中移出。
fetchDatabase
in my完成了在类组件中所做的databaseAction.js
一切。componentDidMount
通常,如果它是单个 API 调用,我会fetchDbSuccess
在 API 调用成功完成时分派它。但是,使用Promise.All
which 需要三个异步 API 调用,我不确定是否应该
fetchProfileSuccess
为每个 API 调用( 、fetchReleaseSuccess
和)创建一个单独的操作,并在每个 Promise 的末尾(我在代码fetchUserSuccess
中放入的地方)分派它们中的每一个。//Use Dispatch Here?
或者
fetchDbSuccess
当Promise.all
问题得到解决时,只需发送单人。
如果我选择做 2,我应该更新states
我的减速器中的所有三个吗?
谢谢
解决方案
如果你有关心状态更新的代码,你应该只调度和更新状态。例如,如果您只想显示一个微调器,然后在完全完成后让微调器消失,您的用户不一定关心每个原子操作,因此您不需要将其反映在状态中。如果您有一个确实显示每个的 UI,那么您会想要那些额外的调度。
顺便说一句,你的 Promises 看起来有点过于复杂。如果您决定不需要这些额外的状态更改,则可以简化为:
export const FETCH_DB_BEGIN = 'FETCH_DB_BEGIN'
export const FETCH_DB_SUCCESS = 'FETCH_DB_SUCCESS'
export const FETCH_DB_FAILURE = 'FETCH_DB_FAILURE'
export const fetchDatabase = () => {
return dispatch => {
dispatch(fetchDbBegin());
const urls = [
'http://localhost:5000/profiles',
'http://localhost:5000/releases',
'http://localhost:5000/users'
];
const options = {
headers: header,
method: 'get',
mode: 'cors',
body: null,
}
const fetchJson = url => fetch(url, options).then(res => res.json());
Promise.all(urls.map(fetchJson))
.then(([profile, release, employee]) => {
dispatch(fetchDbSuccess({ profile, release, employee }));
})
.catch(err => {
dispatch(fetchDbFailure(err));
});
}
}
export const fetchDbBegin = () => ({
type: FETCH_DB_BEGIN
});
export const fetchDbSuccess = (data) => ({
type: FETCH_DB_SUCCESS,
payload: { data }
});
export const fetchDbFailure = (err) => ({
type: FETCH_DB_FAILURE,
payload: { err }
});
推荐阅读
- python-3.x - 迭代 df 中的行并使用匹配行的列操作结果插入新行
- c# - 如何使用 dapper OracleDynamicParameters() 从过程中获取价值?
- debugging - 捕获定期错误的问题
- html - 如何仅使 div 行的一部分可滚动(使用清晰度而不是引导样式)
- typescript - 如何从单元测试中实例化“vscode 模块”使用的类的对象。错误:找不到模块“vscode”
- php - WooCommerce Rest API 在创建订单中返回无效参数“shipping_lines”
- javascript - 订阅类别的推送通知
- qt - 更改宽度不生效
- nativescript - Nativescript中图像的线性渐变
- dicom - 当我们使用不同的传输语法从 CharruaSoft sendcu 工具发送 DICOM 时,为什么 dcm4che 会创建巨大的文件?