What are the best strategies for utilizing AnimatePresence for smooth and seamless transitions?

In my upcoming app, I am working on creating a seamless entry/exit animation using Framer Motion's AnimatePresence component. While experimenting with the delay feature, I encountered an issue where only one component would animate properly, while the rest would simply disappear without any animation. You can see the expected behavior versus the current behavior in the following links:

Expected & Working behavior

Current & Not Working Behavior

Below is the code snippet for the components:

<AnimatePresence>
                  {isAboutVisible && (
                    <motion.div
                      initial={{ x: 1050, opacity: 0 }}
                      animate={{ x: 0, opacity: 1 }}
                      transition={{
                        duration: 1.5,
                        ...(isSkillsVisible || isProjectsVisible
                          ? { delay: 1.5 }
                          : {}),
                      }}
                      exit={{ x: 1050, opacity: 0 }}
                    >
                      <About />
                    </motion.div>
                  )}
                </AnimatePresence>
                <AnimatePresence>
                  {isSkillsVisible && (
                    <motion.div
                      initial={{ x: 1050, opacity: 0 }}
                      animate={{ x: 0, opacity: 1 }}
                      transition={{
                        duration: 1.5,
                        delay: 1.5,
                      }}
                      exit={{ x: 1050, opacity: 0 }}
                    >
                      <Skills />
                    </motion.div>
                  )}
                </AnimatePresence>
                <AnimatePresence>
                  {isProjectsVisible && (
                    <motion.div
                      initial={{ x: 1050, opacity: 0 }}
                      animate={{ x: 0, opacity: 1 }}
                      transition={{ duration: 1.5, delay: 1.5 }}
                      exit={{ x: 1050, opacity: 0 }}
                    >
                      <Projects />
                    </motion.div>
                  )}
                </AnimatePresence>

Answer №1

All conditions should be wrapped within a single AnimatePresence component and the mode="wait" prop needs to be included

Maybe something like this:

 <AnimatePresence  mode="wait" initial={false}>
          {isAboutVisible && (
            <motion.div
              key="about"
              initial={{ x: 1050, opacity: 0 }}
              animate={{ x: 0, opacity: 1 }}
              transition{{
                duration: 1.5,
                ...(isSkillsVisible || isProjectsVisible ? { delay: 1.5 } : {}),
              }}
              exit={{ x: 1050, opacity: 0 }}
            >
              <About />
            </motion.div>
          )}
          {isSkillsVisible && (
            <motion.div
              key="skills"
              initial={{ x: 1050, opacity: 0 }}
              animate={{ x: 0, opacity: 1 }}
              transition={{
                duration: 1.5,
                delay: 1.5,
              }}
              exit={{ x: 1050, opacity: 0 }}
           >
              <Skills />
            </motion.div>
          )}
          {isProjectsVisible && (
            <motion.div
              key="projects"
              initial={{ x: 1050, opacity: 0 }}
              animate{{ x: 0, opacity: 1 }}
              transition={{ duration: 1.5, delay: 1.5 }}
              exit={{ x: 1050, opacity: 0 }}
           >
              <Projects />
            </motion.div>
          )}
        </AnimatePresence>

And as mentioned by @ivanatias, remember to add a key to each animated element to keep track of its presence

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Join our mailing list for exclusive updates on Angular 6

