Skip to content

Instantly share code, notes, and snippets.

@mattlockyer
Last active April 25, 2020 15:30
Show Gist options
  • Save mattlockyer/248c3fbeb19824140e09de133e59be66 to your computer and use it in GitHub Desktop.
Save mattlockyer/248c3fbeb19824140e09de133e59be66 to your computer and use it in GitHub Desktop.
A waaay simpler redux, redux-thunk friendly setup for reducer, state and actions
import { getReducer, getState } from '../util/redux-util'
//default state
const defaultState = {
toggle: false
}
//reducer
const type = 'appReducer'
export const appReducer = getReducer(type, defaultState)
export const appState = getState(type)
//actions
export const onToggle = (val) => async (dispatch, getState) => {
dispatch({ type, toggle: val })
}
//generic reducer for updating state
export const getReducer = (type, defaultState) => {
return (state = defaultState, action) => {
if (action.type !== type) return state
delete action.type
return { ...state, ...action }
}
}
//generic function for returning state based on keys found in defaultState
export const getState = (type) => ({ [type]: { ...state } }) => state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment