【问题标题】:Node/react how to return pure html rendered with react and send to clientNode / react如何返回用react渲染的纯html并发送到客户端
【发布时间】:2018-08-04 02:03:18
【问题描述】:

我有一个简单的 node.js 应用程序,它应该使用 React 的 renderToString 函数以纯 HTML 响应请求。渲染和响应的代码如下所示:

const markup = renderToString(
    <Provider {...stores}>
        <Router location={req.url} context={{}} history={browserHistory}>
            <Routes/>
        </Router>
    </Provider>
);

res.status(200).send(markup);

以及 HTML 响应:

<section data-reactroot="">
    <h1>Header</h1>
    <div>Body</div>
</section>

我不希望输出中有任何反应痕迹。有没有办法在使用 react 渲染时不包含 data-reactroot 或任何其他 react 痕迹?

【问题讨论】:

    标签: javascript node.js reactjs render-to-string


    【解决方案1】:

    你在使用 React 16 吗?使用该版本后,none of the generated React attributes should be present

    【讨论】:

      【解决方案2】:

      只需使用renderToStaticMarkup 而不是renderToString,输出中不会有任何反应痕迹(参考https://reactjs.org/docs/react-dom-server.html#rendertostaticmarkup

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-15
        • 2019-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-18
        • 2019-11-14
        • 2015-11-19
        相关资源
        最近更新 更多