Skip to content

Instantly share code, notes, and snippets.

@eveningkid
Created March 31, 2020 12:14
Show Gist options
  • Save eveningkid/cbab2da67e69939093d9228ccde25178 to your computer and use it in GitHub Desktop.
Save eveningkid/cbab2da67e69939093d9228ccde25178 to your computer and use it in GitHub Desktop.
const initialState = { name: '' };
function reducer(state = initialState, action) {
switch (action.type) {
case 'CHANGE_NAME':
return { ...state, name: action.value };
}
}
const action = {
type: 'CHANGE_NAME',
value: 'Arnaud'
};
reducer(initialState, action);
// { name: 'Arnaud' }
function createStore(reducer) {
let state = {};
return {
getState() {
return state;
},
dispatch(action) {
state = reducer(state, action);
}
};
}
const store = createStore(reducer);
store.dispatch(action);
store.getState();
// { name: 'Arnaud' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment