Created
October 19, 2017 04:49
-
-
Save bjdixon/b1386ebacdb57397e472a9360e106275 to your computer and use it in GitHub Desktop.
Alternative to switch statements in reducers
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 { LANGUAGE, COUNTRY } from './constants'; | |
export const setLanguage = language => ({ | |
type: LANGUAGE, | |
language | |
}); | |
export const setCountry = country => ({ | |
type: COUNTRY, | |
country | |
}); |
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
export const LANGUAGE = 'LANGUAGE'; | |
export const COUNTRY = 'COUNTRY'; |
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 { LANGUAGE, COUNTRY } from './constants'; | |
const defaultState = { language: 'en', country: 'ca' }; | |
export default (state = defaultState, action) => { | |
const updates = { | |
[LANGUAGE]: { language: action.language }, | |
[COUNTRY]: { country: action.country } | |
}; | |
return updates[action.type] ? Object.assign({}, state, updates[action.type]) : state; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment