请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

react-router-dom 6 没有StaticRouter

老师 react-router-dom 6中貌似没有StaticRouter,看的 node_modules下react-router-dom 的 index.d.ts , 个人觉得react-router-dom v4 有点老了。
图片描述

正在回答 回答被采纳积分+3

插入代码

1回答

提问者 给生活一个大大的傻笑 2022-03-29 08:42:32

偶然翻到了 react-router-dom 文档中的SSR Render, 有几个改动

    "react": "^17.0.2",

    "react-dom": "^17.0.2",

    "react-router-dom": "^6.2.2",

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { StaticRouter } from 'react-router-dom/server'// 导入需要从 /server 中导入
 
 const content = renderToString(
    <StaticRouter location={req.path} >
      <IRoutes />  // 这里
    </StaticRouter>
  )
   
   
//////////////////////////////////////////////
// Routes.js  
// react-router-dom 6 中有一些改动,  components => element , Switch => Routes
import React from 'react';
import { Routes, Route } from 'react-router-dom'
import Hello from './containers/Hello';
import Home from './containers/Home';
 
function IRoutes ( ) {
  return <Routes>
    <Route exact path='/' element={<Home />}></Route>
    <Route exact path='/hi' element={<Hello />}></Route>
  </Routes>
}
 
export default IRoutes;


4 回复 有任何疑惑可以回复我~
  • demo 地址: https://stackblitz.com/github/remix-run/react-router/tree/main/examples/ssr?file=src%2FApp.tsx
    回复 有任何疑惑可以回复我~ 2022-03-29 08:42:57
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号