HTML content: Step-by-step tutorial

Matt Perry
In this tutorial, we're going to build the HTML content example step-by-step.
This tutorial is rated 1/5 difficulty, which means we'll spend some time explaining the Motion APIs that we've chosen to use (and why), and also any browser APIs we encounter that might be unfamiliar to beginners.
Here's a live demo of the example we're going to be creating:
Introduction
The HTML Content example shows how to animate numerical content in React without animating React state. This is much better for performance.
The magic is in the motion
component's ability to render motion values. This bypasses React's diffing algorithm, and assigns the value of the motion value to the DOM node directly.
Get started
Let's start with a basic component structure:
Let's animate!
Import from Motion
First, we'll import the necessary hooks and functions from Motion:
Creating the animation
Create a motion value to track our counter:
Use
useTransform
to round the number as it animates:
Start the animation when the component mounts using Motion's
animate
function:
Display the animated value using a
motion.pre
component:
Conclusion
In this tutorial, we learned how to:
Create an animated counter using
useMotionValue
Transform motion values into new values with
useTransform
Start and clean up animations with
useEffect
Display animated numerical content in a React component