惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
L
LINUX DO - 热门话题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
V
V2EX
GbyAI
GbyAI
量子位
Microsoft Azure Blog
Microsoft Azure Blog
有赞技术团队
有赞技术团队
G
Google Developers Blog
云风的 BLOG
云风的 BLOG
B
Blog
Microsoft Security Blog
Microsoft Security Blog
S
SegmentFault 最新的问题
O
OpenAI News
N
News and Events Feed by Topic
博客园 - Franky
爱范儿
爱范儿
Forbes - Security
Forbes - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V2EX - 技术
V2EX - 技术
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Schneier on Security
Schneier on Security
Cloudbric
Cloudbric
Security Archives - TechRepublic
Security Archives - TechRepublic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Recent Commits to openclaw:main
Recent Commits to openclaw:main
人人都是产品经理
人人都是产品经理
P
Privacy International News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog RSS Feed
阮一峰的网络日志
阮一峰的网络日志
D
DataBreaches.Net
Last Week in AI
Last Week in AI
罗磊的独立博客
Spread Privacy
Spread Privacy
Recent Announcements
Recent Announcements
The Cloudflare Blog
Google DeepMind News
Google DeepMind News
AWS News Blog
AWS News Blog
The Register - Security
The Register - Security
Y
Y Combinator Blog
J
Java Code Geeks
I
Intezer

Maggie Appleton

The Dark Forest and Generative AI One Developer, Two Dozen Agents, Zero Alignment Gas Town’s Agent Patterns, Design Bottlenecks, and Vibecoding at Scale January 2026 | Maggie Appleton A Treatise on AI Chatbots Undermining the Enlightenment A Brief History & Ethos of the Digital Garden Vibe Code is Legacy Code May 2025 | Maggie Appleton Home-Cooked Software and Barefoot Developers Statistically, When Will My Baby Be Born? Speculative Calendar Events ChatGPT Would be a Decent Policy Advisor March 2025 | Maggie Appleton The Expanding Dark Forest and Generative AI Humanity's Last Exam Squish Meets Structure Common Misconceptions in AI Undetected AI Exam Answers Unbaited Smidgeons Growing a Human: The First 30 Weeks How to Import Academic Papers from Zotero into Tana December 2024 | Maggie Appleton Aesthetic Command Lines with Hyper, Spaceship, and Oh My Zsh Leaving Elicit July 2024 | Maggie Appleton A Short History of Bi-Directional Links The Pattern Language of Project Xanadu Assumed Audiences Ambient Co-presence On Opening Essays, Conference Talks, and Jam Jars Spinning Worlds, Seasickness, and Dealing with Vestibular Neuritis A Collection of Design Engineers Gathering Structures Daily Notes Pages Historical Trails December 2023 | Maggie Appleton September 2023 | Maggie Appleton Digital Gardening for Non-Technical Folks Language Model Sketchbook, or Why I Hate Chatbots June 2023 | Maggie Appleton Computational Notebooks Folk Interfaces Reverse Outlining with Language Models Command K Bars Spatial Web Browsing A Picture Worth a Thousand Programmes Programmable Notes Programming Portals Teenage Skeuomorphic Desktop Designs Tending Evergreen Notes in Roam Research Growing the Evergreens Why You Own an iPad and Still Can't Draw A Brief Introduction to Digital Anthropology Transclusion and Transcopyright Dreams The Block-Paved Path to Structured Data Empty Pointers and Constellations of AI Metaphors We Web By The Gift Economy Epistemic Disclosure November 2022 | Maggie Appleton Joining Ought July 2022 | Maggie Appleton The Linear Oppression of Note-taking Apps Paleolithic Nostalgia Interoperable Personal Libraries and Ad Hoc Reading Groups The Finest Narrative Non-Fiction Essays Algorithmic Transparency October 2021 | Maggie Appleton Plebeian Programming with Keyboard Maestro The Cultural Anthropology of React August 2021 | Maggie Appleton Natureculture, Moral Purity, and Cultural Boundaries The Echo & Narcissus Writing Club Pink, Soft, Glittering Developers Fetishism & Mechanical Keyboards Making Programming Visual, Spatial, and Learnable Organic, Local, Artisan Data Storage Positioning Elements & Scrollytelling in CSS Painting Roam Research with Custom CSS A Digital Anthropology Reading List The Eponymous Laws of Programming A History of Cyborgs Neologisms The Bare Essentials of Greensock September 2020 | Maggie Appleton Illustrating Gatsby's Key Concepts Problematic Proteins New Harvest & Illustrating the Cultivated Meat Podcast Synecdoche: Drawing the Part for the Whole A Meta-Tour of This Site Douglas, Dirt, and Matter Out of Place The Knowledge Hydrant A Naïve Exploration of Computer-Supported Collaborative Learning Silent Synchronous Reading Sessions What the Fork is React Suspense? Visually Workshopping the AWS Cloud Are Data Unions the Future of Data? Pattern Languages in Programming and Interface Design A Metaphorical Reading Collection
GreenSock Animations with React Hooks
2020-09-27 · via Maggie Appleton

