Redux:是否有任何标准实现或库来跟踪异步请求的状态?
Redux: Is there any standard implementation or library for keeping track of the status of an async request?
在 redux async actions in the docs 中,异步请求的状态在各种对象的状态容器中保存为 属性 isFetching
:
{
selectedSubreddit: 'frontend',
postsBySubreddit: {
frontend: {
isFetching: true,
didInvalidate: false,
items: []
},
reactjs: {
isFetching: false,
...
这很好用,但是我正在构建我的应用程序,我正在寻找可以扩展到必须保存在我的状态容器中并与我的 api 同步的多个对象的设计模式.所以我正在寻找 redux 社区采用的标准或库。
我发现 Flux Standard Action 看起来很合理,但这更多是关于如何处理负载和错误的标准化,不是 异步请求的状态。
是否有许多 redux 开发人员正在使用的任何库或模式?我想可能会有类似 { success, isFetching, error }
.
的东西
看看这个library,想怎么用就怎么用。
在我的应用程序中,我是这样使用它的,首先你将它添加到商店配置中的中间件。在此之后,您将操作设置为承诺,有效负载就是承诺。
export const reqAllGames = games => {
const promise = new Promise((resolve, reject) => {
request
.get(`${config.ROOT_URL}/${config.API_KEY}`)
.end((err, res) => {
if (err) {
reject(err);
} else {
resolve(res.body.top);
}
});
});
return {
type: types.RECEIVE_ALL_GAMES,
payload: promise
};
};
在你可以设置你的减速器之后:
const gameReducer = (games = { isFetched: false }, action) => {
switch (action.type) {
case `${types.RECEIVE_ALL_GAMES}_PENDING`:
return {};
case `${types.RECEIVE_ALL_GAMES}_FULFILLED`:
return {
games: action.payload,
err: null,
isFetched: true
};
case `${types.RECEIVE_ALL_GAMES}_REJECTED`:
return {
games: null,
err: action.payload,
isFetched: true
};
default:
return games;
}
};
希望能帮到你 ;)
是的,Redux 有各种各样的插件,其中许多与异步行为相关。我的 Redux addons catalog lists pretty much all of them. There's middlewares for handling async behavior, utilities to generate actions describing async work, prebuilt libs to track request status,等等。
在 redux async actions in the docs 中,异步请求的状态在各种对象的状态容器中保存为 属性 isFetching
:
{
selectedSubreddit: 'frontend',
postsBySubreddit: {
frontend: {
isFetching: true,
didInvalidate: false,
items: []
},
reactjs: {
isFetching: false,
...
这很好用,但是我正在构建我的应用程序,我正在寻找可以扩展到必须保存在我的状态容器中并与我的 api 同步的多个对象的设计模式.所以我正在寻找 redux 社区采用的标准或库。
我发现 Flux Standard Action 看起来很合理,但这更多是关于如何处理负载和错误的标准化,不是 异步请求的状态。
是否有许多 redux 开发人员正在使用的任何库或模式?我想可能会有类似 { success, isFetching, error }
.
看看这个library,想怎么用就怎么用。
在我的应用程序中,我是这样使用它的,首先你将它添加到商店配置中的中间件。在此之后,您将操作设置为承诺,有效负载就是承诺。
export const reqAllGames = games => {
const promise = new Promise((resolve, reject) => {
request
.get(`${config.ROOT_URL}/${config.API_KEY}`)
.end((err, res) => {
if (err) {
reject(err);
} else {
resolve(res.body.top);
}
});
});
return {
type: types.RECEIVE_ALL_GAMES,
payload: promise
};
};
在你可以设置你的减速器之后:
const gameReducer = (games = { isFetched: false }, action) => {
switch (action.type) {
case `${types.RECEIVE_ALL_GAMES}_PENDING`:
return {};
case `${types.RECEIVE_ALL_GAMES}_FULFILLED`:
return {
games: action.payload,
err: null,
isFetched: true
};
case `${types.RECEIVE_ALL_GAMES}_REJECTED`:
return {
games: null,
err: action.payload,
isFetched: true
};
default:
return games;
}
};
希望能帮到你 ;)
是的,Redux 有各种各样的插件,其中许多与异步行为相关。我的 Redux addons catalog lists pretty much all of them. There's middlewares for handling async behavior, utilities to generate actions describing async work, prebuilt libs to track request status,等等。