Introduction to Animations in React

In the last couple of React tutorials, you got familiar with basic React concepts like JSX, routing, and forms. In this tutorial, we'll take it to the next level and try to understand animations in React. 

Getting Started

Create a directory called ReactAnimations. Navigate to the directory and initiate the project using Node Package Manager or npm.

Install react and react-dom to the project. 

We'll be using webpack module bundler for this project. Install webpack and webpack development server.

Install the babel package to convert JSX syntax to JavaScript in our project.

Create a configuration file required by webpack-dev-server where we'll define the entry file, output file, and the babel loader. Here is how webpack.config.js looks:

Create an index.html file where the application will be rendered. Here is how it looks:

Create a file called app.js. Inside app.js import the required react libraries as shown:

Create a stateless component called Home which renders a H1 tag.

Render the Home component inside the app element in the index.html page. Here is how app.js looks:

Save the above changes and start the webpack server. You should have your app running at http://localhost:8080/index.html.

Animations in React

React provides a number of add-on utilities for creating React apps. TransitionGroup and CSSTransitionGroup are the APIs provided for animation.

From the official documentation,

The ReactTransitionGroup add-on component is a low-level API for animation, and ReactCSSTransitionGroup is an add-on component for easily implementing basic CSS animations and transitions.

Appear Animation

Let's start by trying out a simple animation in React. Install the react-addons-css-transition-group to the project.

Import ReactCSSTransitionGroup inside the app.js file.

Inside the Home component that you created, wrap up the h2 tag inside the ReactCSSTransitionGroup tag.

Using the ReactCSSTransitionGroup tag, you have defined the portion where animation would take place. You have specified a name for the transition using transitionName. You have also defined whether the transition appear, enter and leave should happen or not.

Using the transition name defined inside the ReactCSSTransitionGroup, you'll define the CSS classes which would be executed on appear and when in active state. Add the following CSS style to the index.html page.

As you would have noticed, you need to specify the animation duration both in the render method and in the CSS. It's because that's how React knows when to remove the animation classes from the element and when to remove the element from the DOM.

Save the above changes and refresh the page. Once the page has loaded, within a few seconds you should be able to see the animated text.

Enter/Leave Animation

To get a better understanding of the enter and leave animation, we'll create a small React application. The app would have an input text box to enter the name. You'll see how to add the enter animation when a name is added to the list. 

Inside app.js, create a new class called App.

Initialize a data list and a name variable inside the initial state of the component.

Inside the render portion of the App component, place an input text box for entering the name and a button to add the name to the array list.

Define the input handleChange event and the add event inside the App component.

The handleChange event sets the value of the input text box to the name variable. Here is how the add method looks:

Inside the add method, the entered name and a unique ID is pushed to the data array list.

Bind the handleChange and add method in the App component's constructor.

You'll be displaying the entered names inside a list. Modify the render HTML code to add the list.

To animate the newly added items, we'll add the ReactCSSTransitionGroup tag over the li elements.

Add the following CSS transition style to the index.html page. 

Here is the complete App component:

Save the above and refresh the page. Enter a name and enter the add button, and the item should be added to the list with animation.

Similarly, the leave animation can also be implemented in the above code. Once the delete functionality has been implemented in the application, add the leave and leave-active class to the index.html. Set the transitionLeave to True in the ReactCSSTransitionGroup tag in the render method, and you should be good to go. 

Wrapping It Up

In this tutorial, you saw how to get started with using animations in React. You created a simple React app and saw how to implement the appear and enter animation. For in-depth information on animations in React, I would recommend reading the official documentation.

The source code from this tutorial is available on GitHub.

Over the last couple of year, React has grown in popularity. In fact, we’ve a number of items in the marketplace that are available for purchase, review, implementation, and so on. If you’re looking for additional resources around React, don’t hesitate to check them out.

Do let us know your thoughts in the comments below. Have a look at my Envato Tuts+ instructor page for more tutorials on React and related web technologies.



Related Articles