What is react-motion?

React-motion is a library that is used to create animated user interfaces. It provides an API which simplifies the task of animating components in React. React-motion has three main components:

  1. Motion
  2. StaggeredMotion
  3. TransitionMotion

(The function spring() is also frequently used.)

To start, we’ll only look at how Motion and spring() work; therefore, it is necessary to write the following import statement:

import { Motion, spring } from "react-motion";

Props

The Motion component receives two props:

  1. defaultStyle: This prop is an object that contains the starting values for all the properties of the component that we want to change during animation(e.g., the place of a text, font size, font color, etc.).

  2. style: This prop is also an object, but it contains the destination values of the properties specified in defaultStyle. These destination values may be specified with, or without, the spring() function.

spring()

The spring() function indicates that the destination is to be reached slowly, with a transition, instead of instantly. The first parameter of this function is the destination value, while the optional second parameter* is an object which specifies the stiffness and damping of the transition; skipping this parameter sets stiffness equal to 170 and damping equal to 26.

Stiffness refers to the speed with which the destination value is reached; damping is the friction encountered by the animated component during the transition.

Child

The Motion component receives a function as its child, ​which is used to apply the updated value to a React component. This function is automatically passed as an object which contains the updated values as its parameter. This value is then used to modify the property, which is to be animated.

Basic parts of <Motion>
Basic parts of <Motion>

Code

The following example shows how to animate the font size of three different <p> tags, using variables x, y and z:

import React from 'react';

import ReactDOM from 'react-dom';
import App from './app.js';

ReactDOM.render(
  <App />, 
  document.getElementById('root')
);

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved