Ready State
Whenever Relay is fulfilling data requirements, it can be useful to know when certain events occur. For example, we might want to record how long it takes for data to be available, or we might want to log errors to the server. These events are available on most Relay APIs via the onReadyStateChange callback.
onReadyStateChange
When Relay fulfills data, the onReadyStateChange callback is called one or more times with an object that describes the current "ready state". This object has the following properties:
ready: booleanThis is true when the subset of data required for rendering is ready.
done: booleanThis is true when all data requirements are ready for rendering.
error: ?ErrorThis is an instance of
Errorif there is a failure. Otherwise, this isnull.events: Array<ReadyStateEvent>This is an array of events received so far (see
ReadyStateEventbelow).stale: booleanWhen "force fetching", this is true if
readyis true as a result of data being available on the client before the server request has completed.aborted: booleanWhether the request was aborted.
ReadyStateEvent
ABORTCACHE_RESTORED_REQUIREDCACHE_RESTORE_FAILEDCACHE_RESTORE_STARTNETWORK_QUERY_ERRORNETWORK_QUERY_RECEIVED_ALLNETWORK_QUERY_RECEIVED_REQUIREDNETWORK_QUERY_STARTSTORE_FOUND_ALLSTORE_FOUND_REQUIRED
Examples
Fetching Data from the Server
If insufficient data on the client leads Relay to send a server request for more data, we can expect the following behavior:
- Once with
readyset to false. - Once with
readyanddoneset to true.
Resolving Data from the Client
If sufficient data is available on the client such that Relay does not need to send a server request, we can expect the following behavior:
- Once with
readyanddoneset to true.
Server Error
If a server request results in a failure to load data, we can expect the following behavior:
- Once with
readyset to false. - Once with
errorset to anErrorobject.
Note that ready and done will continue to be false.
Force Fetching with Data from the Client
If a "force fetch" occurs and there is insufficient data on the client, the same behavior as Fetching Data from the Server can be expected. However, if a "force fetch" occurs and there is sufficient data on the client to render, we can expect the following behavior:
- Once with
ready,done, andstaleset to true. - Once with
readyanddoneset to true, butstaleset to false.