How to implement animations in ReactJS applications

Animations play an important role in enhancing the user experience of web applications. React JS provides a powerful platform for creating interactive user interfaces. We can use various techniques and libraries to implement animations in ReactJS applications, such as basic CSS transitions, React transition group, React spring, framer motion, animate.css, using SVG for Animations, etc.

Let's explore some of them.

Basic CSS transitions

CSS transitions are a straightforward way to add simple animations to React components. By defining transition properties such as duration, timing function, and property to animate, you can create smooth transitions when certain conditions change.

Example

Explanation

App.js

  • Line 5: This line uses the useState hook to declare a state variable isHovered and a function setIsHovered to update its value. The initial value of isHovered is set to false. The useState hook is part of React and allows components to have a local state.

  • Lines 7–9: These lines define a function handleMouseEnter that is called when the mouse enters the div element. It sets the isHovered state variable to true.

  • Lines 11–13: These lines define a function handleMouseLeave that is called when the mouse leaves the div element. It sets the isHovered state variable to false.

Styles.css

  • Lines 1–7: These CSS rules define the styling for the class .card. It sets the width, height, background color, transition effect, and padding for the element.

  • Lines 9–16: These CSS rules define the styling for the class .card-hover. It sets the background color, text color, width, height, and transform properties for the element. These styles will be applied when the isHovered state variable is true.

React Transition Group

React Transition Group is a widely-used library that provides a simple way to perform animations when React components enter or exit the DOM. It offers a set of components like Transition, CSSTransition, TransitionGroup, etc., that can be used to define animation behaviors.

Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Your App</title>
</head>
<body>
  <div id="root"></div>
  <div id="dialog-root"></div>
</body>
</html>

Explanation

App.js

  • Line 6: This line uses the useState hook to declare a state variable showComponent and a function setShowComponent to update its value. The initial value of showComponent is set to false.

  • Lines 8–10: These lines define a function toggleComponent that is called when the button is clicked. It toggles the value of the showComponent state variable.

  • Lines 14–16: The button element is rendered with an onClick event handler that triggers the toggleComponent function. The text of the button changes based on the value of the showComponent state variable.

  • Lines 17–26: The CSSTransition component from 'react-transition-group' is used to apply CSS transitions. It wraps the component that should be animated based on the showComponent state variable.

App.css

  • Lines 1–26: These CSS rules define the styling for the button element. The styles include various properties like appearance, text rendering, color, font, padding, border, and more.

React Spring

React Spring is a popular animation library that uses the physics-based animation library called Spring. It provides a declarative way to define animations using hooks and components. React Spring offers various interpolation functions and physics-based behaviors for creating natural and interactive animations.

Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Your App</title>
</head>
<body>
  <div id="root"></div>
  <div id="dialog-root"></div>
</body>
</html>

Explanation

App.js

  • Line 6: This line declares an object config that defines the animation configuration, including properties like mass, tension, and friction.

  • Line 9: This line uses the useState hook to declare a state variable toggle and a function set to update its value. The initial value of toggle is set to true.

  • Line 10: This line uses the useTrail hook to create an array of animated values based on the length of the items array and the animation configuration specified in the object.

  • Lines 21–32: The trail.map function is used to iterate over each item in the trail array and render an a.div element for each item. The a.div component is an animated version of the div component provided by react-spring.

  • Line 24: This line renders the text of each item in the items array within an a.div component. The height style is applied to each a.div component to control the height of the animated trail.

App.css

  • Lines 17–20: This CSS rule sets the width and height of the #root element, which is typically the root element in the HTML where React components are rendered.

  • Lines 22–31: These CSS rules define the styles for the .trails-main class, which is applied to the main container of the component. It sets the position, width, height, overflow, cursor, display, and alignment properties.

  • Lines 33–44: These CSS rules define the styles for the .trails-text class, which are applied to the animated text elements. It sets the position, width, height, line-height, color, font-size, font-weight, text-transform, and overflow properties.

  • Lines 46–48: This CSS rule targets the nested div elements within the .trails-text class and sets the overflow property to hidden.

Animate.css

Animate.css is a popular CSS animation library that provides a collection of pre-defined animation classes. By combining Animate.css with React, you can easily apply CSS animations to your React components.

Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Your App</title>
</head>
<body>
  <div id="root"></div>
  <div id="dialog-root"></div>
</body>
</html>

Explanation

  • Line 6: This line creates a <div> element with the class names "animate__animated" and "animate__bounce". These class names are specific to the "animate.css" library and trigger the "bounce" animation effect on the <div> element.

Using SVG for Animations

SVG (Scalable Vector Graphics) is a powerful technology for creating vector-based graphics and animations. React provides excellent support for integrating SVG animations into your applications. By using libraries like React Spring or Framer Motion, you can animate SVG elements seamlessly.

Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Your App</title>
</head>
<body>
  <div id="root"></div>
  <div id="dialog-root"></div>
</body>
</html>

Explanation

  • Lines 7–61: A series of <svg> elements are rendered, each containing a <motion.circle> component. The <motion.circle> components define SVG circles with different properties, such as cx, cy, r, and fill.

  • Lines 8–14: The <motion.circle> components are configured with initial and animate properties, specifying the initial and target states of the circle animations. In this code, the circles start with an initial opacity and scale of 0 and animate to an opacity of 1 and a scale of 1.

  • Line 15: The <motion.circle> components are also configured with a transition property, which defines the duration of the animation. Each circle has a different duration specified in seconds (1, 2, 3, 4, and 5, respectively).

Conclusion

We have explored various techniques and libraries for implementing animations in React JS applications. From basic CSS transitions to advanced animation libraries like React Spring and Framer Motion, we have a wide range of options to create engaging and interactive user interfaces.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved