scrollreveal icon indicating copy to clipboard operation
scrollreveal copied to clipboard

ScrollReveal not working with Next JS (create-next-app) ?

Open wistie opened this issue 3 years ago • 5 comments

I have some issues using ScrollReveal with create-next-app.

I installed ScrollReveal with npm, then imported it using ES6 :

import sr from 'scrollreveal';

I immediatly have an error message :

Server Error ReferenceError: document is not defined

I use ScrollReveal with create-react-app, and it works perfectly. So I don't understand.

Thanks.

Environment

  • Operating System: Linux Ubuntu / Windows 10 Pro
  • Browser Version: Chrome 90.0.4430.72
  • ScrollReveal Version: 4.0.9

wistie avatar Jul 05 '21 20:07 wistie

This issue is happening because the DOM is only available inside the browser client side and not server side. Nextjs is executing your code server side. To get around the error you can leverage nextjs dynamic imports feature and a react useEffect hook. This will ensure the DOM is available before importing scrollreveal

const MyComponent = () => { 
 const refToComponent = React.useRef(null)

  useEffect(() => {
    async function animate() {
      if (refToComponent.current) {
        const sr = (await import("scrollreveal")).default
        sr().reveal(refToComponent.current)
      }
    }
    animate()
  }, [])

   return (
     <div ref={refToComponent}>HEY THERE!</div>
   )
}

Hope this helps 😄

antwash avatar Aug 24 '21 13:08 antwash

If anyone facing the same issue, I have created a helper package to use Scrollreveal with Next.js. You can check out here.

ritmillio avatar Sep 16 '22 08:09 ritmillio

use sr().reveal(refToComponent.current, {reset: true}), scroll back to the revealed element, the animation can't animate again, Do you have a solution to solve the bug?

const MyComponent = () => { 
 const refToComponent = React.useRef(null)

  useEffect(() => {
    async function animate() {
      if (refToComponent.current) {
        const sr = (await import("scrollreveal")).default
        sr().reveal(refToComponent.current, {reset: true})
      }
    }
    animate()
  }, [])

   return (
     <div ref={refToComponent}>HEY THERE!</div>
   )
}

MQDXL avatar Jul 11 '23 02:07 MQDXL

Hey @MQDXL , due to the nature of Next13/React 18 (Now React is a server side lib) -> you cannot grab elements from the DOM without specifying the 'use client' at the top of the component/page. Also rather than this implementation you may consider next-reveal: https://github.com/ritmillio/next-reveal which provides you with a RevealWrapper for animating single elements and a RevealList to animate multiple items in a sequence.

ritmillio avatar Jul 11 '23 06:07 ritmillio

Hey @MQDXL , due to the nature of Next13/React 18 (Now React is a server side lib) -> you cannot grab elements from the DOM without specifying the 'use client' at the top of the component/page. Also rather than this implementation you may consider next-reveal: https://github.com/ritmillio/next-reveal which provides you with a RevealWrapper for animating single elements and a RevealList to animate multiple items in a sequence.

@ritmillio thanks bro, I found my code error.that's the element's parent set height: 100%. say thanks to you again .

MQDXL avatar Jul 11 '23 06:07 MQDXL