redux-anon-reducer
Version:
A simple anonymous reducer for redux
57 lines (41 loc) • 1.83 kB
Markdown
A simple anonymous function reducer for use with redux
`npm install --save-dev redux-anon-reducer`
To use `redux-anon-reducer`, your actions must follow the [Flux Standard Action format](https://github.com/redux-utilities/flux-standard-action)
Create an actionMap, with keys of your action types corresponding to the relevant state updater (functions inside what is normally the reducer file)
```
import * as actiontypes from './actiontypes';
import * as stateupdaters from './stateupdaters';
const actionMap = {
[]: stateupdaters.loadQuiz,
[]: stateupdaters.generateQuiz,
[]: stateupdaters.setAnswer,
[]: stateupdaters.submitAnswers,
[]: stateupdaters.getLeaderboards,
[]: stateupdaters.getValidQuizCodes,
[]: stateupdaters.getValidQuizOptions
}
```
In your store.js, create the reducer
```
import anonReducer from 'redux-anon-reducer';
import actionmap from './actionmap';
const reducer = anonReducer(actionmap);
```
In the case of sliced reducers, you can pass in an initial return state.
```
import { combineReducers } from 'redux';
const initialReturn = ...
const singleReducer = anonReducer(actionmap, initialReturn);
const reducer = combineReducers({ singleReducer })
```
In the case where the reducers correspond exactly to properties of your inital state, we have provided a helper.
```
import { combineReducers } from 'redux';
import { anonReducersFromInitialState } from 'redux-anon-reducer';
import actionmaps from './actionmaps';
const initialState = ...
const reducer = combineReducers(anonReducersFromInitialState(actionmaps, initialState))
```