2016-12-27 35 views
0

如何在SideBarReducer中更改MenuReducer的状态?这意味着当用户在边栏中执行操作时在菜单中显示警告消息。用户将来也会在其他减速器中执行操作,这会导致MenuReducer中的警告弹出。如何更改react.js中的另一个reducer的状态?

MenuReducer:

import actionTypes from '../action-types'; 

export const initialState = { 
    showWarning: false, 
}; 

export default function MenuReducer(state = initialState, action) { 
    switch (action.type) { 
    case actionTypes.REQUEST_REFRESH: 
     if (action.payload.unsavedChanges) { 
     return { ...state, showWarning: true }; 
     } 
    default: 
     return state; 
    } 
} 

SidebarReducer:

import actionTypes from '../action-types'; 

export default function sideBarReducer(state = initialState, action) { 
    switch (action.type) { 
    case actionTypes.REQUEST_CLOSE: 
     if (action.payload.unsavedChanges) { 
     // Change the state of MenuReducer 
     } 
    default: 
     return state; 
    } 
} 

回答

1

您可以创建调度2点的操作(使用咚中间件)的动作。 您也可以在两个缩减器中捕获相同的操作并相应地更新相应的东西。

0

你不应该从另一个减速器发射动作,这是反模式。所有操作都需要在React组件生命周期或动作创建器中触发。

减速机只是为了国家的变化。

相关问题