javascript - ReactDOMServer.renderToString is not a function - Stack Overflow

I render react ponent on server and when go to route get error message: const renderToString = ReactDOM

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743934945a4532467.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信