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.

In shortChange a class or style from state and let CSS animate it. For leaving animations use a library.

Theory in plain words

State drives animationClick changes stateClass or stylechangesCSS transitionplaysNew look on screen
State drives animation

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
A smooth accordion with CSSTry it Yourself
Mount animation with a keyframeTry it Yourself

New items play the keyframe once when they appear. Keys keep older items from replaying it.

Motion library for exit animation
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>
Tip:Respect prefers-reduced-motion. Animate transform and opacity, not width or top.

Quick quiz

1. Cheapest properties to animate?

2. Why do exit animations need a library?

3. Which media query respects user settings?

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.