Jump to content
Search Community

The right way using gsap context in vue or nuxt project?

Reksa Andhika test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

Hi, actually just quick question, because this can be change my behavior in future on writing gsap code.

In gsap context docs, mostly the docs only show use case for react, meanwhile, I'm using vue/nuxt 3.

So I have a question especially on reverting the context,  which one is the right one?

Example 1: 
image.png.6410cdf94ac57b67d915ac8bcee655ab.png

Example 2 (define ctx variable outside and call the revert onUnmounted):
image.png.4d886764fb337f56b82bc59f66aeeba9.png

Or maybe there's a better way to code than these examples?

Thank you.

Link to comment
Share on other sites

  • Solution

Hi,

 

The second approach is the one that will work actually. This is a React pattern (although is also used in Svelte as well):

onMounted(() => {
  // rest of your code
  return () => console.log("this doesn't work in Vue"); // this won't be executed when the component is unmounted
});

Finally there is no need to turn the GSAP Context instance into a reactive property using ref():

let ctx;

onMounted(() => {
  ctx = gsap.context(() => {});
});

onUnmounted(() => {
  ctx && ctx.revert();
});

Hopefully this helps.

Happy Tweening!

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