React Router之服务端渲染

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,则说明该应用已重定向。<

猜你喜欢

转载自blog.csdn.net/qq_27868061/article/details/112725359