Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Medusa
Medusa

Posted on

     

Easily animate React components when mount/unmount 😅

Example 1

One of the things that I have neglected the most as a React programmer when it comes to animations is the transitions when mounting and unmounting a component. Normally I alwaysleft without animations because I didn't see an easy and comfortable way to put them either. The result is quite forced and the experience for the user is that things appear and disappear by magic, nothing fluid.

Until a couple of days ago I decided to investigate the best and simplest way to run animations when mounting and unmounting components. I came to the conclusion that the best thing was tocreate a generic component that allows mounting and unmounting components through the simplest possible syntax.

I started creating a component that would manage the animation of the component and take care of mounting and unmounting it when indicated. The animation had to be done using theCSS keyframes, so I came up with a solution like this simple example:

Instead of this (mount/unmount without animation):

constMyComponent=()=>{const[isMounted,setIsMounted]=useState(false);...return(<>{isMounted&&(<div>          Hi World!</div>)}</>)...
Enter fullscreen modeExit fullscreen mode

We do this (same with animation):

importAnimatedfrom"react-mount-animation";constMyComponent=()=>{const[isMounted,setIsMounted]=useState(false);...return(<Animated.div//You can use any HTML element hereshow={isMounted}mountAnim={`             0% {opacity: 0}            100% {opacity: 1}        `}>        Hi World!</Animated.div>)...
Enter fullscreen modeExit fullscreen mode

Anyone who wants to collaborate on it can contact me and any technical or non-technical opinion I will receive with pleasure. Maybe this package does not make sense, maybe it does, in any case I would love for you to leave a comment.

If you want toinspect the code or simplytest it, you can do it by consulting the repository:https://github.com/mijim/react-mount-animation

https://www.npmjs.com/package/react-mount-animation

Thanks & Happy Coding! ⚡️

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
kellygue profile image
Kelly Guelcé
Passionate coder exploring the realms of tech 🚀Web development enthusiast 💻Always up for a coding challenge! Let's connect and build awesome things together
  • Email
  • Location
    Gatineau, QC
  • Education
    University of Ottawa
  • Pronouns
    He/him
  • Work
    Software Developer at the University of Ottawa
  • Joined
• Edited on• Edited

Very useful
Thank you !!! :)

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A software engineer in 💜 with generative art.
  • Location
    Remote
  • Education
    Bachelor's degree in computer engineering
  • Work
    Frontend engineer at Freelance
  • Joined

More fromMedusa

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp