getStatus(state, statusLocation, [treatNullAsPending])
Returns an object with boolean values representing the request status of a particular CRUD action. 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.[
treatNullAsPending
] (Boolean): Whether or not a request status ofNULL
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:
{
null: Boolean,
pending: Boolean,
failed: Boolean,
succeeded: Boolean
}
One of these values is always true
, reflecting the value of the request
status. When treatNullAsPending
is true
, then request statuses that are
"NULL"
will be returned as pending: true
.
Notes
Passing more than one status location will aggregate the statuses. The rules of aggregation work as follows:
- If all of the requests are null, then the aggregate is null
- If any of the requests are failed, then the aggregate is failed.
- If no requests have failed, but some are pending, then the aggregate is pending.
- If all requests have succeeded, then the aggregate has 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
treatNullAsPending
also works when aggregating.
Examples
In this example, we pass a single status location:
import { getStatus } from 'redux-resource';
import store from './store';
const state = store.getState();
const bookDeleteStatus = getStatus(state, 'books.meta[23].deleteStatus');
In this example, we pass two locations:
import { getStatus } from 'redux-resource';
import store from './store';
const state = store.getState();
const bookReadStatus = getStatus(
state,
[
'articles.meta[23].readStatus',
'comments.requests.detailsRead.status'
],
true
);
Tips
The third argument,
treatNullAsPending
, 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 beNULL
. PassingtreatNullAsPending
will consider theseNULL
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 ascomponentWillReceiveProps
, 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 an upcoming status.