Created
April 11, 2019 17:05
-
-
Save mattlockyer/9f8040b6e03a6d454a7909e5f44d8b1e to your computer and use it in GitHub Desktop.
Example of Reduced Redux Code for a User State Boilerplate
This file contains hidden or 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 { reducer, UPDATE } from '../util/redux-util' | |
//state | |
export const userState = ({ userReducer: { ...keys } }) => (keys) | |
//dispatch | |
export const userDispatch = (dispatch) => ({ | |
onMount: () => dispatch(mount()), | |
}); | |
//functions | |
export const mount = () => async (dispatch, getState) => { | |
console.log('REDUX: user mount') | |
const { user } = getState().userReducer | |
dispatch({ type: UPDATE, user }) | |
} | |
//reducer | |
export const userReducer = (state = { | |
user: null, | |
}, action) => reducer(state, action) | |
/* Somewhere else */ | |
//generic event type for updating state | |
export const UPDATE = 'UPDATE_STATE' | |
//generic reducer for updating state | |
export const reducer = (state, action) => { | |
//console.log(action) | |
const { type } = action | |
switch (type) { | |
case UPDATE: | |
const a = { ...action } | |
delete a.type | |
return { ...state, ...a } | |
default: | |
return state | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment