Unmount animation with ReactJS

Tram Ho

Hello everyone, today I was walking around the internet to see what the Web APIs were, and suddenly I came across something very new. I asked the question: “When can these be used”. After thinking for a long time, I also found the answer

Today I try to create an animation at unmount component like. As everyone knows, adding an animation when mount component is very simple, just use the keyframes of css finish.

However, to add animation when unmount is another story, with jQuery making things easier to breathe, just use fadeIn & fadeOut , but when working with ReactJS developers are often advised not to use jQuery to touch. touch the DOM. Luckily, I also found a satisfactory API that can help me achieve this desire, which is onanimationend .

First you need a css keyframe for this animation:

Create a small small component

After trying this code, every time you reload the page you will see the Mount text with a very nice effect, next I need a button to toggle the display of text again, adding a little bit of App.js

A bit more for fade.js

Now every time you click on the Toggle mount button you will see the animation happening, but the animation when unmount is not available, absurd, I’ve added a keyframe fadeOut like fadeIn .

Analyze a little bit, so what do we do

  • show changes
  • Tell react to unmount , delay it a bit
  • Run animate
  • Animate just finished running, unmount
  • Tell react to unmount , delay it a bit. This is when onanimationend comes into play, this API will automatically start when a keyframe animation finishes, fix fade.js a little more.

Everything is done, please re-code it, I have a demo here – with the addition of reusing the component If you are looking to work with the old browser, you should pay attention because it is not yet fully supported. Thank you for reading

Share the news now

Source : Viblo