美文网首页让前端飞
react 服务端渲染图解

react 服务端渲染图解

作者: 漓漾li | 来源:发表于2018-12-17 17:23 被阅读0次
react服务端渲染原理图解.png

注解

服务端字符串化react应用
  const html = ReactDOMServer.renderToString(
    <Provider store={store}>
      <StaticRouter location={ctx.url} context={{}}>
        <App />
      </StaticRouter>
    </Provider>
  );
向html中注入store的方式
var htmlTemplate = `
  <!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"> ${content}</div>
    <script>window.__INITIAL_STATE__ =${JSON.stringify(initState)}</script>
    <script type="text/javascript" src="/static/js/main.js"></script>
</body>
</html>
`

客户端创建store时,用window.__INITIAL_STATE__初始化store即可

相关文章

网友评论

    本文标题:react 服务端渲染图解

    本文链接:https://www.haomeiwen.com/subject/npyckqtx.html