Duck

An example of /store/redux/app.js following re-duck style

Define `createReducer` helper

const createReducer = (initialState = {}, handlers = {}) => (state, action) => {
  const nextState = produce(state, draft => {
    if (handlers[action.type]) {
      return handlers[action.type](draft, action)
    }
  })

  return nextState
}

produce is a function of immer

Declare initial state

const initialState = {
  locale: 'en',
  theme: 'light',
}

Declare action types

prefix app_name/domain_namespace for separating your action from 3rd parties actions such as connected-react-router, redux-saga when working with redux devtools

Declare action creators

Declare reducer

Declare selectors

createSelector is a function of reselect

Last updated

Was this helpful?