How to animate unmounting of a child with Framer Motion<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

How to animate unmounting of a child with Framer Motion

Posted on: October 10, 2022

Imagine the situation where you have one parent and a direct child component. The child component has a list of elements. These elements are animated as the parent component is mounted.

framermotion parentChildElements

The parent renders the child that renders the list of elements, and each element is animated in a staggered fashion. The tricky part is how do you animate when the parent component is unmounted to have the children unmounted that trigger the list of elements to be animated oppositely from the mounted animation?

Structure of the Prototype

First, let's define what we need. The application needs to host a component that will remain mounted. This mounted component is called Parent.

1export default function App() {
2 return (
3 <div className="App">
4 <div className="Main">
5 <div className="BoxContainer">
6 <Parent />
7 </div>
8 </div>
9 </div>
10 );

The parent contains a component that will be mounted and unmounted. The component is named Child. That is the one we want to have an animation on many items to fade in and out as the component mount and unmount.

1export const Parent = () => {
2 const [mountParentCount, setParentMountCount] = useState(0);
3 useEffect(() => {
4 console.log("Parent Mounting");
5 return () => {
6 console.log("Parent UnMounting");
7 };
8 });
9 return (
10 <div className="Parent">
11 <Child />
12 <br />
13 <h3>Mounting Parent count: {mountParentCount}</h3>
14 <button
15 onClick={() => {
16 setParentMountCount((prev) => ++prev);
17 }}
18 >
19 Remount Parent
20 </button>
21 </div>
22 );

The Parent component has a useEffect to have some insight into whether the Parent component mounts and unmounts. Otherwise, it has a useState that will force a render of the Parent. Also, there is a button. Every click changes the state making a new render of the Parent. At that point, the Child remains the same as it does not have any change and hence does not mount and unmount on the button click. We will change that soon.

Finally, we need to define the Child. Similarly, we will have a useEffect to see when the component mounts and unmounts.

1export const Child = () => {
2 useEffect(() => {
3 console.log("Child Mounting");
4 return () => {
5 console.log("Child UnMounting");
6 };
7 });
9 return (
10 <ul
11 className="Child"
12 >
13 <li key="Child1">
14 <div>😊</div>
15 </li>
16 <li key="Child2">
17 <div>🥶</div>
18 </li>
19 </ul>
20 );

Mounting and Unmounting

To perform the desired effect of mounting and unmounting the Child component on the button click, React must know that the Child has changed. The usage of key does the trick. Hence, in the Parent container, we will assign a unique key property to the Child component that is crafted with the incremental state.

1<Child key={"unique_" + mountParentCount} />


The animation occurs inside the Child components. The idea is to have each li having an opacity change from 0 to 1 and simultaneously have the li move from right to left with a rotating effect.

React Framer Motion has the concept of Variants that define specific animation by name. We will define two sets of variants. One set that will take care of the opacity, movement and rotation on each LI. The second variant will take care of the UL to ensure each LI animation is not coinciding: we want each `LI to stagger its animation. That way, the visualization is more fluid with each LI starting with a slight delay between each other.

The variants for the LI are called inScreen and outScreen. The name can be anything you like, but you need to be consistent between all your animation sets to ensure they are performed together. In the LI case, we see we are setting the x position to 0, and when the component leaves the screen, it moves to the right of 40 pixels in half a second.

1const variantsList: Variants = {
2 inScreen: {
3 x: 0,
4 rotate: 0,
5 transition: { duration: 0.5 }
6 },
7 outScreen: {
8 x: 40,
9 rotate: 180,
10 transition: { duration: 0.5 }
11 }
12 };

The other set of variants is more complex because it has a staggering effect.

1const variants: Variants = {
2 inScreen: {
3 opacity: 1,
4 transition: {
5 opacity: {
6 duration: 1
7 },
8 staggerChildren: 0.15,
9 delayChildren: 0.05,
10 staggerDirection: 1
11 }
12 },
13 outScreen: {
14 opacity: 0,
15 transition: {
16 opacity: {
17 duration: 1
18 },
19 staggerChildren: 0.15,
20 delayChildren: 0.05,
21 staggerDirection: -1
22 }
23 }
24 };

Nonetheless, the same principle of naming phases exists. In that particular one, you can see that the whole UL has an opacity and that the duration is set for that specific property. What is the most important for this variant is the staggerChildren, which indicates the delay between each LI as well as the delayChildren which indicates when we start batching all the LI. In that case, we wait 50ms before starting to move the first LI, then wait 150ms before starting the second LI.

framermotion parentChildElementsTiming

The illustration above shows when each of the UL and LI gets animated with the above variants.

Framer Motion

To apply the variant, we need to rely on Framer Motion and it means some adjustment. First of all, we cannot use the UL and LI directly. The Child needs to use the motion.ul and, which has additional property like setting the variant and which step to use.

2 className="Child"
3 variants={variants}
4 initial="outScreen"
5 animate="inScreen"
6 exit="outScreen"
8 < key="Child1" variants={variantsList}>
9 <div>😊</div>
10 </>
11 < key="Child2" variants={variantsList}>
12 <div>🥶</div>
13 </>

At this point, the animation works halfway. It should mount with the animation, but when unmounting, no animation occurs. The reason is that we need to tell Framer Motion that we need to execute the animation and not unmount the component directly. Wrapping the Child with AnimatedPresence triggers the exit animation. Here is the modified parent:

1<div className="Parent">
2<AnimatePresence exitBeforeEnter={true}>
3 <Child key={"unique_" + mountParentCount} />
5<br />
6<h3>Mounting Parent count: {mountParentCount}</h3>
8 onClick={() => {
9 setParentMountCount((prev) => ++prev);
10 }}
12 Remount Parent

At this point, unmounting will wait for the exit animation occurs to unmount. The exitBeforeEnter={true} tells of hiding the previous component while the second one is mounting. It ensures that you do not see two components. The reason is that while animating the unmount of the former component (remember we change the key hence different component), we want to start the animation of the mounting of the new component. They occur at the same time and can be in the DOM tree at the same time. Using the exitBeforeEnter property will play the exit animation and then initial of the new component.

Code Sandbox and Final Tips

Here is the complete code. Minor detail: if you use a different transition timing, the basic duration, you may fall into a more complicated visualization bug. For example, if you use one of the many physic transitions, you may have an element that bounces very little for a long time, causing the unmounting to take a long time. Visually, it might be so subtle that you do not realize that something is happening, and you might scratch your head to figure out why the mounting effect takes so long to start. The answer is that the unmounting might take longer. A good debug tip is to use a fixed duration and move with some fancier animation if required.