服务器端数据获取
Server side data fetching
我正在尝试设置在服务器和客户端上呈现的基本组件。该组件填充了来自 JSON API 的数据。我通过在 componentDidMount 中加载我的数据并在加载时调用 this.setState 来在客户端工作。
我遇到的问题是数据没有加载到服务器上。如何将初始数据获取到组件的服务器呈现版本中?
https://github.com/rackt/react-router/blob/latest/docs/guides/advanced/ServerRendering.md 对此很模糊:
For data loading, you can use the renderProps argument to build
whatever convention you want--like adding static load methods to your
route components, or putting data loading functions on the
routes--it's up to you.
你有任何关于如何做到这一点的例子吗?这似乎是一个通用应用程序想要做的非常基本的事情!
是的 - 我们有 https://github.com/rackt/async-props,它提供了一个库来处理服务器和客户端之间的异步数据,并提供了这样做的示例。如果您使用的是 Flux 框架,则需要进行适当的调整,但基本方法非常相似。
我正在尝试设置在服务器和客户端上呈现的基本组件。该组件填充了来自 JSON API 的数据。我通过在 componentDidMount 中加载我的数据并在加载时调用 this.setState 来在客户端工作。
我遇到的问题是数据没有加载到服务器上。如何将初始数据获取到组件的服务器呈现版本中?
https://github.com/rackt/react-router/blob/latest/docs/guides/advanced/ServerRendering.md 对此很模糊:
For data loading, you can use the renderProps argument to build whatever convention you want--like adding static load methods to your route components, or putting data loading functions on the routes--it's up to you.
你有任何关于如何做到这一点的例子吗?这似乎是一个通用应用程序想要做的非常基本的事情!
是的 - 我们有 https://github.com/rackt/async-props,它提供了一个库来处理服务器和客户端之间的异步数据,并提供了这样做的示例。如果您使用的是 Flux 框架,则需要进行适当的调整,但基本方法非常相似。