Jump to content
GreenSock

jonathanrace77

'From' not working (React import component)

Recommended Posts

So I've been putting some animations together in React using 'from', so far so good.  I start to realise that I'm going to have quite a few of these so decide to create a separate component for all of the animations and then import them in.

 

Out of the 4 animations though, only one of them works properly and that's the 'to' animation.

 

With the 'from' animations, they are stuck in their 'from position' and won't move.

 

main App.js:

import React, { useEffect } from "react";
import "./css/style.css";
import animations from "./animations.js";

...

useEffect(() => {
    animations();
  });

...

animations.js

import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
gsap.registerPlugin(ScrollTrigger);
const tlAboutTitle = gsap.timeline();
const tlAboutPic = gsap.timeline();
const tlAboutText = gsap.timeline();
const tlSkillBar = gsap.timeline();

var staggerTime = 0.3;

const animations = () => {
  tlAboutTitle.from("#about-title", {
    scrollTrigger: {
      trigger: "#about-title",
      start: "bottom bottom",
      markers: true,
    },
    opacity: 0,
    x: "-50px",
  });

  tlAboutPic.from(".jonathan-box-left-gsap", {
    scrollTrigger: {
      trigger: ".about-jonathan-box-right",
      start: "bottom bottom",
      markers: true,
    },
    opacity: 0,
    x: "-50px",
    stagger: staggerTime,
  });

  tlAboutText.from(".about-jonathan-box-right-gsap", {
    scrollTrigger: {
      trigger: ".about-jonathan-box-right",
      start: "bottom bottom",
      markers: true,
    },
    opacity: 0,
    y: "50px",
    stagger: staggerTime,
  });

  tlSkillBar.to(".about-skill-bar-fill-design", {
    scrollTrigger: {
      trigger: "#experienced-about-skill-box",
      start: "center bottom",
      markers: true,
    },
    width: "100%",
    stagger: staggerTime,
  });
};

// Exporting the component
export default animations;

 

Link to comment
Share on other sites

Solved.

 

There was another animation in the main App.js. 

 

I'd taken out "import gsap..." from here which caused it to freak out.

  • Like 2
Link to comment
Share on other sites

Glad you got something figured out. 
 

A couple of notes about your code:

  • In general you should try to use refs in React, not selector strings like what you have. More on that here.
  • No need for the value being a string here: y: "50px". Just use y: 50 as covered in the most common GSAP mistakes article.
  • You might as well use let instead of var.
  • Like 1
Link to comment
Share on other sites

  • 1 year later...
const divRef=useRef();
 
useEffect(()=>{
gsap.from(divRef.current,{
duration:1,
autoAlpha:0,
ease:"none",
delay:3
})
})
here gsap.from are not working but gsap.to working what i do for gap.from ?????? pleaaaaase reply
Link to comment
Share on other sites

Heya!

 

Yeah it's annoying - React 18 changed some things and now double calls effects because of strict mode.
 

Here's a thread on how to work around the new behaviour 
 

 

Link to comment
Share on other sites

Yep. I bet the problem is that React 18 runs in "strict" mode locally by default which causes your useEffect() to get called TWICE! Very annoying. It has caused a lot of headaches for a lot of people outside the GSAP community too.

 

.from() tweens use the CURRENT value as the destination and it renders immediately the value you set in the tween, so when it's called the first time it'd work great but if you call it twice, it ends up animating from the from value (no animation). It's not a GSAP bug - it's a logic thing.

 

For example, let's say el.x is 0 and you do this: 

useEffect(() => {
  // what happens if this gets called twice?
  gsap.from(el, {x: 100})
}, []);

 

The first time makes el.x jump immediately to 100 and start animating backwards toward the current value which is 0 (so 100 --> 0). But the second time, it would jump to 100 (same) and animate back to the current value which is now 100 (100 --> 100)!  See the issue?

 

So you can either turn off strict mode in React or you can add some conditional logic to your useEffect() call so that it only runs ONCE. Sorta like:

const didAnimate = useRef(false);

useEffect(() => {
  // if we already ran this once, skip!
  if (didAnimate.current) { return; }
  // otherwise, record that we're running it now and continue...
  didAnimate.current = true;
  gsap.from(el, {x: 100});
}, []);

 

Or you could just use .fromTo() tweens so that you define both the start and end values.

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