javascript – 在两个Redux Reducers / States之间共享数据

前端之家收集整理的这篇文章主要介绍了javascript – 在两个Redux Reducers / States之间共享数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是两个州/减少者之间数据共享的合理解决方案吗?
//combineReducers
function coreReducer(state = {},action){

    let filteRSState = filters(state.filters,action);
    let eventsState = events(state.events,action,{ filters: filteRSState});

    return { events: eventsState,filters : filteRSState};

}

export const rootReducer = combineReducers(
    {
        core  : coreReducer,users
    }
);

如果是这样,如果对同一个调度事件和第二个减少函数的回答都取决于第一个的新状态,那么如何保证执行reducer函数的顺序呢?

假设我们调度一个SET_FILTER事件,该事件附加到过滤器Store中的activeFilters集合,然后更改事件Store中项目相对于activeFilters值的可见性.

//ActiveFilters reducer
function filtersActions(state = {},action){

    switch (action.type) {
        case SET_FILTER:
            return Object.assign({},state,{
                [action.filterType]: action.filter
            })
        case REMOVE_FILTER:
            var temp = Object.assign({},state);
            delete temp[action.filterType];
            return temp;

        case REMOVE_ALL_FILTERS:
            return {};

        default:
            return state
    }
}

我想我找到了答案 – 计算派生数据 – 重新选择

http://redux.js.org/docs/recipes/ComputingDerivedData.html

解决方法

/--------container--------/
import {getGroupsAndMembers} from '../reducers'
const mapStateToProps = (state) => {
  return {
    inputValue: state.router.location.pathname.substring(1),initialState: getGroupsAndMembers(state) <-- this one
 }
}


/--------reducers--------/
export function getGroupsAndMembers(state){
   let { groups,members } = JSON.parse(state)
   response = {groups,members}
   return response;
}


GroupsContainer.propTypes = {
 //React Redux injection
 pushState: PropTypes.func.isrequired,// Injected by React Router
 children: PropTypes.node,initialState:PropTypes.object,}

不要忘记遵循’连接’的指导方针

export default connect(mapStateToProps,{ pushState })(GroupsContainer)

猜你在找的JavaScript相关文章