Created
January 25, 2017 10:47
-
-
Save jyotendra/1e59b2ef2152c790a1a753f62db092f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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