Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import {combineReducer} from "redux";
import counter from "./reducerExample";
/*
The idea is to
*/
export default const rootReducer = combineReducer({
counter
//, other reducers goes here with this object.
});
const INITIAL_STATE = {
count: 0
};
/*
Counter reducer takes two parameter:
state, action
state is initialized to an object with count object. Action parameter
receives action dispatched by some component.
Every reducer gets every action, that's being dispatched. The reducer then checks for its type and see
if it has execution method for that type, by using switch-case statements.
*/
export default function CounterReducer(state=INITIAL_STATE, action){
switch(action.type){
case "INCREMENT_COUNTER":
// Caution !! its considered to be a bad practice to mutate the state.
// You should use Object.assign method or immutable.js to make a new copy, rather.
state.count += 1;
return state;
default:
return state;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment