Jump to content
Search Community

Does useGSAP context revert kill Draggable instances?

NickWoodward

Go to solution Solved by Cassie,

Recommended Posts

NickWoodward
Posted

I have a useGSAP hook with a Draggable instance inside it. When my dependency changes, useGSAP reverts and re-runs, but the old Draggable instance seems to survive — I end up with multiple Draggables responding to the same drag.
 

const { contextSafe } = useGSAP(() => {
  const mm = gsap.matchMedia();

  mm.add({ isMobile: "...", isDesktop: "..." }, (context) => {
    const items = gsap.utils.toArray(".slide");
    
    const tl = gsap.timeline({ paused: true });
    // ... timeline setup ...

    Draggable.create(proxy, {
      trigger: items[0].parentNode,
      type: "x",
      onPress() {
        console.log("onPress, items:", items.length);
      },
    });
  });
}, { scope: wrapperRef, dependencies: [numSlides] });


When numSlides changes (e.g. 7 → 8), I see two onPress logs — one with items: 7, one with items: 8. The old Draggable isn't being killed.

Is Draggable.create() supposed to be tracked by the GSAP context and reverted automatically? Or do I need to manually kill it in a cleanup return from mm.add()?

Sorry for the lack of a working code example, this is part of a much larger slider which I think would've just complicated the issue!

Thanks,

Nick

NickWoodward
Posted
On 4/6/2026 at 5:07 AM, Cassie said:

Heya!

 

You're looking for revertOnUpdate:true

WithoutrevertOnUpdate: true, useGSAP only reverts when the component unmounts, not on dependency changes. The dependency array just re-runs the callback but doesn't clean up the previous context.

 

https://gsap.com/resources/React/#revertOnUpdate
 

 

Amazing, thank you very much!

  • Like 1

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...