Giter Club home page Giter Club logo

Comments (5)

antwash avatar antwash commented on June 5, 2024 23

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 😄

from scrollreveal.

ritmillio avatar ritmillio commented on June 5, 2024 3

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

from scrollreveal.

ritmillio avatar ritmillio commented on June 5, 2024 2

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.

from scrollreveal.

MQDXL avatar MQDXL commented on June 5, 2024

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

from scrollreveal.

MQDXL avatar MQDXL commented on June 5, 2024

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 .

from scrollreveal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.