type-to-reducer
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "type-to-reducer", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Create reducer functions based on an object keyed by action types", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
# type-to-reducer | ||
Create reducer functions based on an object keyed by action types | ||
## Why? | ||
Pretty much the same as the `handleActions` function in https://github.com/acdlite/redux-actions | ||
Differences being that this only exposes the one function and allows nesting - API suggested by https://github.com/pburtchaell/redux-promise-middleware/issues/35#issuecomment-164650859 | ||
## Usage | ||
`npm install type-to-reducer --save` | ||
```js | ||
import { typeToReducer } from 'type-to-reducer' | ||
import { GET, UPDATE } from 'app/actions/foo' | ||
const initialState = { | ||
data: null, | ||
isLoading: false, | ||
error: false, | ||
} | ||
export const fooReducers = typeToReducer({ | ||
[GET]: (state, action) => ({ | ||
...state, | ||
data: getFoo(action), | ||
}), | ||
[UPDATE]: (state, action) => ({ | ||
...state, | ||
data: getFoo(action), | ||
}), | ||
}, initialState) | ||
``` | ||
Can also be used to group reducers by prefix. | ||
```js | ||
import { typeToReducer } from 'type-to-reducer' | ||
import { API_FETCH } from 'app/actions/bar' | ||
const initialState = { | ||
data: null, | ||
isLoading: false, | ||
error: false, | ||
} | ||
export const barReducers = typeToReducer({ | ||
[ API_FETCH ]: { | ||
PENDING: () => ({ | ||
...initialState, | ||
isPending: true, | ||
}), | ||
REJECTED: (state, action) => ({ | ||
...initialState, | ||
error: action.payload, | ||
}), | ||
FULFILLED: (state, action) => ({ | ||
...initialState, | ||
data: getBar(action), | ||
}), | ||
}, | ||
}, initialState) | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5644
71
5