Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wistie opened this issue Jul 5, 2021 · 5 comments
Open

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

wistie opened this issue Jul 5, 2021 · 5 comments

Comments

@wistie
Copy link

wistie commented Jul 5, 2021

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
@antwash
Copy link

antwash commented Aug 24, 2021

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 😄

@ritmillio
Copy link

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

@MQDXL
Copy link

MQDXL commented Jul 11, 2023

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>
   )
}

@ritmillio
Copy link

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.

@MQDXL
Copy link

MQDXL commented Jul 11, 2023

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 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants