admin管理员组

文章数量:1353587

I render react ponent on server and when go to route get error message:

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

I render react ponent on server and when go to route get error message:

  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())
Share Improve this question edited Mar 22, 2017 at 13:57 Khotey Vitaliy asked Mar 22, 2017 at 13:51 Khotey VitaliyKhotey Vitaliy 5191 gold badge6 silver badges19 bronze badges 6
  • 1 Do you import/require renderToString somehow? – Dan Cantir Commented Mar 22, 2017 at 13:55
  • Yes, renderToString = ReactDOMServer.renderToString – Khotey Vitaliy Commented Mar 22, 2017 at 13:56
  • Do you have something like import ReactDOMServer from 'react-dom/server'. at the top lines of your .js file? – Dan Cantir Commented Mar 22, 2017 at 13:58
  • If I console.log ReactDOMServer.renderToString it return function – Khotey Vitaliy Commented Mar 22, 2017 at 13:59
  • Ok, and what if you try to call renderToString like this ReactDOMServer.renderToString? – Dan Cantir Commented Mar 22, 2017 at 14:00
 |  Show 1 more ment

1 Answer 1

Reset to default 8

I suggest you write it like this:

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

I hope it helps you.

本文标签: javascriptReactDOMServerrenderToString is not a functionStack Overflow