ReactDOMServer.renderToString不是函数

时间:2017-03-22 13:51:45

标签: javascript node.js reactjs react-server

我在服务器上渲染react组件,当转到路由获取错误消息时:

enter image description here

  const renderToString = ReactDOMServer.renderToString
  const fac = React.createFactory(React.createClass({
    render: function() { 
      return (
        <Provider store={store}>
          <StaticRouter location={location} context={routeContext}>
            <App />
          </StaticRouter>
        </Provider>
      )
  }}))

  const appHtml = renderToString(fac())

1 个答案:

答案 0 :(得分:4)

我建议你这样写:

const ReactDOMServer = require('react-dom/server');
const appHtml = ReactDOMServer.renderToStaticMarkup (
    <Provider store={store}>
      <StaticRouter location={location} context={routeContext}>
        <App />
      </StaticRouter>
    </Provider>
);

我希望它可以帮到你。