ingredients : Array additionalIngredients : Array In my code, I have two different methods for subscribing: this.ingredients.valueChanges.subscribe(val=> { console.log(val); } this.additionalIngredients.valueChanges.subscribe(val2=> { console.lo ...

Issues with Angular 2 and Deserialization of .NET List<T>

I'm encountering issues when trying to deserialize a .NET List into an Angular 2 array. An error keeps popping up: ERROR Error: Cannot find a differ supporting object...NgFor only supports binding to Iterables such as Arrays. I've looked around ...

Incorporate helmet into the nextjs middleware stack

Currently, I am working on a full-stack next.js application and my goal is to enhance security by implementing secure headers using the Helmet package. However, I have encountered an issue where the headers are not being added to the response when using ...

When implementing Angular 6, using a shared module within individual lazy-loaded modules can lead to a malfunctioning app when changes are

Hey there, I've encountered a strange problem that didn't occur when I was using Angular 5. Let me explain the situation: In my App routing module, I have: { path: 'moduleA', pathMatch: 'full', loadChildren: &ap ...

What is the best way to create a TypeScript function similar to a 'map' in React?

I recently started learning TS and am having trouble typing this arrow function: const mapLikeGet = (obj, key) => { if (Object.prototype.hasOwnProperty.call(obj, key)) return obj[key] } ...

Angular service is able to return an Observable after using the .then method

I am currently facing an issue with retrieving the authentication status in a service method. Everything seems to be working fine except for the return statement. I am struggling with the usage of .then inside .map and I am unable to figure out how to retu ...

Using NextJS to render a Link component by creating an element

Struggling to use createElement in NextJS to render a Link, but facing issues. Code: import {createElement} from "react"; import Link from "next/link"; createElement( item.href ? Link : "div", { href: item.hre ...

New configuration for NextJS: Redefining redirects in a separate file

Transitioning from my old WordPress site to a fresh, sleek Next.js platform has been an exciting journey. One challenge I've encountered is dealing with the numerous redirects that need to be set up. I prefer keeping things organized and clutter-free, ...

Getting the Most Out of .find() in Angular 4

Despite reading various similar questions, I'm still struggling to make the .find() function work in my application. I have a service with the following API: export class VehicleService { private defUrl = 'API'; constructor(private ht ...

No pathways can be established within Core UI Angular

I've been attempting to use the router link attribute to redirect to a new page, but instead of landing on the expected page, I keep getting redirected to the dashboard. Below is an overview of how my project's structure looks: [![enter image de ...

Adding URL path in Angular 7's .ts file

I currently have the following code in my component's HTML file: <button mat-flat-button class="mat-flat-button mat-accent ng-star-inserted" color="accent" (click)="playVideo(video)"> <mat-icon [svgIcon]="video.type === 'external' ...

Encountered an error with an unexpected token while attempting to upgrade React in a Next

I am looking to update my React 18 in a Next.js application. I followed the guidance provided in the official documentation here. npm install next@latest react@latest react-dom@latest However, when attempting to run npm run build, I encountered the follo ...

`Even though the user has signed in, the NextAuth.js getServerSession function is still returning null

I have been working on implementing a protected route in Next.js using NextAuth.js. Within my API, I am utilizing the getServerSession method to fetch the login user's session. Initially, this works as expected when used inside the getServerSideProps ...

Creating a method that can adopt the return type of the nested function?

I have a function that takes in a callback and returns the value obtained using the useSelector hook from the react-redux library. Is there a way to utilize the return type of useSelector within my wrapper function? import { shallowEqual, useSelector } f ...

typescript - instantiate an object using values stored in an array

Assume we have a model defined as follows. export interface Basicdata { materialnumber: number; type: string; materialclass: string; } We also have an array containing values that correspond directly to the Basicdata model in order, like this: ...

Develop an item with a function that takes an input of the identical type as a variable within the item itself

I am facing a challenge with managing an array of objects that represent commands for my game. Each object includes an input field to define the types of arguments expected by the command. The purpose of this setup is to enable validation on the arguments ...

Implementing Right-to-Left (RTL) support for Shadcn components in Next.js

Currently, I am working with version 14 of the Next.js app. Unfortunately, RTL is not being applied to Shadcn components in my project. In addition, I am utilizing next-intl for supporting multiple languages. Below is a snippet from my layout.tsx file: lay ...

Is it possible to alter the state of one page by clicking a link on another page?

Is it possible to update the state of a different page when a link is clicked and the user navigates to that page? For example: Homepage <Link href="/about"> <button>Click here for the About page</button> </Link> If a ...

Error: Module 'react' not found. Please make sure it is installed and correctly imported

Recently, I've been working on developing a React app using TypeScript. To kickstart the project, I used yarn create react-app (name) --use-pnp --typescript. However, I encountered an issue with the TS linter repeatedly showing the error: Cannot find ...

"Woops! An error occurred with the message: "SassError: Could not locate the specified target selector" while working with SCSS Modules and incorporating

Currently, I am working with Next.js and utilizing SCSS Modules. To incorporate Bootstrap into my project, I opted to install it using the command npm install bootstrap. Following this installation, I went ahead and created a new file titled common.scss wi ...