ReactDOMServer.renderToString 不是函数

ReactDOMServer.renderToString is not a function

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

  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())

我建议你这样写:

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

希望对你有所帮助