minutemailer / react-popup

React popup component
http://minutemailer.github.io/react-popup/
MIT License
208 stars 71 forks source link

Not working with SSR #42

Closed casoetan closed 6 years ago

casoetan commented 6 years ago

Trying to use this with a server render react app, but it blows up.

The problem seems to be with keymaster. Anyway to turn that off?

node_modules/keymaster/keymaster.js:267
  addEvent(document, 'keydown', function(event) { dispatch(event) }); // Passing _scope to a callback to ensure it remains the same by execution. Fixes #48

ReferenceError: document is not defined
    at node_modules/keymaster/keymaster.js:267:12
k1tzu commented 6 years ago

Ran into same problem just now. Looks like the way to solve this is to re-render like this https://codepen.io/bastianalbers/pen/PWBYvz

casoetan commented 6 years ago

Thanks @k1tzu But here is how I solved this with dynamic imports. First

let PopupComponent
import('react-popup').then(module => {
  PopupComponent = module.default
})

Then this in your render or wherever needed

<>
{PopupComponent && <PopupComponent />}
{PopupComponent && PopupComponent.alert('Whatever you want')}
</>