Using Request Statuses
When request action types are dispatched, Redux Resource will store information about those requests in the store. This guide will cover how you can use those statuses in your view layer.
Note: these examples are React components using react-redux. Keep in mind that nothing in Redux Resource requires React: if you're using Redux with any other view layer, then this library will work just as well.

getStatus

One of the exports of this library is getStatus. This function facilitates using Redux Resource request statuses to build your interfaces. It will likely be one of the Redux Resource functions that you rely on the most.
Let's look at an example. Let's say we have a page that displays details about a book. We might write the following component:
1
import React, { Component } from 'react';
2
import { connect } from 'react-redux';
3
import { getStatus } from 'redux-resource';
4
5
export class BookDetails extends Component {
6
render() {
7
const { book, readStatus } = this.props;
8
9
return (
10
<div>
11
{readStatus.pending && 'Loading...'}
12
{readStatus.failed && 'There was an error.'}
13
{readStatus.succeeded && (
14
<div>
15
<span>
16
{book.title} ({book.id})
17
</span>
18
<span>
19
{book.releaseYear}, {book.author}
20
</span>
21
</div>
22
)}
23
</div>
24
);
25
}
26
}
27
28
function mapStateToProps(state, props) {
29
// A user can pass a `bookId` into this Component to view the book's data
30
const bookId = props.bookId;
31
const readStatus = getStatus(state, `books.meta[${bookId}].readStatus`, true);
32
const book = state.books.resources[book.id];
33
34
return {
35
book,
36
readStatus
37
};
38
}
39
40
export default connect(mapStateToProps)(BookDetails);
Copied!
You can see how the object returned from getStatus makes a render function very expressive. It's also convenient that there aren't any checks for existence here, even though our data is nested in our store: the API of Redux Resource provides you with very predictable data.

Aggregating Statuses

Often times, the data displayed on a single page comes from multiple sources. Whenever possible, we recommend using multiple getStatus calls in these situations, so that you can display information to the user as it becomes available. This way, if one endpoint is slow, or if the request fails entirely, the rest of the interface isn't affected by it.
With that said, we know this isn't always possible. Sometimes, you simply do need to wait for multiple requests to resolve before there is anything useful to show on the page.
You can use getStatus to aggregate these calls together into status. The API for this is as follows:
1
import { getStatus } from 'redux-resource';
2
import store from './get-store';
3
4
const state = store.getState();
5
6
const aggregatedStatus = getStatus(state, [
7
'books.meta.23.readStatus',
8
'books.requests.getBookComments.status'
9
], true);
Copied!
The rules of aggregation work as follows:
  • If any status is failed: true, then the group is failed: true.
  • If no status is failed: true, but at least one is pending: true, then the
    group is pending: true.
  • If all statuses are succeeded: true, then the group is
    succeeded: true.
At most, only one of these values will ever be true.
If treatIdleAsPending (the third argument, see below) is false, then all three values will be false if all of the request statuses in the state tree are "IDLE".

treatIdleAsPending

The third argument to getStatus is a Boolean called treatIdleAsPending. It determines whether a request status of "IDLE" will count as pending or not.
Consider an interface that loads a particular book when the page loads. Right at page load, there will always be a short moment when the request hasn't begun, yet your store has been set up. At this moment, the request status for this read will have a value of "IDLE".
If you don't pass true, then there will be a "flash of no content" unless you explicitly check for the "IDLE" status yourself. To avoid this, pass treatIdleAsPending as true, and getStatus will instead consider that to be a pending state.
The default value of treatIdleAsPending is false.

The Rule of Thumb

There is a rule of thumb for using treatIdleAsPending:
  • For requests that happen when the page loads, pass treatIdleAsPending as true
  • For requests that happen as a response to a user's action (such as clicking a
    button), pass treatIdleAsPending as false