getStatus
Returns an object with boolean values representing the request status of a particular CRUD operation. It can also be used to aggregate multiple request statuses together.
Arguments
state
(Object): Typically, the current state of the Redux store, but more generally it can be any object that has a request status somewhere deeply nested within it.statusLocation
(String|Array): A single path that points to a request status withinstate
. If you pass an array of status locations, then they will be aggregated. For more on status locations and status aggregation, see the Notes below.[
treatIdleAsPending
] (Boolean): Whether or not a request status ofIDLE
is to be considered as apending
request. Defaults tofalse
. See Tips on when to use this.
Returns
(Object
): An Object representing the status of this request for the statusLocation. It has the following shape:
Only one of these values is always true
, reflecting the value of the request status. When treatIdleAsPending
is true
, then request statuses that are "IDLE"
will be returned as pending: true
.
Notes
Passing an array of status locations as the second argument will aggregate the statuses. The aggregation works as follows:
If all of the requests are idle, then the aggregate status is idle
If any of the requests are failed, then the aggregate status is failed.
If no requests have failed, but some are pending, then the aggregate status is pending.
If all requests have succeeded, then the aggregate status is succeeded.
A status location is a string that specifies a location of a request status in your state tree. For instance
"books.meta.24.readStatus"
or"books.requests.dashboardSearch.status"
.
Keep in mind that
treatIdleAsPending
also works when aggregating.
Examples
In this example, we pass a single status location:
In this example, we pass two locations:
Tips
The third argument,
treatIdleAsPending
, is useful for requests that are made when your components mount. The components will often render before the request begins, so the status of these requests will beIDLE
. PassingtreatIdleAsPending
will consider theseIDLE
states aspending: true
.If you're using React, we recommend computing your
getStatus
values inmapStateToProps
, and then passing them in as props into your component. That way, you have access to this information in all of the lifecycle methods of your component.The first argument,
state
, doesn't always need to be the state of your Redux store. For instance, if you're using this method within your component's lifecycle methods, such ascomponentDidUpdate
, you may instead pass it an object that is a subset of the state. This can be useful when you're comparing a previous status against the current status.
Last updated