我正在尝试通过在redux传奇中调用put方法来触发redux reducer,但是即使我返回了应该在我的reducer中捕获的相同操作类型,它也不会被触发。
这是我的传奇故事
export function* registrationSaga(action) {
try {
const response = yield call(registerUser, action.payload);
console.log(response)
yield put({ type: REGISTER_USER_SUCCESS, response })
} catch(error) {
yield put({ type: REGISTER_USER_ERROR, error });
}
}
这是我的减速器:
export const registrationReducer = (state = {}, action) => {
let response = action.response;
switch(action.type) {
case REGISTER_USER_SUCCESS: {
console.log("successful registration")
return { ...state, registration_success: response.success, registration_message: response.message };
}
case REGISTER_USER_ERROR:
return { ...state, registration_success: response.success, registration_message: response.message };
default:
return state;
}
}
我希望在控制台中看到“成功注册”,并在组件中获得该响应作为支持,但这不会发生。什么会导致此问题?
这是我的rootReducer,还存储:
const rootReducer = () => combineReducers({
registrationReducer,
todoReducer
})
export default rootReducer;
并存储:
const sagaMiddleware = createSagaMiddleware()
export const store = createStore(
rootReducer,
applyMiddleware(sagaMiddleware)
);
sagaMiddleware.run(rootSaga)
答案 0 :(得分:1)
rootReducer
不应是调用combineReducers
的函数,而应是combineReducers
的返回值:
const rootReducer = combineReducers({
registrationReducer,
todoReducer,
});
export default rootReducer;