Skip to content

Instantly share code, notes, and snippets.

@pavsidhu
Last active January 29, 2017 11:41
Show Gist options
  • Save pavsidhu/e37c51a359bab6c19f76aa7a019a88ea to your computer and use it in GitHub Desktop.
Save pavsidhu/e37c51a359bab6c19f76aa7a019a88ea to your computer and use it in GitHub Desktop.
Redux reducer example
import {ADD_TO_COUNTER} from './actions'
// This is the default state of the app i.e. when the app starts for the first time
const initialState = {
counter: 0
}
// This is a reducer which listens to actions and modifies the state
const reducer = (state = initialState, action) => {
// A switch is used since if more actions are added in the future, it will be easy
// to be able to handle this in the reducer since we just add another 'case'.
switch (action.type) {
case ADD_TO_COUNTER:
return {
...state,
counter: state.counter + 1
}
default:
return state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment