Jump to content
Search Community

how can I duplicate sections in gsap.context

PavelZolotin test
Moderator Tag

Recommended Posts

Hi @elegantseagulls!

I'm done doing what I can:)
How do I make projects animate one after another in the React version like it's done in native JS.
The forEach method doesn't work in conjunction with self.selector.
At the moment, the animations of the projects overlap each other when scrolling. 

Link to comment
Share on other sites

Hi there!

This is the only way it worked for me:
https://stackblitz.com/edit/react-ts-yopzt8?file=src%2FApp.jsx
Yes, there are no indents, but the meaning of the animation seems clear.

 

I only had to have one generic ref={main} for the whole container and use "selector" for projects divs, and then go through this selector by forEach method and use for internal divs - querySelector instead of ref.current-links like I did before.

As far as I know, using querySelector instead of ref, is a React bad practice.
Is there a cleaner solution to my problem? 

Link to comment
Share on other sites

Hi,

 

I'm not sure I follow exactly what is the issue you're having right now. The only problem I see in your code is the end point of your ScrollTrigger configurations:

scrollTrigger: {
  trigger: proj,
  start: 'top top',
  end: () => '+=' + (animWrap.scrollWidth - window.innerWidth),
  pin: wrap,
  scrub: true,
},

If you log that you'll get a negative value, so you're telling ScrollTrigger that your animation will end before it starts.

 

This seems to work as you intend:

https://stackblitz.com/edit/react-ts-rukknu?file=src%2FApp.jsx

 

As for query selector being a bad practice, that's a deep rabbit hole that I don't intend to go through. One of the main reasons I'm not a big fan of React is because the React team takes too much responsibility away from developers. Why? Because they always preventing users from doing this or that, or not doing things the React way, or saying that something is an anti-pattern, etc. Using a regular query selector is completely fine as long as you know that the target element IS in the DOM. As developers is our responsibility to know exactly when an element is or isn't rendered in the DOM. If you don't know then, IMHO, you have other problems that don't relate to selectors being a bad practice or any nonsense like that. In this cases is most about when the element is in the DOM and as we write the code of our apps and components we should know the logic that tells us when something is in the DOM and when is not. Then you have lifecycle hooks that allow you to make changes so nothing breaks your app. Finally GSAP Context provides great help when developing apps in React with GSAP.

 

Hopefully this helps.

Happy Tweening!

  • Like 3
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...