【发布时间】:2019-12-04 03:35:07
【问题描述】:
我正在使用 react、redux 和 redux-thunk 有一组我必须在“逻辑层”中运行的查询。每个“action”调用的结果决定了我采取的路径,所以我实际上在等待从 dispatch 调用返回给 redux-thunk 的 promise。
const handleClick = async (args) => {
let foo = await dispatch(fetchFoo(args));
if(foo) { do something... }
else {
let bar = await dispatch(fetchBar(args));
if(bar) { do another thing... }
else { or do another thing... }
}
};
thunk 示例:
export const fetchFoo = args => async (dispatch) => {
let foo = await api.fetchFoo(args);
dispatch(fetchSuccess(foo));
// !!!!!!!!!!
return foo;
// !!!!!!!!!!
}
如果我不这样做,等到重新渲染(可能)将“foo”放入 redux 状态道具,然后再等到重新渲染(可能)将“bar”放入,这是非常尴尬的redux 状态等...
我以前从未真正见过这种模式,尽管我已经看到等待 void 承诺从 thunk 返回。
是否可以从 redux-thunk 操作返回值并使用它而不是从 redux 状态选择器中获取值?这似乎打破了“单一事实来源”的规则。如果没有,我该怎么办?
【问题讨论】:
标签: reactjs redux react-redux redux-thunk