Jump to content
Search Community

React GSAP ref.current undefined inside onUpdate

timduncan test
Moderator Tag

Recommended Posts

Hello, I am quite stuck with something and could really use a pair of extra eyes for the following code: 

 

gsap.to(wrapperRef.current, {
  scrollTrigger: {
    trigger: wrapperRef.current,
    pin: imgWrapRef.current,
    pinSpacing: true,
    start: "top top",
    end: "bottom bottom",
    scrub: true,
    markers: false,
    onUpdate: ({ progress }) => {
      if (progress > 0.5) {
        console.log(imgWrapRef.current)
        document.querySelector('img').setAttribute("data-hide", "true")
      } else {
        document.querySelector('img').setAttribute("data-hide", "false")
      }
    }
  }
})

 

and I can feel its so simple, but still can't understand why my console.log returns null. so basically I am in another scope and onUpdate doesn't know what's my ref.
does anyone have a suggestion of what's the fix? 
thanks a lot in advance. 

 

 

Link to comment
Share on other sites

It's pretty tough to troubleshoot without a minimal demo. Would you please provide a very simple CodePen or CodeSandbox that demonstrates the issue? 

 

Scope has nothing to do with it from what I can tell from your excerpt. Did you console.log() the same thing right above where you create the tween? Are you positive it's defined at any point? It could also be that in your React app, React removed the element and that's why current is null at that point. It's just impossible to tell by glancing at that one very small piece of the overall context. 

 

I'm sure it'll be much easier to diagnose one we see a minimal demo👍

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

 

Here's a React starter template.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...