Our brain thinks of animations imperatively. For example, to show a container (div), we want to expand the container from zero size to its full dimensions. And when we hide the container, we want to collapse the container from its full dimensions to zero size. The way we describe animations is imperative. How do we translate this to declarative way of writing code in React?

In this article, we will animate the show / hide action of a div container.


Render function

The render function of our App component has the button and the container to animate.

We toggle the button title based on the show state. We want to animate the div with box as className. To animate, we attach different classes to the box container. These classes indicate that the animation is in progress.

Animations using classes

When we click on “Show” button, we set the class to “box-expanding”. After the show animation is complete, we set the class to “box-expanded”

Similarly, when we click on “Hide” button, we set the class to “box-collapsing”. After the hide animation is complete, we set the class to “box-collapsed”.

The handleShowToggle function performs the className changes.

CSS Transitions

The animation duration is 3 seconds or 3000 milliseconds. As we expect, box-expanding and box-collapsing classes have CSS transitions.

For the sake of completeness, we add a few more styles to our app.

To do it the React way, we have to define a class for the duration of the animation. And that class has CSS transitions set in it. In our case, the expanding and collapsing classes have transitions which affect the width and height of the container.

Performing animations in React declaratively
Tagged on: