Home / Learn React / Animation in React
Animation in React
Use CSS transitions for simple effects, and a library such as Motion (Framer Motion) for enter, exit and layout animation.
Theory in plain words
What it is
Animation changes a value over time. In React you change state, and either CSS or a library animates the difference between the old and new look.
Why we use it
Motion explains changes (a panel opened, an item was added) and makes the interface feel alive. Bad animation is slow and annoying, so keep it short and respect users who ask for less motion.
How it works
CSS handles transition on properties like transform and opacity, which are cheap. React removes elements immediately on unmount, so exit animations need a library or a delayed unmount.
Where it is used
- Menus, accordions, toasts, page changes, loaders
New items play the keyframe once when they appear. Keys keep older items from replaying it.
npm install motion
import { AnimatePresence, motion } from "motion/react";
<AnimatePresence>
{show && <motion.div initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }}>Hello</motion.div>}
</AnimatePresence>prefers-reduced-motion. Animate transform and opacity, not width or top.Quick quiz
1. Cheapest properties to animate?
They avoid layout work.
2. Why do exit animations need a library?
Libraries like Motion delay unmount.
3. Which media query respects user settings?
Turn animations down for them.
Common questions
Cheapest properties to animate?
transform and opacity. They avoid layout work.
Why do exit animations need a library?
React removes elements immediately on unmount. Libraries like Motion delay unmount.
Which media query respects user settings?
prefers-reduced-motion. Turn animations down for them.