使用 Node/React 的服务器端渲染。我如何获取数据?
Server-side rendering with Node/React. How do I fetch the data?
我的目标是以服务器端呈现我的 blog built with Node and React/Redux. I am following this tutorial, and using this 项目为例。
我已经加载了我的组件,创建了一个空商店,成功地使用这些东西来呈现和 return html 页面。
我剩下的挑战是弄清楚如何获取初始数据,以便我可以将其放入商店。我假设,我需要以某种方式告诉商店(在后端)分派将用数据填充它的操作。
This example is using this 函数,似乎是在告诉商店发送操作,并且 return 商店充满了数据(?)。但它飞过我的脑海,我不明白它是如何工作的,当我复制代码时,商店仍然 return 是空的。
你能帮我理解这是如何工作的吗?我需要做什么来告诉商店执行操作,这些操作将转到我的 API、获取数据并将其放入商店?
基本上 fetchComponentData
将在服务器渲染时调度操作,如文档所述
fetchComponentData
collects all the needs (need is an array of actions that are required to be dispatched before rendering the component) of components in the current route.
其中 need
是一个函数数组,return 一个 redux 动作,它定义在每个智能组件内部(与 redux store 和 react-router 连接)
比如在mern-starter
中,当你访问index route, it'll render this component and there is the need
method时,服务器渲染时会被fetchComponentData
执行。
我的目标是以服务器端呈现我的 blog built with Node and React/Redux. I am following this tutorial, and using this 项目为例。
我已经加载了我的组件,创建了一个空商店,成功地使用这些东西来呈现和 return html 页面。
我剩下的挑战是弄清楚如何获取初始数据,以便我可以将其放入商店。我假设,我需要以某种方式告诉商店(在后端)分派将用数据填充它的操作。
This example is using this 函数,似乎是在告诉商店发送操作,并且 return 商店充满了数据(?)。但它飞过我的脑海,我不明白它是如何工作的,当我复制代码时,商店仍然 return 是空的。
你能帮我理解这是如何工作的吗?我需要做什么来告诉商店执行操作,这些操作将转到我的 API、获取数据并将其放入商店?
基本上 fetchComponentData
将在服务器渲染时调度操作,如文档所述
fetchComponentData
collects all the needs (need is an array of actions that are required to be dispatched before rendering the component) of components in the current route.
其中 need
是一个函数数组,return 一个 redux 动作,它定义在每个智能组件内部(与 redux store 和 react-router 连接)
比如在mern-starter
中,当你访问index route, it'll render this component and there is the need
method时,服务器渲染时会被fetchComponentData
执行。