Jump to content
GreenSock

pietdasilva

gsap.utils.selector on react

Recommended Posts

hi friends, i'm new to gsap and just learned how the .from tween got some issues with react. it's the same thing with the gsap.utils.selector method? cause i can't seem to animate the children on scroll. thanks!  *edit: can't seem to use sandbox either so here's the code

import React, { useEffect, useRef } from "react";
import { gsap } from "gsap";
import "./styles.css";
import { ScrollTrigger } from "gsap/all";
gsap.registerPlugin(ScrollTrigger);
 
export default function App() {
const boxRef = useRef(null);
const q = gsap.utils.selector(boxRef);
 
useEffect(() => {
gsap.to(q, {
rotation: "+=360",
duration: 2.5,
scrollTrigger: { trigger: q },
});
});
 
return (
<div className="app">
<div className="boxes" ref={boxRef}></div>
<div className="box">
<div>1</div>
</div>
<div className="box">
<div>2</div>
</div>
<div className="box">
<div>3</div>
</div>
<div className="box">
<div>4</div>
</div>
</div>
);
}
Link to comment
Share on other sites

You need to use the selector in the effect since refs are not populated until after render. Also, be wary of reacts double render in strict mode.

Link to comment
Share on other sites

like this? still no luck, localhost is blank :(

 

import { useState, useEffect, useRef } from "react";
import "./Projects.css";
import { ProjectImg } from "./ProjectImg";
import gsap from "gsap";
import { ScrollTrigger } from "gsap/all";
gsap.registerPlugin(ScrollTrigger);
 
function Projects() {
  const wordsRef = useRef();
 
  useEffect(() => {
    const q = gsap.utils.selector(wordsRef);
    gsap.to(q, {
      y: -50,
      opacity: 1,
      duration: 2.5,
      scrollTrigger: { trigger: q }
    });
  });
 
  return (
    <>
      <section className="projects">
        {ProjectImg.map((elem, i) => (
          <li key={i} useRef={wordsRef}>
            <span className="title">{elem.name}</span>
            <div className="img-cont">
              <img src={elem.img} alt={elem.name} />
            </div>
            <p>{elem.text}</p>
          </li>
        ))}
      </section>
    </>
  );
}
 
export default Projects;
Link to comment
Share on other sites

@pietdasilva gsap.utils.selector returns a selector function. You cannot target q directly in your example. If you want to select everything withing the wordsRef element, use q(">  *") or some other css selector.

  • Like 2
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.
×