原文地址
上一篇 Root Container
Relay文档翻译目录
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.
当Relay在满足数据需求的过程中,知道特定事件的发生状态是很有用的。例如,我们想知道数据获取花了多长时间,如我们想向服务器端写日志。这些事件在大多数Relay API上通过onReadyStateChange
都是可以监听到的。
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:
当Relay执行数据需求时,onReadyStateChange
被调用一次或多次,包含一个描述当前”ready state”的对象。它有如下属性:
ready: boolean
This is true when the subset of data required for rendering is ready.
为真表示所需数据的子集准备完毕done: boolean
This is true when all data requirements are ready for rendering.
为真表示所有需要的数据准备完毕error: ?Error
This is an instance of
Error
if there is a failure. Otherwise, this is
null
.
如果发生错误,将有一个Error实例,负责为nullstale: boolean
When “force fetching”, this is true if
ready
is true as a result of data being available on the client before the server request has completed.
当强制从服务器端获取数据时,全部数据获取完毕之前,当ready为真时它为真。aborted: boolean
Whether the request was aborted.
请求失败被中断。
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:
如果客户端没有足够的数据,Relay向服务器端发送数据请求,将有下面的行为:
- Once with
ready
set to false. - Once with
ready
anddone
set 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:
如果客户端有足够的数据,Relay不需要向服务器端发送数据请求,将有下面的行为
- Once with
ready
anddone
set to true.
Server Error
If a server request results in a failure to load data, we can expect the following behavior:
如果获取数据的服务器端请求失败,将有下面行为:
- Once with
ready
set to false. - Once with
error
set to anError
object.
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:
当强制从服务器端获取数据,并且客户端数据不足时,与Fetching Data from the Server的情况相同。如果强制从服务器端获取数据,客户端数据已经满足的情况下,将有下面行为:
- Once with
ready
,done
, andstale
set to true. - Once with
ready
anddone
set to true, butstale
set to false.