Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to change content of div with animation in ReactJS

Understanding Divs and Animation in ReactJS

Before we dive into the topic, let's clarify a few terms. In the world of web development, a div is a container that can hold other HTML elements. Imagine it as an invisible box where you can place text, images, buttons, or even other boxes.

On the other hand, animation refers to the process of creating motion and shape change. In this context, we'll be using it to change the content of the div smoothly instead of abruptly.

ReactJS, the library we'll be using, is excellent for managing these kinds of dynamic changes.

Setting up a Basic React App

To start, we'll need a basic React application. You can create one by using the command npx create-react-app react-div-animation. This command creates a new React application in a directory named react-div-animation.

npx create-react-app react-div-animation
cd react-div-animation
npm start

Once you've set this up and navigated into the directory, we can start coding!

Animating a Div in React

To animate a div in React, we'll be using a library called react-transition-group. This library allows components to transition in and out of the DOM gracefully.

First, let's install it:

npm install react-transition-group

Now, let's create a simple component that we'll animate.

import React from 'react';

class AnimatedDiv extends React.Component {
  render() {
    return <div>Hello, world!</div>;
  }
}

export default AnimatedDiv;

Using React Transition Group

Import the Transition component from the react-transition-group library in your AnimatedDiv file.

import { Transition } from 'react-transition-group';

Next, we'll wrap our div in the Transition component.

<Transition in={this.state.show} timeout={500}>
  {state => <div className={`box box-${state}`}>Hello, world!</div>}
</Transition>

Here, in is a boolean that determines whether the component should be visible or not and timeout is the duration of the transition. The state variable can have four different values: entering, entered, exiting, and exited. We append this value to the class of our div to apply different styles for each state.

Styling the Animation

For the animation to work, we need to add some CSS. We'll use CSS transitions to animate the opacity and scale of the div.

.box-entering {
  opacity: 0;
  transform: scale(0.9);
}
.box-entered {
  opacity: 1;
  transform: scale(1);
}
.box-exiting {
  opacity: 0;
  transform: scale(0.9);
}
.box-exited {
  opacity: 0;
}

In the code above, when the state is entering or exiting, the div will have an opacity of 0, making it invisible, and it will be slightly scaled down. When the state is entered, the div will be fully visible and not scaled down at all.

Changing the Div Content

Now, let's add a button to change the content of the div and animate it whenever it changes. We'll add a content state to our component and a method to change it.

class AnimatedDiv extends React.Component {
  state = {
    show: true,
    content: 'Hello, world!'
  };

  changeContent = () => {
    this.setState({
      show: false
    });

    setTimeout(() => {
      this.setState({
        content: 'New content!',
        show: true
      });
    }, 500);
  };

  render() {
    // ...
  }
}

In the changeContent method, we first hide the div, then after 500ms (the duration of our transition), we change the content and show the div again.

Conclusion

With a dash of creativity and a pinch of ReactJS, you've created a dynamic, animated div! Like a magician with a hat and rabbit, you're now able to make content appear and disappear, but with a smooth transition that enhances the user experience.

This example is a wonderful demonstration of the power of ReactJS and its ability to work with other libraries. As you continue your coding journey, remember that the possibilities are as vast as your imagination. Happy coding!