-
Notifications
You must be signed in to change notification settings - Fork 290
Example redux modules
There're a bunch of redux modules already on the example app. Here's an explanation of each of them.
The entities module handles normalization through the normalizr library. The middleware does 3 things:
- intercepts
actionsdispatched withmeta.entitiesproperty; - normalizes the action
payloadproperty based on the value ofmeta.entities; - dispatches an
ENTITIES_RECEIVEaction with normalized entities, which will be handled by the entities reducer.
To be able to have an entity normalized, you need to do 3 things:
-
add
meta.entitiesto the success action:const resourceCreateSuccess = (resource, detail) => ({ type: RESOURCE_CREATE_SUCCESS, payload: detail, + meta: { + entities: resource, + }, }) -
create
resourceschema onstore/entities/schemas.jsexport const resource = new schema.Entity('resource')
-
on containers, use
selectorsfrom the entities store instead of the resource one:- import { fromResource } from 'store/selectors' + import { fromResource, fromEntities } from 'store/selectors' ... const mapStateToProps = state => ({ - list: fromResource.getList(state, 'resource'), + list: fromEntities.getList(state, 'resource', fromResource.getList(state, 'resource')), })
TODO
TODO
The resource has generic CRUD operations within redux.
Example action:
store.dispatch(resourceCreateRequest('posts', { title: 'foo' }))
^ resource endpointThe first parameter (called resource) is important as it will be used both to call the API and to combine with other modules such as entities:
// The resulting action
{
type: RESOURCE_CREATE_REQUEST,
payload: {
data: {
title: 'foo',
},
},
meta: {
resource: 'posts',
thunk: 'postsCreate',
},
}
// The resulting API call (inside saga)
const detail = yield call(api.post, '/posts', { title: 'foo' })TODO
Special thanks to @kybarg and @protoEvangelion for helping to write this Wiki. Please, feel free to edit/create pages if you think it might be useful (also, see #33)