Redux:为什么reducer被两次调用(初学者)

时间:2019-01-02 12:46:44

标签: redux

为什么我的异径管叫两次?请首先找到源代码,并在开发人员工具的屏幕截图下方。

代码:

import { createStore } from 'redux';

const initState = {
    title: "-",
    todos: []
}

function rootReducer(state = initState, action) {
    console.log("rootReducer, state=", state, "rootReducer, action=", action);

    if (action.type === "ADD_TODO") {
        return {
            ...state,
            todos: [...state.todos, action.todo],
        }
    }
    return state;
}

const store = createStore(rootReducer);


store.subscribe(() => {
    console.log("State updated!", store.getState());
});


store.dispatch({ type: 'ADD_TODO', todo: 'Learn Babel' });

开发人员工具: enter image description here

谢谢

1 个答案:

答案 0 :(得分:1)

第一个调用来自createStore(rootReducer),试图测试您的reducer的不良模式。