收到 setVariables 回调后的 Props 值

Props value after receiving setVariables callback

我对以下场景有疑问: 如果您使用中继道具中的 setVariables 方法并更改变量,则该中继必须从服务器检索其他数据(例如,在以下游乐场示例中单击 "load more stories" relay playgorund).

我希望在收听具有值 ready = truedone = true 的回调 readyState 之后,道具包含新获取的信息。就像在操场上的例子一样,在将 numStoriesToLoad 的初始值 3 增加 1 之后,prop viewer.storyFeed.edges.length 是 4.

实际发生的是,即使在收到命名回调后,道具仍然具有旧值。如果此行为是故意的,您如何链接 setVariables 依赖于先前调用结果的调用?

setVariables 不会立即更新变量。

它实际上进入了挂起状态,并将继续返回旧变量,直到 props 被新数据填充。

如果您想了解其处理方式,请查看 RelayContainer 中的 _runVariables 函数。似乎在 ReadyStateCallback 被调用时,新的道具还没有被填充。

来源: https://github.com/facebook/relay/blob/0ba7d8c1cf2899ec2ac3e779ac7dedbff5f9d092/src/container/RelayContainer.js#L242-L322

也许你可以在 componentWillReceiveProps 中解决这个问题?

编辑:刚刚发现:

https://github.com/facebook/relay/blob/0ba7d8c1cf2899ec2ac3e779ac7dedbff5f9d092/docs/APIReference-Container.md#example-5

setVariables does not immediately mutate variables, but creates a pending state transition. variables will continue returning the previous values until this.props has been populated with data that fulfills the new variable values.