Reading Resources
Redux Resource provides four action types for reading resources asynchronously. They are:
Each request will always begin with an action with type READ_RESOURCES_PENDING
. Then, one of the other three action types will be used to represent the resolution of that request. Use the other action types in the following way:
READ_RESOURCES_FAILED
: Use this if the request fails for any reason. Thiscould be network errors, or any
greater than or equal to 400.
READ_RESOURCES_IDLE
: Use this when the request is aborted.READ_RESOURCES_SUCCEEDED
: Use this when the request was successful.
Request Objects
Specifying a request key on the actions will create a request object in the store for this request. This object can be used to look up the status of the request.
Although it is recommended that you specify a request key when possible, there are some situations when you may not need to when fetching resources.
When fetching a single resource, you typically provide the ID to be fetched. Therefore, request objects aren't always necessary, as you can track the request on the resource's metadata directly.
For read requests that return multiple resources, it is typically preferable to specify a request key.
Successful Reads
When an action of type READ_RESOURCES_SUCCEEDED
is dispatched, three things will happen:
the resources included in the action's
resources
will be added to theresources
section of the resource slice. Existing resources with the same ID will be merged with the new ones. To replace existing resources, rather than merge them, specifymergeResources: false
on the action.The metadata for each of the
resources
specified on the action will be updated withreadStatus: 'SUCCEEDED'
. To replace all of the existing meta, rather than merging it, specifymergeMeta: false
on the action.When a
list
is passed, the IDs from theresources
array on the action will be added to the list. You may specifymergeListIds: false
to replace the existing list instead.
Redux Resource XHR
Redux Resource XHR provides an action creator that simplifies making CRUD requests. If you'd like to build your own, then that's fine, too. The example below may help.
Example Action Creator: Reading One Resource
This example shows an action creator to read a single book. It uses the redux-thunk middleware and the library xhr for making requests.
Example Action Creator: Reading Many Resource
This example shows an action creator to read multiple books. It uses the redux-thunk middleware and the library xhr for making requests. To create a query string, it uses the querystring module.
Last updated