1.简介
服务器上的渲染有些不同,因为它们都是无状态的。
基本思想是,我们将应用包装在无状态<StaticRouter>
而不是有状态的<BrowserRouter>
。
我们从服务器传入请求的url,以便路由可以匹配,然后context
我们将讨论一个prop。
// client
<BrowserRouter>
<App/>
</BrowserRouter>
// server (not the complete story)
<StaticRouter
location={req.url}
context={context}
>
<App/>
</StaticRouter>
当您在客户端上呈现<Redirect>
时,浏览器历史记录会更改状态,我们会看到新屏幕。
在静态服务器环境中,我们无法更改应用程序状态。
相反,我们使用context
道具来找出渲染的结果。如果找到context.url
,则说明该应用已重定向。<