Skip to content

Instantly share code, notes, and snippets.

@bjdixon
Created October 19, 2017 04:49
Show Gist options
  • Save bjdixon/b1386ebacdb57397e472a9360e106275 to your computer and use it in GitHub Desktop.
Save bjdixon/b1386ebacdb57397e472a9360e106275 to your computer and use it in GitHub Desktop.
Alternative to switch statements in reducers
import { LANGUAGE, COUNTRY } from './constants';
export const setLanguage = language => ({
type: LANGUAGE,
language
});
export const setCountry = country => ({
type: COUNTRY,
country
});
export const LANGUAGE = 'LANGUAGE';
export const COUNTRY = 'COUNTRY';
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