jsonkao / react-scrollama

Simple scrollytelling with the IntersectionObserver in React.
https://jsonkao.github.io/react-scrollama
MIT License
390 stars 30 forks source link

Eexpects window to be defined, but now always the case #9

Closed jslyonnais closed 6 years ago

jslyonnais commented 6 years ago

Webpack production build using Gatsby return WebpackError: ReferenceError: window is not defined. Should make sure window is define before loading intersection-oberver. Will also make other window usage in scrollama.js works with that mechanic.

NicholasLYang commented 6 years ago

Just clarifying, this is code executed by the browser, correct? Because Gatsby is a server side renderer, so if you're rendering on node, window won't be defined. Just want to make sure we're on the same page.

jslyonnais commented 6 years ago

Finally found a workaround. You got right about Gatsby, but I was thinking if I can find a way to make things work.

Comment for futur people, finally I've use scrollama directly from npm, and call it on the componentDidMount of react.

componentDidMount() {
    // Loading module that require "window" to be defined
    require('intersection-observer')
    const scrollama = require('scrollama')

    const scrollThreshold = 0.33
    const scroller = scrollama()
    scroller.setup({
      // debug: true,
      step: '.scroll__step',
      threshold: scrollThreshold,
      container: '.scroll__container',
      graphic: '.scroll__graphic'
    }).onStepEnter(this.handleScrollStepEnter)
  }
NicholasLYang commented 6 years ago

Great! Thanks for posting your solution.