<StaticRouter>

<Router> 从不会改变地址

当用户实际上没有点击时, 这在服务端的渲染场景中可能会非常有用, 所以这个地址从来没有改变. 因此, 称为: static (静态). 当您只需要插入一个位置并在渲染输出上作出断言时,它也可用于简单的测试 这里有一个简单 nodejs 服务 : 为[<Redirect>](Redirect.md)和其他请求的常规HTML发送302状态代码:

  1. import { createServer } from 'http'
  2. import React from 'react'
  3. import ReactDOMServer from 'react-dom/server'
  4. import { StaticRouter } from 'react-router'
  5. createServer((req, res) => {
  6. // This context object contains the results of the render
  7. const context = {}
  8. const html = ReactDOMServer.renderToString(
  9. <StaticRouter location={req.url} context={context}>
  10. <App/>
  11. </StaticRouter>
  12. )
  13. // context.url will contain the URL to redirect to if a <Redirect> was used
  14. if (context.url) {
  15. res.writeHead(302, {
  16. Location: context.url
  17. })
  18. res.end()
  19. } else {
  20. res.write(html)
  21. res.end()
  22. }
  23. }).listen(3000)

basename: string

所有地址的基本 URL . 正确格式化的基本名称应该有一个主要的斜杠,但没有尾部斜杠

  1. <StaticRouter basename="/calendar">
  2. <Link to="/today"/> // renders <a href="/calendar/today">
  3. </StaticRouter>

location: string

服务器收到的 URL, 在 node 服务上可能是 req.url

  1. <StaticRouter location={req.url}>
  2. <App/>
  3. </StaticRouter>

location: object

一个格式像 { pathname, search, hash, state } 的地址对象

  1. <StaticRouter location={{ pathname: '/bubblegum' }}>
  2. <App/>
  3. </StaticRouter>

context: object

记录渲染结果的纯JavaScript对象。 见上面的例子

children: node

要呈现的 单个子元素