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
useStatehook to declare a state variableisHoveredand a functionsetIsHoveredto update its value. The initial value ofisHoveredis set tofalse. TheuseStatehook is part of React and allows components to have a local state.Lines 7–9: These lines define a function
handleMouseEnterthat is called when the mouse enters thedivelement. It sets theisHoveredstate variable totrue.Lines 11–13: These lines define a function
handleMouseLeavethat is called when the mouse leaves thedivelement. It sets theisHoveredstate variable tofalse.
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 theisHoveredstate variable istrue.
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
useStatehook to declare a state variableshowComponentand a functionsetShowComponentto update its value. The initial value ofshowComponentis set tofalse.Lines 8–10: These lines define a function
toggleComponentthat is called when the button is clicked. It toggles the value of theshowComponentstate variable.Lines 14–16: The button element is rendered with an
onClickevent handler that triggers thetoggleComponentfunction. The text of the button changes based on the value of theshowComponentstate variable.Lines 17–26: The
CSSTransitioncomponent from'react-transition-group'is used to apply CSS transitions. It wraps the component that should be animated based on theshowComponentstate 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
configthat defines the animation configuration, including properties like mass, tension, and friction.Line 9: This line uses the
useStatehook to declare a state variabletoggleand a functionsetto update its value. The initial value oftoggleis set totrue.Line 10: This line uses the
useTrailhook to create an array of animated values based on the length of theitemsarray and the animation configuration specified in the object.Lines 21–32: The
trail.mapfunction is used to iterate over each item in thetrailarray and render ana.divelement for each item. Thea.divcomponent is an animated version of thedivcomponent provided byreact-spring.Line 24: This line renders the text of each item in the
itemsarray within ana.divcomponent. Theheightstyle is applied to eacha.divcomponent to control the height of the animated trail.
App.css
Lines 17–20: This CSS rule sets the width and height of the
#rootelement, 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-mainclass, 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-textclass, 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
divelements within the.trails-textclass 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 ascx,cy,r, andfill.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 atransitionproperty, 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