fisshy / react-scroll

React scroll component
https://github.com/fisshy/react-scroll/blob/master/README.md
MIT License
4.36k stars 436 forks source link

Cannot render on server-side #13

Closed shellka closed 9 years ago

shellka commented 9 years ago

Im trying to render application on server-side with React.renderToString method https://github.com/mhart/react-server-example and have got an error:

e:\repos\wt-site\node_modules\react-scroll\lib\mixins\cancel-events.js:6
            document.addEventListener(events[i], cancelEvent);
            ^
ReferenceError: document is not defined
    at Object.module.exports.register (e:\repos\wt-site\node_modules\react-scroll\lib\mixins\cancel-events.js:6:4)
    at Object.<anonymous> (e:\repos\wt-site\node_modules\react-scroll\lib\mixins\animate-scroll.js:11:14)
    at Module._compile (module.js:456:26)
    at Object.require.extensions.(anonymous function) [as .js] (e:\repos\wt-site\node_modules\node-jsx\index.js:26:12)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (e:\repos\wt-site\node_modules\react-scroll\lib\mixins\Helpers.js:4:21)
    at Module._compile (module.js:456:26)
fisshy commented 9 years ago

Interesting, actually never tried to render it server side. Seems like its looking for the DOM element when it does not exists. Not sure what the best solution is atm.

BerkeleyTrue commented 9 years ago

Here is a suggestion: add a check to see if DOM can be used

var canUseDOM = !!(
  typeof window !== 'undefined' &&
  window.document &&
  window.document.createElement
);

And mock document object in non-DOM environments.

shellka commented 9 years ago

yes, i did it in my fork: https://github.com/juvty/react-scroll/commit/7f6d72353c290c19a796dd3c7767f1e1d4c9dd88

fisshy commented 9 years ago

make a PR and will look at it :)

fisshy commented 9 years ago

Should work now!

danilvalov commented 7 years ago

@fisshy

Hi all.

The following option:

var canUseDOM = !!(
  typeof window !== 'undefined' &&
  window.document &&
  window.document.createElement
);

has a conflict with a GSAP option for fixing Server-Side rendering: http://greensock.com/forums/topic/7422-using-tweenlite-timelinelite-server-side-with-nodejs/

I think we need to use an another option in this library.