采纳答案成功!
向帮助你的同学说点啥吧!感谢那些助人为乐的人
老师 react-router-dom 6中貌似没有StaticRouter,看的 node_modules下react-router-dom 的 index.d.ts , 个人觉得react-router-dom v4 有点老了。
偶然翻到了 react-router-dom 文档中的SSR Render, 有几个改动
"react": "^17.0.2", "react-dom": "^17.0.2", "react-router-dom": "^6.2.2",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^6.2.2",
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>
'/hi'
element={<Hello />}></Route>
</Routes>
}
export
default
IRoutes;
demo 地址: https://stackblitz.com/github/remix-run/react-router/tree/main/examples/ssr?file=src%2FApp.tsx
登录后可查看更多问答,登录/注册
从零开始,带你搭建React SSR框架,从根本上解决客户端渲染问题
1.1k 11
677 10
1.9k 9
1.1k 9
6.5k 7
购课补贴联系客服咨询优惠详情
慕课网APP您的移动学习伙伴
扫描二维码关注慕课网微信公众号