Skip to content

configureCrudReducer

Fausto NA edited this page Jul 20, 2017 · 2 revisions

The configureCrudReducer builder is used to export the default crudReducer:

export const defaultExtractors = {
  index: action => action.payload,
  meta: () => ({}),
  error: action => action.payload,
  single: action => action.payload,
}

export const crudReducer = configureCrudReducer(defaultExtractors)

You can use it in your application's code to create CRUD reducers that properly consume the data returned by your back-end. For example, imagine you call GET on your /books endpoint, expecting a server response with all available books in the following JSON format:

{
  "data": [{ "id": 0, "title": "The Stranger" }, { "id": 1, "title": "The Plague" }],
  "errors": [],
  "meta_data": { "page": 1, "total_pages": 1 },
}

You can build your own crudReducer that takes data in that format like this:

export const myCrudReducer = configureCrudReducer({
  index: action => action.payload.data,
  errors: action => action.payload.errors,
  meta: action => action.payload.meta_data,
  single: action => action.payload.data, // This wouldn't be used on a fetchIndex dispatch
})

Use this to create your own CRUD reducers that know how to talk to the different APIs your application uses. Here's documentation on the functionality that the generated reducer implements.

Clone this wiki locally