DEV Community

Cover image for React + GreenSock Integration
Christopher Daniel
Christopher Daniel

Posted on • Edited on

11

React + GreenSock Integration

React + Greensock

I will explain you how to integrate GSAP animations to React application.

demo: GSAP

Using create-react-app create your react application setup.

Now the react application setup is done, we have to add green sock for animation.

create-react-app react-gsap
yarn add gsap

Now you can see the react icon spinning with the help of css animation. We have to convert it to GSAP.

Change App.css. We are removing all the animations.

.App {
text-align: center;
}
.App-logo {
height: 40vmin;
}
.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}
.App-link {
color: #61dafb;
}

Its time to add GSAP animation ...

To add animation to an element using GSAP we need a reference for the element in react. I'm going to use useRef() hook.

Now I will show how to get reference to the element using callback ref

import React, {useEffect, useRef} from 'react';
import logo from './logo.svg';
import './App.css';
const App = () => {
let logoElement = useRef(null);
useEffect(() => {
console.log(logoElement);
}, []);
return (
<div className="App">
<header className="App-header">
<img src={logo}
onMouseEnter={scaleUp}
onMouseLeave={scaleDown}
ref={element => {logoElement = element}}
className="App-logo"
alt="logo"
/>
<p>
React + GSAP animation
</p>
</header>
</div>
);
}
export default App;

You can see the element in the console.

TweenMax

TweenMax in a animation util from greensock. To learn more about TweenMax visit tweenmax.

Inorder to rotate the div we have to use

   TweenMax.to(
            logoElement,                  // reference to the element 
            1,                            // duration 
            {
                repeat: -1,               // infinite loop of animation
                rotation: 360,            // 360deg rotation
                ease: Linear.easeNone     // linear speed no acceleration -------
            }
        )

Enter fullscreen mode Exit fullscreen mode

Now we can add the animation when the component mounts.
Here is the final version

import React, {useEffect, useRef} from 'react';
import {TweenMax, Linear} from 'gsap';
import logo from './logo.svg';
import './App.css';
const App = () => {
let logoElement = useRef(null);
useEffect(() => {
TweenMax.to(
logoElement,
1,
{
repeat: -1,
rotation: 360,
ease: Linear.easeNone
}
)
}, []);
function scaleUp() {
TweenMax.to(logoElement, 1, {
scale: 1.25,
ease: Linear.ease
});
}
function scaleDown() {
TweenMax.to(logoElement, 1, {
scale: 0.75
});
}
return (
<div className="App">
<header className="App-header">
<img src={logo} onMouseEnter={scaleUp} onMouseLeave={scaleDown} ref={element => {logoElement = element}} className="App-logo" alt="logo"/>
<p>
React + GSAP animation
</p>
</header>
</div>
);
}
export default App;

The full code is available on github github repo

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (3)

Collapse
 
atulcodex profile image
🚩 Atul Prajapati 🇮🇳

i think it's Totally fack because all links are fack

Collapse
 
chris_daniel profile image
Christopher Daniel

Hey,

just copy the link and try instead of using anchor tag, I think the links are messed up. I'll correct it.

Collapse
 
atulcodex profile image
🚩 Atul Prajapati 🇮🇳

haa, nice you have corrected the links great

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free