为什么派遣到减速器会导致所有减速器都被调用?

Why a dispatch to a reducer causes all reducers get called?

在此 github redux example, a dispatch of the event ADD_TODO is used to add a task. During the debugging, I found out that adding a task causes both the reducers todosvisibilityFilter 被调用。

如何在添加任务时只调用 todos reducer 而不是 visibilityFilter reducer。还有 visibilityFilter reducer 如果我发送类型为 SET_VISIBILITY_FILTER 的事件。

combineReducers 实用程序有意为每个操作调用所有附加的 reducer 函数,并给它们一个响应的机会。这是因为建议的 Redux reducer 结构是 "reducer composition",其中许多大部分独立的 reducer 函数可以组合到一个结构中,并且许多 reducer 函数可能会响应单个操作并更新它们自己的状态片。

如其他答案中所述,每当调用分派时,combineReducers 都会调用每个 reducer。您可以通过使默认情况等于传入的状态参数来避免其他值发生变化,因此实际上它们被重新分配了当前值。 例如:

const individualReducer = (state = "initialState", action) => {
    switch(action.type)
    {
        case "ACTION_TYPE":
            return action.payload;
        default:
            return state;
    }
}

export default individualReducer;