Skip to content

Instantly share code, notes, and snippets.

@bjdixon bjdixon/action.js
Created Oct 19, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.