Using the GreenSock web animation library (also known as gsap) in vanilla JavaScript isn’t too complex. But when we want it to play nicely with a frontend framework like React, we run into some finicky issues.

This guide is specifically about using React Hooks with GreenSock, as many of the existing guides only explain how to set it up with the old style of class components.

This isn’t an introduction to the GreenSock library itself – head over to my post on The Bare Essentials of Greensock

The Bare Essentials of Greensock

Notes on the basics of the Greensock animation llibrary for that.

The issue with combining these two libraries is that React acts as a DOM interloper – it sits between the code we write and the DOM, managing it for us.

React builds it’s own internal “tree” of DOM nodes based off our code, and only updates the actual DOM as needed (a concept previously known as the React Virtual DOM

What the Fork is the React Virtual DOM

Illustrated notes on how the React virtual DOM works ) The React team has gone off the term of the virtual DOM and prefer to talk about the render tree. Potatoes, potataos. For more on how the virtual DOM works, I have an illustrated guide

A diagram of React generating all the DOM nodes for us

Meanwhile, the GreenSock library usually works by directly animating our DOM elements. Perhaps you can sense the tension in the room…

Since the GreenSock library needs access to the DOM in order to animate it, we need to tell React about our Greensock animations. Otherwise the two end up in awkward arguments that can end in tears (primarily mine).

Bring in the React Hooks

There’s two essential React hooks we’ll use to connect our React code to our Greensock code: useRef and useEffect

useRef lets us access and directly target our DOM nodes

useEffect lets us to perform side effects – any functions or data requests that we want to run after rendering the component to the DOM

If you’re not completely comfortable with React Hooks yet and those technical explanations mean nothing to you, don’t worry we’re going to slowly walk through this. With illustrations. Because who the fork properly understands “side effects”?


Getting Setup

We first need to run yarn install gsap or npm install gsap into our React project repo gsap is short for GreenSock Animation Platform and it’s the way we reference the Greensock library in all our code sytax

Feel free to open up a Codesandbox project to work through the steps while you read. I’ve set one up for you with React and Greensock already installed:


Building our Animation Component

Let’s start by creating a React component called <AnimatedBox/> that is going to hold all our Greensock code. We’re going to build a simple <div> element and have it animate across the page (exciting!).

We’ll need to import the Greensock library into our component with import { gsap }from 'gsap', as well as React and our two hooks with import React, { useRef, useEffect } from 'react'

import React, { useRef, useEffect } from "react";
import { gsap } from "gsap";

const Animation = () => {
	return (
		<div
			style={{
				width: "160px",
				height: "160px",
				background: "salmon",
			}}
		/>
	);
};

I’ve set some basic width, height, and color CSS styles on our div to make it visible on the page.

Good looking div, right? Salmon is always the right color choice.


Adding the useRef Hook

We’re now going to add references to our animation with the useRef hook.

We create a new reference by adding const boxRef = useRef() anywhere outside of our return statement. Call it anything you like, but boxRef is a good enough name for this example.

We then add a ref={boxRef} attribute on our <div> element.

const Animation = () => {
	const boxRef = useRef();

	return (
		<div
			ref={boxRef}
			style={{
				width: "120px",
				height: "120px",
				background: "salmon",
			}}
		/>
	);
};

This tells React we want to be able to control our <div> element with the boxRef reference once our component is mounted onto the DOM.


Adding the useEffect Hook

Next we’re going to add our useEffect hook, and write our GreenSock animation inside of it.

If you’re already familiar with greensock this will look fairly straightforward.

const Animation = () => {
	const boxRef = useRef();

	useEffect(() => {
		gsap.to([boxRef.current], {
			x: "400px",
			duration: 2,
		});
	});

	return (
		<div
			ref={boxRef}
			style={{
				width: "120px",
				height: "120px",
				background: "salmon",
			}}
		/>
	);
};

We declare a normal gsap.to() tween, but instead of passing in an id or class name as the target element, we use [boxRef.current].

This means we want gsap to animate the .current property of our boxRef reference – which is the <div> element we put that reference on.

gsap.to([boxRef.current], {});

We then pass in our variables object, which tells Greensock to move our div 400px along the x axis over a duration of 2 seconds:

{ x: 400px, duration: 2 }

And thus, our div animates…

Tada 🎉

So by this point, you hopefully have a working animation in that Codesandbox project or a local repo.


If you don’t feel like mucking around with useRef and useEffect, John Lindquist created a simple React hook called use-gsap that hides them away.