简体   繁体   English

Redux合并来自不同reducer的2个状态

[英]Redux merge 2 states from different reducers

I have 2 reducers (categoryReducer and productReducer) which fetch data from an API. 我有2个reducer(categoryReducer和productReducer),它们从API提取数据。 Both set a loading and error state. 两者都设置加载和错误状态。 I combine these 2 reducers with combineReducers: 我将这两个减速器与CombineReducers结合使用:

const rootReducer = combineReducers({
 categoryReducer,
 productReducer
});

Is it possible to merge the loading and error state of these 2 reducers ? 是否可以合并这两个减速器的负载和错误状态? Because my Application should display a loading text, whenever the loading state is true. 因为我的应用程序应该显示加载文本,所以每当加载状态为true时。 So that I can do something like: 这样我可以做类似的事情:

if (this.props.isLoading)
   ...

instead of two different loading States 而不是两个不同的加载状态

if (this.props.isCategoryLoading || this.props.isProductLoading)
   ...

You can use selector and caculate isLoading from isCategoryLoading and isProductLoading within the selector: 您可以使用选择器,并在选择器中从isCategoryLoading和isProductLoading计算isLoading:

// Your container component:
const mapStateToProps = (state) => ({
    isLoading: getIsLoading(state)
});

export const YourContainer = connect(
    mapStateToProps,
    mapDispatchToProps
)(YourComponent);

// Your selector:

export const getIsLoading = state => state.isCategoryLoading || state.isProductLoading;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM