react icon indicating copy to clipboard operation
react copied to clipboard

ReactDOMServer.renderToString: presence of onClick handler causes errors on async update

Open denvned opened this issue 11 years ago • 12 comments

The code

class Comp extends React.Component {
  componentWillMount() {
    setTimeout(() => {
      this.setState({});
    });
  }

  render() {
    return <div onClick={() => {}} />;
  }
}

ReactDOMServer.renderToString(<Comp />);

produces errors:

Warning: React can't find the root component node for data-reactid value .0. If you're seeing this message, it probably means that you've loaded two copies of React on the page. At this time, only a single copy of React can be loaded at a time.

Uncaught TypeError: Cannot read property 'firstChild' of undefined

But if I remove onClick handler the errors go away...

See demo: http://codepen.io/dened/pen/YyByYr

denvned avatar Nov 15 '15 17:11 denvned

This line tries to get a node that is not mounted: https://github.com/facebook/react/blob/eb056fcf5d12f100e4f8df700b634d3c4cc5f9ba/src/renderers/dom/client/eventPlugins/SimpleEventPlugin.js#L586

denvned avatar Nov 15 '15 17:11 denvned

cc @spicyj and @syranide because: events.

actually, calling setState on SSR might not be legal/valid, is it?

jimfb avatar Nov 15 '15 20:11 jimfb

Yeah, I suppose setState should warn and do nothing. What did you expect would happen though?

sophiebits avatar Nov 15 '15 20:11 sophiebits

Not only setState triggers this error. For example, forceUpdate also produces this.

I expect asynchronous updates to be ignored (with a warning) on components rendered using ReactDOMServer.renderToString.

denvned avatar Nov 15 '15 20:11 denvned

I also ran into this error when trying to update a codebase to make use of server-side rendering. There was a counter using setInterval initialised in the constructor for some reason that ended up trying to call setState.

I had trouble tracking the problem down as the error is pretty confusing in this case. Some sort of warning when setState is called would certainly help pinpoint the actual problem. I feel like it's a relatively easy mistake to make for those of us new to SSR.

dominicenglish avatar Jan 15 '16 02:01 dominicenglish

In dev mode, we could mark a component as having been rendered using SSR. If setState or forceUpdate is ever called on such a component, print an intelligent warning.

jimfb avatar Jan 15 '16 02:01 jimfb

Hello, first attempts to dive into react sources here :) I took a look at similar issues but I still wonder if it's 'react-enough' way. I'm concerned about if (this._reactInternalInstance && this._reactInternalInstance._isServerSideRendered) condition in particular. Am I on the right track or there is more convenient way to mark & check it? Thanks.

mdolbin avatar Jan 18 '16 10:01 mdolbin

@mdolbin That looks reasonable to me.

jimfb avatar Jan 18 '16 20:01 jimfb

Looks like this is up for grabs again as #5879 did not progress. Some info on how to implement it: https://github.com/facebook/react/pull/5879#issuecomment-216425382. I updated the fiddle demonstrating the issue (the error message is different in 15.x but the cause is the same): http://codepen.io/gaearon/pen/jryXOj

gaearon avatar Jun 26 '16 18:06 gaearon

I think I found a way to use ReactNoopUpdateQueue in ReactServerRenderingTransaction. That way we can do as @spicyj advised. I send you a PR as soon as possible, maybe tonight, maybe tomorrow!

rricard avatar Jun 26 '16 22:06 rricard

Okay, trick question: how can I let the setState go through in componentWillMount if I use ReactNoopUpdateQueue ?

My change, for instance, make fail https://github.com/facebook/react/blob/master/src/renderers/dom/server/tests/ReactServerRendering-test.js#L383

rricard avatar Jun 27 '16 07:06 rricard

Answering my own question: maybe during initial mount, don't use (yet) the ReactNoopUpdateQueue.

rricard avatar Jun 27 '16 07:06 rricard