react-modal icon indicating copy to clipboard operation
react-modal copied to clipboard

Incompatible with server-side rendering

Open misuba opened this issue 11 years ago • 12 comments

A source tree with react-modal in it, running in Node, yields the following:

/node_modules/react-modal/lib/components/Modal.js:18
    appElement: React.PropTypes.instanceOf(HTMLElement),
                                           ^
ReferenceError: HTMLElement is not defined

...and halts entirely.

How would you feel about making this propType a little more lenient (object, perhaps)?

misuba avatar Jan 06 '15 21:01 misuba

How about including this into your app?

HTMLElement = typeof HTMLElement === 'undefined' ? function(){} : HTMLElement;
var appElement = ENV.CLIENT ?
  document.getElementById('appElement') :
  new HTMLElement();

ryanflorence avatar Jan 06 '15 22:01 ryanflorence

I could do that (plus shim document.body.etc - HTMLElement is just the first choke point), but I'd have to run a hacked version of this package rather than just put that in my app, and I'd prefer not to do that.

I could also possibly just require react-modal on the client-side and pass either it or a server-side alternative element all the way down my app hierarchy, but... come on. Plenty of people are running React trees in Node; it'd be nice to have a modal library that didn't require all that ducking and weaving.

misuba avatar Jan 06 '15 23:01 misuba

I had a little peek at wrapping the offending bits in ExecutionEnvironment.canUseDOM but I could not really think up a good way to do portals without the dom.

nelix avatar Jan 19 '15 00:01 nelix

Could you post a gist? I'm pretty sure I don't need the things to work on the server... I just need them to not crash the server.

misuba avatar Jan 19 '15 17:01 misuba

@misuba this is crude and untested, just designed to give you the idea, https://gist.github.com/nelix/24dd21b12d4b005880aa

you could also not mess with react-modal at all and just render null instead if the dom is not available, but I think we could come up with a server render alternative that mostly works instead of these hacks

nelix avatar Jan 20 '15 12:01 nelix

With the @nelix 's gist augmented with the @ryanflorence 's workaround (HTMLElement = typeof HTMLElement === 'undefined' ? function(){} : HTMLElement;), it doesn't crash on server and work well in the browser.

Can we considered the issue as solved and make a new release ? :)

ncuillery avatar Feb 19 '15 11:02 ncuillery

:+1:

cmeiller avatar Feb 19 '15 11:02 cmeiller

@ncuillery :+1:

fdubost avatar Feb 19 '15 11:02 fdubost

@ncuillery :+1:

oziks avatar Feb 19 '15 13:02 oziks

I wonder if the work from M6Web/react-modal#1 could be merged into this repository? I've just tried it and it works fine when rendering React on server for me.

XeeD avatar Apr 02 '15 15:04 XeeD

Yes, something similar has already been done here by @misuba : #22

ncuillery avatar Apr 02 '15 16:04 ncuillery

Resolved with #22

mzabriskie avatar May 08 '15 21:05 mzabriskie