×

React Animation

React Animation

The animation is a procedure in which an image is manipulated to appear as a moving image. It is widely used to create an interactive web application. In React, we have to use an explicit group of components known as the React Transition Group for adding the transition.

The transition group is an add-on component to manage the component states and used for defining the entering andthe exiting transitions. React transition group exposes transition states, manage classes and the elements of the group, and also manipulates the DOM.

The react transition group includes two APIs for creating transitions. These are:

  1. ReactTransitionGroup: It is used as a low-level API for animation.
  2. ReactCSSTransitionGroup: It is used as a high-level API to implement the basic animations and transitions in CSS.

React Transition Group components

The React Transition Group API gives three main components:

  • Transition
  • CSSTranstion
  • TransitionGroup

Transition

It is a simple component API for describing a transition from one component state to another. It is used for animating the mounting and unmounting of a component.

There are four states for accessing the transition component:

  • entering
  • entered
  • exiting
  • exited

CSSTransition

This component uses CSS stylesheet classes for writing the transition and creates animations. It inherits all the props of the transition component.

There are three states of the CSSTransition that are listed as follows:

  • Appear
  • Enter
  • Exit

The CSSTransition component applies to a pair of class names to the child components. The first class is the name-stage and the second class is the name-stage-active.

For example: Suppose you are giving the name as fade, and when it is in the ‘enter’ stage, then the classes will be fade-enter and fade-enter-active.

TransitionGroup

It is used for managing the set of transition components (Transition and CSSTransition) in the list. It is a state machine used to control the mounting and unmounting of components. The “TransitionGroup” component has different animations within a component.

Now, understand how to use the TransitionGroup.

Step by step installation for React Transition Group

Step1: Install React CSS Transition Group

First, we require installing the CSS transition group. This is a React add-on that is used to create basic CSS transitions and animations. You have to run the following command in your command prompt window for installing it.

npm install react-addons-css-transition-group

npm install react-addons-

Step2: Add a CSS file

Now, you have to add a new CSS file appnew.css and to use it in the app; you need to link it with the head element in index.html.

Index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<link rel="stylesheet" type="text/css" href="./appnew.css">
< meta name ="viewport" content ="width = device-width, initial-scale = 1"/> 
<meta name="theme-color" content="#000000" />
<meta name="description" conten<pre class="wp-block-preformatted"><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<link rel="stylesheet" type="text/css" href="./appnew.css">
< meta name ="viewport" content ="width = device-width, initial-scale = 1"/> 
<meta name="theme-color" content="#000000" />
<meta name="description" content="Web site created using create-react-app" /> 
<link rel="apple-touch-icon" href="logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> 
<title>React App</title>
</head>
<body>
<noscript>You have to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html> </pre>t="Web site created using create-react-app" />
<link rel="apple-touch-icon" href="logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>React App</title>
</head>
<body>
<noscript>You have to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>

Step3: Now Appear Animation

Here, we are creating a basic React component. The ReactCSSTransitionGroup element will be treated as a wrapper of the animated component. It will also use the transitionAppear and the transitionAppearTimeout, while transitionEnter and transitionLeave are false.

App.js

importReactfrom'react';
varReactCSSTransitionGroup=require('react-addons-css-transition-group');
classAppextendsReact.Component{
render(){
return(
<div>
<ReactCSSTransitionGrouptransitionName="element" transitionAppear={true}transitionAppearTimeout={5000} transitionEnter={false}transitionLeave={false}>
<h1>HelloWorld:):)</h1>
</ReactCSSTransitionGroup>
</div> 
);
}
}
exportdefaultApp;

appnew.css

.element-appear {
opacity: 0.09;
}
.element-appear.element-appear-active {
opacity: 3;
transition: opacity 10000s ease-in;
} 

Once the application gets started, the element starts to fade in. It is clear from the output given below:

Output:

application gets started

After seconds, the output will be:

element starts to fade in

Now, let us see another example of a transition group in which we will show the Enter and Leave animations in the form of inserting and deleting the elements from the list.

Example:

App.js

import React, { Component } from 'react'; 
import { CSSTransitionGroup } from 'react-transition-group'; 
class App extends React.Component { 
  constructor(props) { 
  super(props); 
  this.state = {names: ['Akash', 'Anup', 'Anil', 'Pawan']}; 
  this.insert = this.insert.bind(this); 
  } 
  insert() { 
  const newInsertednames = this.state.names.concat([  
  prompt('Enter Name of the Employee') 
  ]); 
  this.setState({names: newInsertednames}); 
  } 
  delete(i) { 
  let newInsertednames = this.state.names.slice(); 
  newInsertednames.splice(i, 1); 
  this.setState({names: newInsertednames});  
  } 
  render() { 
  const names = this.state.names.map((item, i) => ( 
  <div key={item} onClick={() => this.delete(i)}> 
  {item} 
  </div> 
  )); 
  return (  
  <div> 
  <h1>Animation Example</h1> 
  <button onClick={this.insert}>Insert the name of Employees</button> 
  <CSSTransitionGroup transitionName="element" transitionEnterTimeout={800} transitionLeaveTimeout={600}>  
  {names} 
  </CSSTransitionGroup> 
  </div> 
  ); 
  } 
 } 
 export default App;

