Javascript 服务器端呈现-renderToString返回空html
我正在尝试在服务器上呈现react应用程序,但renderToString函数似乎无法正确呈现标记,当我执行console.log(标记)时,我得到如下结果:Javascript 服务器端呈现-renderToString返回空html,javascript,reactjs,redux,react-router,server-side-rendering,Javascript,Reactjs,Redux,React Router,Server Side Rendering,我正在尝试在服务器上呈现react应用程序,但renderToString函数似乎无法正确呈现标记,当我执行console.log(标记)时,我得到如下结果: <div data-reactroot="" data-reactid="1" data-react-checksum="-1424217736"> <!-- react-empty: 2 --><!-- react-empty: 3 --><!-- react-empty: 4 -->&l
<div data-reactroot="" data-reactid="1" data-react-checksum="-1424217736">
<!-- react-empty: 2 --><!-- react-empty: 3 --><!-- react-empty: 4 --><!-- react-empty: 5 --></div>
我的代码如下所示:
app.use('*', (req, res) => {
const context = {}
const store = configureStore()
const markup = renderToString(
<Provider store={store}>
<StaticRouter
location={req.url}
context={context}
>
<Routes/>
</StaticRouter>
</Provider>
)
console.log(markup)
if (context.url) {
// Somewhere a `<Redirect>` was rendered
redirect(301, context.url)
} else {
// we're good, send the response
res.send(`
<!doctype html>
<head>
<meta name="google-site-verification" content="P5pcBDBR0SrpH4kFMOp5Ush5JxDdG-3oPhjKO8Zf5WE" />
</head>
<body>
<div id="app">${markup}</div>
</body>
`)
}
})
app.use(“*”,(req,res)=>{
常量上下文={}
const store=configureStore()
常量标记=renderToString(
)
console.log(标记)
if(context.url){
//某处出现了一个``字
重定向(301,context.url)
}否则{
//我们很好,请回复
res.send(`
${markup}
`)
}
})
我使用的是React路由器v4我必须查看您的路由器,但看起来不错。这些空注释是未呈现的路由的占位符。下面是我的路由函数:export const routes=()=>(),并在顶部像这样导入:const{routes}=require('../client/routes'),您使用什么路径访问应用程序?你要做类似
http://localhost/
?如果是这样,您没有path=“/”
的路由设置,也不会渲染任何路由。