react-motion, react-spring, react-transition-group, and react-transition-state are libraries used to handle animations and state transitions in React applications. react-spring is a physics-based animation library that succeeds react-motion. react-transition-group is a low-level utility for managing component entry and exit transitions, often paired with CSS. react-transition-state offers a lightweight, hook-based alternative for managing transition states without the overhead of a full transition group. Together, they cover the spectrum from complex physics-driven motion to simple CSS class toggling.
When building interactive React applications, motion adds polish and guides user attention. However, choosing the right tool depends on whether you need physics-based interpolation, CSS class management, or simple state tracking. react-motion, react-spring, react-transition-group, and react-transition-state each solve different parts of this puzzle. Let's break down how they work and when to use them.
react-motion is the predecessor to react-spring.
// react-motion: Legacy API (Class-based / Render Props)
import { Motion, spring } from 'react-motion';
function Box() {
return (
<Motion defaultStyle={{ x: 0 }} style={{ x: spring(100) }}>
{interpolatingStyle => (
<div style={{ transform: `translateX(${interpolatingStyle.x}px)` }} />
)}
</Motion>
);
}
react-spring is the active successor.
// react-spring: Modern Hooks API
import { useSpring, animated } from '@react-spring/web';
function Box() {
const props = useSpring({ to: { x: 100 }, from: { x: 0 } });
return <animated.div style={{ transform: props.x.to(x => `translateX(${x}px)`) }} />;
}
react-transition-group is actively maintained by the React community.
// react-transition-group: CSSTransition
import { CSSTransition } from 'react-transition-group';
function FadeItem({ show, children }) {
return (
<CSSTransition in={show} timeout={300} classNames="fade">
{children}
</CSSTransition>
);
}
react-transition-state is a lightweight, modern alternative.
react-transition-group feels heavy.// react-transition-state: Hook-based state
import { useTransitionState } from 'react-transition-state';
function FadeItem({ show, children }) {
const { mounted, transitionState } = useTransitionState(show, { timeout: 300 });
if (!mounted) return null;
return <div className={`fade-${transitionState}`}>{children}</div>;
}
The way you define timing changes drastically between these libraries.
react-spring uses physics.
// react-spring: Spring config
const props = useSpring({
from: { opacity: 0 },
to: { opacity: 1 },
config: { tension: 280, friction: 60 }
});
react-motion also used physics.
// react-motion: Spring config
<Motion style={{ opacity: spring(1, { stiffness: 180, damping: 20 }) }}>
{style => <div style={style} />}
</Motion>
react-transition-group uses time-based CSS.
timeout in milliseconds./* react-transition-group: CSS matching timeout */
.fade-enter {
opacity: 0;
}
.fade-enter-active {
opacity: 1;
transition: opacity 300ms;
}
react-transition-state uses time-based state.
timeout in milliseconds.'entering', 'exiting') to apply classes.// react-transition-state: State string
const { transitionState } = useTransitionState(show, { timeout: 300 });
// transitionState === 'entering' | 'entered' | 'exiting' | 'exited'
How you integrate these into your component tree varies significantly.
react-transition-group requires wrapping children.
<CSSTransition> or <TransitionGroup>.// react-transition-group: Wrapper component
<CSSTransition in={isOpen} timeout={300} classNames="modal">
<ModalContent />
</CSSTransition>
react-transition-state uses hooks inside the component.
// react-transition-state: Internal hook
function Modal({ isOpen }) {
const { mounted, transitionState } = useTransitionState(isOpen, { timeout: 300 });
if (!mounted) return null;
return <div className={`modal-${transitionState}`}>...</div>;
}
react-spring uses animated wrappers.
animated.div to bind values.// react-spring: Animated component
import { animated } from '@react-spring/web';
return <animated.div style={props}>Content</animated.div>;
react-motion uses render props.
<Motion> component.// react-motion: Render prop
<Motion style={{ x: spring(100) }}>
{interpolated => <div style={{ left: interpolated.x }} />}
</Motion>
Animating lists (entering/leaving items) is a common challenge.
react-transition-group provides <TransitionGroup>.
// react-transition-group: List handling
import { TransitionGroup, CSSTransition } from 'react-transition-group';
<TransitionGroup>
{items.map(item => (
<CSSTransition key={item.id} timeout={300} classNames="list-item">
<li>{item.text}</li>
</CSSTransition>
))}
</TransitionGroup>
react-spring provides useTransition.
// react-spring: List handling
const transitions = useTransition(items, {
from: { opacity: 0, transform: 'translateY(-10px)' },
enter: { opacity: 1, transform: 'translateY(0)' },
leave: { opacity: 0, transform: 'translateY(-10px)' }
});
react-transition-state requires manual mapping.
// react-transition-state: Manual list handling
{items.map(item => {
const { mounted } = useTransitionState(item.visible, { timeout: 300 });
return mounted ? <li key={item.id}>{item.text}</li> : null;
})}
react-motion requires manual handling.
TransitionGroup.// react-motion: Manual list handling
{items.map(item => (
<Motion key={item.id} style={{ opacity: spring(item.visible ? 1 : 0) }}>
{style => <li style={style}>{item.text}</li>}
</Motion>
))}
These tools are powerful, but overkill for simple cases.
react-motion: As stated, avoid this in new work. It is legacy technology.| Feature | react-spring | react-transition-group | react-transition-state | react-motion |
|---|---|---|---|---|
| Primary Use | Physics animations | CSS Enter/Exit | Hook-based State | Legacy Physics |
| API Style | Hooks + Components | Components | Hooks | Render Props |
| Timing | Springs (Tension/Friction) | Duration (ms) | Duration (ms) | Springs (Stiffness/Damping) |
| List Support | useTransition | <TransitionGroup> | Manual | Manual |
| Maintenance | ā Active | ā Active | ā Active | ā Deprecated |
react-spring is your go-to for motion design.
react-motion in every way.react-transition-group is the standard for UI state changes.
react-transition-state is the lightweight hook alternative.
react-transition-group without the wrapper components.react-motion is history.
react-spring instead.Final Thought: For most modern apps, a combination of react-spring (for interactive motion) and react-transition-group or react-transition-state (for mounting transitions) provides the best balance of power and simplicity.
Do not choose react-motion for new projects. It is effectively deprecated and unmaintained. The creator moved development to react-spring, which offers better performance, TypeScript support, and a more modern API. Using react-motion today introduces technical debt and potential compatibility issues with modern React versions.
Choose react-spring if you need complex, physics-based animations that feel natural and interactive. It is ideal for drag gestures, springy UI elements, and coordinated animations across multiple properties. It requires a shift in thinking from duration-based to spring-based timing, but offers superior control for high-fidelity motion design.
Choose react-transition-group if you need standard, CSS-driven enter/exit transitions (like fading a modal in or out). It is the industry standard for mounting and unmounting components with animation. It is best suited for declarative transitions where you define CSS classes for states like 'enter-active' or 'exit-done'.
Choose react-transition-state if you want a lighter, hook-based solution for managing transition states without the complexity of react-transition-group. It is great for simple toggles where you need to know if an element is entering, leaving, or mounted, allowing you to apply classes or styles manually without the overhead of cloning children.
import {Motion, spring} from 'react-motion';
// In your render...
<Motion defaultStyle={{x: 0}} style={{x: spring(10)}}>
{value => <div>{value.x}</div>}
</Motion>
Animate a counter from 0 to 10. For more advanced usage, see below.
Npm: npm install --save react-motion
Bower: do not install with bower install react-motion, it won't work. Use bower install --save https://unpkg.com/react-motion/bower.zip. Or in bower.json:
{
"dependencies": {
"react-motion": "https://unpkg.com/react-motion/bower.zip"
}
}
then include as
<script src="bower_components/react-motion/build/react-motion.js"></script>
<script src="https://unpkg.com/react-motion/build/react-motion.js"></script>
(Module exposed as `ReactMotion`)
Works with React-Native v0.18+.
git clone https://github.com/chenglou/react-motion.git
cd react-motion
npm install
npm start.npm run build-demos and open the static demos/demo_name/index.html file directly. Don't forget to use production mode when testing your animation's performance!To build the repo yourself: npm run prepublish.
For 95% of use-cases of animating components, we don't have to resort to using hard-coded easing curves and duration. Set up a stiffness and damping for your UI element, and let the magic of physics take care of the rest. This way, you don't have to worry about petty situations such as interrupted animation behavior. It also greatly simplifies the API.
This library also provides an alternative, more powerful API for React's TransitionGroup.
Exports:
springMotionStaggeredMotionTransitionMotionpresetsHere's the well-annotated public Flow type definition file (you don't have to use Flow with React-motion, but the types help document the API below).
P.S. using TypeScript? Here are the React-motion TypeScript definitions!
Used in conjunction with the components below. Specifies the how to animate to the destination value, e.g. spring(10, {stiffness: 120, damping: 17}) means "animate to value 10, with a spring of stiffness 120 and damping 17".
val: the value.
config: optional, for further adjustments. Possible fields:
stiffness: optional, defaults to 170.damping: optional, defaults to 26.precision: optional, defaults to 0.01. Specifies both the rounding of the interpolated value and the speed (internal).It's normal not to feel how stiffness and damping affect your spring; use Spring Parameters Chooser to get a feeling. Usually, you'd just use the list of tasteful stiffness/damping presets below.
{stiffness, damping}Commonly used spring configurations used like so: spring(10, presets.wobbly) or spring(20, {...presets.gentle, precision: 0.1}). See here.
<Motion defaultStyle={{x: 0}} style={{x: spring(10)}}>
{interpolatingStyle => <div style={interpolatingStyle} />}
</Motion>
Required. The Style type is an object that maps to either a number or an OpaqueConfig returned by spring() above. Must keep the same keys throughout component's existence. The meaning of the values:
OpaqueConfig returned from spring(x): interpolate to x.number x: jump to x, do not interpolate.Optional. The PlainStyle type maps to numbers. Defaults to an object with the same keys as style above, whose values are the initial numbers you're interpolating on. Note that during subsequent renders, this prop is ignored. The values will interpolate from the current ones to the destination ones (specified by style).
Required function.
interpolatedStyle: the interpolated style object passed back to you. E.g. if you gave style={{x: spring(10), y: spring(20)}}, you'll receive as interpolatedStyle, at a certain time, {x: 5.2, y: 12.1}, which you can then apply on your div or something else.
Return: must return one React element to render.
Optional. The callback that fires when the animation comes to a rest.
Animates a collection of (fixed length) items whose values depend on each other, creating a natural, springy, "staggering" effect like so. This is preferred over hard-coding a delay for an array of Motions to achieve a similar (but less natural-looking) effect.
<StaggeredMotion
defaultStyles={[{h: 0}, {h: 0}, {h: 0}]}
styles={prevInterpolatedStyles => prevInterpolatedStyles.map((_, i) => {
return i === 0
? {h: spring(100)}
: {h: spring(prevInterpolatedStyles[i - 1].h)}
})}>
{interpolatingStyles =>
<div>
{interpolatingStyles.map((style, i) =>
<div key={i} style={{border: '1px solid', height: style.h}} />)
}
</div>
}
</StaggeredMotion>
Aka "the current spring's destination value is the interpolating value of the previous spring". Imagine a spring dragging another. Physics, it works!
Required function. Don't forget the "s"!
previousInterpolatedStyles: the previously interpolating (array of) styles (undefined at first render, unless defaultStyles is provided).
Return: must return an array of Styles containing the destination values, e.g. [{x: spring(10)}, {x: spring(20)}].
Optional. Similar to Motion's defaultStyle, but an array of them.
Required function. Similar to Motion's children, but accepts the array of interpolated styles instead, e.g. [{x: 5}, {x: 6.4}, {x: 8.1}]
(No onRest for StaggeredMotion because we haven't found a good semantics for it yet. Voice your support in the issues section.)
Helps you to do mounting and unmounting animation.
You have items a, b, c, with their respective style configuration, given to TransitionMotion's styles. In its children function, you're passed the three interpolated styles as params; you map over them and produce three components. All is good.
During next render, you give only a and b, indicating that you want c gone, but that you'd like to animate it reaching value 0, before killing it for good.
Fortunately, TransitionMotion has kept c around and still passes it into the children function param. So when you're mapping over these three interpolated styles, you're still producing three components. It'll keep interpolating, while checking c's current value at every frame. Once c reaches the specified 0, TransitionMotion will remove it for good (from the interpolated styles passed to your children function).
This time, when mapping through the two remaining interpolated styles, you'll produce only two components. c is gone for real.
import createReactClass from 'create-react-class';
const Demo = createReactClass({
getInitialState() {
return {
items: [{key: 'a', size: 10}, {key: 'b', size: 20}, {key: 'c', size: 30}],
};
},
componentDidMount() {
this.setState({
items: [{key: 'a', size: 10}, {key: 'b', size: 20}], // remove c.
});
},
willLeave() {
// triggered when c's gone. Keeping c until its width/height reach 0.
return {width: spring(0), height: spring(0)};
},
render() {
return (
<TransitionMotion
willLeave={this.willLeave}
styles={this.state.items.map(item => ({
key: item.key,
style: {width: item.size, height: item.size},
}))}>
{interpolatedStyles =>
// first render: a, b, c. Second: still a, b, c! Only last one's a, b.
<div>
{interpolatedStyles.map(config => {
return <div key={config.key} style={{...config.style, border: '1px solid'}} />
})}
</div>
}
</TransitionMotion>
);
},
});
First, two type definitions to ease the comprehension.
TransitionStyle: an object of the format {key: string, data?: any, style: Style}.
key: required. The ID that TransitionMotion uses to track which configuration is which across renders, even when things are reordered. Typically reused as the component key when you map over the interpolated styles.
data: optional. Anything you'd like to carry along. This is so that when the previous section example's c disappears, you still get to access c's related data, such as the text to display along with it.
style: required. The actual starting style configuration, similar to what you provide for Motion's style. Maps keys to either a number or an OpaqueConfig returned by spring().
TransitionPlainStyle: similar to above, except the style field's value is of type PlainStyle, aka an object that maps to numbers.
Required. Accepts either:
an array of TransitionStyle configs, e.g. [{key: 'a', style: {x: spring(0)}}, {key: 'b', style: {x: spring(10)}}].
a function similar to StaggeredMotion, taking the previously interpolating styles (undefined at first call, unless defaultStyles is provided), and returning the previously mentioned array of configs. You can do staggered mounting animation with this.
Optional. Similar to the other components' defaultStyle/defaultStyles.
Required function. Similar to other two components' children. Receive back an array similar to what you provided for defaultStyles, only that each style object's number value represent the currently interpolating value.
Optional. Defaults to () => null. The magic sauce property.
styleThatLeft: the e.g. {key: ..., data: ..., style: ...} object from the styles array, identified by key, that was present during a previous render, and that is now absent, thus triggering the call to willLeave. Note that the style property is exactly what you passed in styles, and is not interpolated. For example, if you passed a spring for x you will receive an object like {x: {stiffness, damping, val, precision}}.
Return: null to indicate you want the TransitionStyle gone immediately. A Style object to indicate you want to reach transition to the specified value(s) before killing the TransitionStyle.
{key: string, data?: any}) => voidOptional. Defaults to () => {}.
styleThatLeft: the {key:..., data:...} that was removed after the finished transition.Optional. Defaults to styleThatEntered => stripStyle(styleThatEntered.style). Where stripStyle turns {x: spring(10), y: spring(20)} into {x: 10, y: 20}.
styleThatEntered: similar to willLeave's, except the TransitionStyle represents the object whose key value was absent during the last render, and that is now present.
Return: a defaultStyle-like PlainStyle configuration, e.g. {x: 0, y: 0}, that serves as the starting values of the animation. Under this light, the default provided means "a style config that has the same starting values as the destination values".
Note that willEnter and defaultStyles serve different purposes. willEnter only triggers when a previously inexistent TransitionStyle inside styles comes into the new render.
(No onRest for TransitionMotion because we haven't found a good semantics for it yet. Voice your support in the issues section.)
Hard-coded duration goes against fluid interfaces. If your animation is interrupted mid-way, you'd get a weird completion animation if you hard-coded the time. That being said, in the demo section there's a great Spring Parameters Chooser for you to have a feel of what spring is appropriate, rather than guessing a duration in the dark.
TransitionMotion container itself?You don't. Unless you put it in another TransitionMotion...
See StaggeredMotion
ref doesn't work in the children function.React string refs won't work:
<Motion style={...}>{currentValue => <div ref="stuff" />}</Motion>
This is how React works. Here's the callback ref solution.