Skip to content

Instantly share code, notes, and snippets.

@mattlockyer
Created April 11, 2019 17:05
Show Gist options
  • Save mattlockyer/9f8040b6e03a6d454a7909e5f44d8b1e to your computer and use it in GitHub Desktop.
Save mattlockyer/9f8040b6e03a6d454a7909e5f44d8b1e to your computer and use it in GitHub Desktop.
Example of Reduced Redux Code for a User State Boilerplate
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