appnew.css

.element-enter {
opacity: 0.03;
}
.element-enter.element-enter-active {
opacity: 1;
transition: opacity 500ms ease-in;
}
.element-leave {
opacity: 1;
}
.element-leave.element-leave-active {
opacity: 0.02;
transition: opacity 300ms ease-in;
} 

Output:

After the execution of the above program, you will get the following output:

 execution of the above program

After the clicking of the button, the prompt will appear and requires the input.

prompt will appear and requires the input.

After clicking the OK button, you will see that the new element will fade in.

see that the new element will fade in.

After the seconds, the element will be inserted completely.

element will be inserted completely.

If you click on the specific element (as element3…), then the corresponding element will fade out from the list.

If you click on the specific element

 When the item gets completely disappear, then the list will be:

When the item gets completely disappear

Related Topics

React Constructors

What is constructor? The constructor is a method that is used for initializing the state of the object in the class. It calls automatically during the creation of an object in...

4 minutes read.

React Code Splitting

React Code Splitting React Code Splitting is the procedure of splitting the bundle files by which the files get easily loaded on the webpage. The react application bundles the files by using...

4 minutes read.

React Router

What is React Router? Routing is a process by which a user can direct to different pages based on their actions and requests. ReactJS router is generally used to develop single...

10 minutes read.

React Component Life-Cycle

React Component Life-Cycle In ReactJS, the creation process of every component includes several lifecycle methods. These methods, together stated as component's lifecycle. Four phases of the component's life cycle are as...

4 minutes read.

React Forms

React Forms Forms are the important part of any web application. It allows the interaction of the user with the app as well as the gathering of the information from the...

3 minutes read.

Difference Between State and Props in React

Comparison between State and Props State The state is an updatable structure which is used for containing data or information about the component. The state in a component can be changed over...

2 minutes read.

React Bootstrap

React Bootstrap React has a widely used JavaScript framework for creating web applications, and Bootstrap has become the most popular CSS framework. Single-page apps have become popular from the last few...

9 minutes read.

React Environment Setup Step by Step

React Environment Setup This section will provide you the information about how to set up an environment for the successful development of ReactJS application: Pre-Requisite for ReactJS NodeJS and NPMReact and React DOMWebpackBabel There are two...

5 minutes read.

React JSX

React JSX As we know, all of the React components include a render function. The Render function specifies the HTML output of a React component. JSX is an acronym of JavaScript extension that...

3 minutes read.

React Lists

React Lists Lists are used for displaying the data in an ordered format and mainly used to display the menus on websites. The creating of lists in React is as similar...

2 minutes read.

React Hooks

React Hooks The Hooks were introduced in React 16.8. By using React Hooks, you can use the state and other features of the React without writing a class. Hooks do not work within the classes....

5 minutes read.

React Animation

React Animation The animation is a procedure in which an image is manipulated to appear as a moving image. It is widely used to create an interactive web application. In React, we have to...

4 minutes read.

React Map

React Map The map() is the standard function of JavaScript, which can be called at any array. The map() method is used for traversing and displaying a list of the similar objects of...

2 minutes read.

Features of ReactJS

ReactJS Features As of now, within the developers, ReactJS is gaining much popularity as the best JavaScript framework. It is crucial for the front-end ecosystem. There are some of the crucial features of ReactJS that are...

2 minutes read.

React Conditional Rendering

React Conditional Rendering In React, the working of the conditional rendering is similar to the condition works in JavaScript. We use JavaScript operators for creating elements that represent the current state, and then the React...

3 minutes read.

React Table

React Table The table represents an arrangement that organizes the information in the form of rows and columns. The table is mainly used for storing and displaying the data within a structured format. Features of...

2 minutes read.

Difference Between React Flux and MVC

React Flux vs MVC MVC MVC is an acronym of 'Model View Controller.' It is the architectural pattern that is used to develop the user interface. It has three different logical components: The...

2 minutes read.

React Refs

React Refs The word ‘Refs’ is an abbreviation of ‘References’ in React. It is as similar as Keys in React. Refs are a function in React which is used for accessing the DOM element and...

7 minutes read.

React Redux

How to Use Redux with React Hooks It is an open-source library of JavaScript which is used for managing the application state. React uses the Redux to build the user interface....

5 minutes read.

React Props

React Props A prop is an abbreviation of ‘properties.' State and props are mainly different from each other because props are immutable. Props are the read-only components. It is the object that stores the attribute...

3 minutes read.