Issue: Animation not triggered on exit by React-transition-group Transition

I am aiming to create a unique animation where an icon spins 90 degrees to become flat and then seamlessly transitions into another icon, simulating a coin-spin effect.

The desired effect is for the icons to spin both on enter and exit, although there may be a slight delay due to an XHR call.

Interestingly, while the animation successfully triggers on enter, it does not seem to work as expected on exit.

These are the three distinct states of the animation, with the 'in' state being mutually exclusive.

<div className={c.wrapper}>
  <Transition in={loading} timeout={allTransitions}>
    {state => (
      <div style={{ ...defaultStyle, ...transitionStyles[state] }}>
        {(loading) && <SyncIcon className={c.loadingIcon} />}
      </div>
    )}
  </Transition>
  <Transition in={success} timeout={allTransitions}>
    {state => (
      <div style={{ ...defaultStyle, ...transitionStyles[state] }}>
        {(success) && <SuccessIcon className={c.successIcon} />}
      </div>
    )}
  </Transition>
  <Transition in={fail} timeout={allTransitions}>
    {state => (
      <div style={{ ...defaultStyle, ...transitionStyles[state] }}>
        {(fail) && <FailIcon className={c.failIcon} />}
      </div>
    )}
  </Transition>
</div>

Below are the key style definitions:

const duration = 1000
const allTransitions = {
  appear: duration,
  enter: duration,
  exit: duration
}
const easingFunction = 'ease-in-out'

const defaultStyle = {
  transition: `transform ${duration}ms ${easingFunction}`,
  transform: 'rotateY(90deg)',
  alignSelf: 'center'
}

const transitionStyles = {
  entering: { transform: 'rotateY(0deg)' },
  entered: { transform: 'rotateY(0deg)' },
  exiting: { transform: 'rotateY(90deg)' },
  exited: { transform: 'rotateY(90deg)' },
}

You can view a demo of this animation here: https://codesandbox.io/s/coin-state-indicator-3bnv4

Answer №1

The main issue is that the boolean value for loading switches to false too soon, before the exit animation actually begins.

  1. To fix this, consider removing the conditional rendering.
  2. You may also want to adjust the CSS so that all transitions are positioned on top of each other correctly.

For a demonstration, refer to this working sandbox: https://codesandbox.io/s/coin-state-indicator-elutx

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

Creating SEO-friendly slug images in Next.js using the next/image component

Is it possible to change the image URL to a slug URL in Next.js using next/image? Currently, my image URL looks like: http://localhost:3000/_next/image?url=http%3A%2F%2F127.0.0.1%3A8000%2Fmedia%2Fuimages%2F0481%2F2408696.s.jpg&w=1920&q=75 Can I c ...

How can one create a walking person animation using Blender, and what are the necessary coding procedures to export it as a JSON file?

I am in need of displaying a walking person animation on a web page using a character model created in Blender and saved as a JSON file. Can you provide me with detailed steps on how to achieve this? Thank you, ASH ...

Loading Images in Advance with jQuery, Native JavaScript, and CSS

After successfully implementing a method to hover on a small image and load an additional image to the right side of the page, I am now looking to enhance user experience by preloading images. Is there a way to preload an image using JQuery, allowing it t ...

Select Menu (Code Languages:CSS, JS, HTML, BBCode)

I'm currently in the process of setting up a BB code for a forum I moderate. Here's the code snippet I'm using to generate a dropdown box that users can add to the signature field of their profiles: <!DOCTYPE html> <html> <d ...

What are the steps to address unhandled promise rejections?

Issue: UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token o in JSON at position 1 Currently working on a MERN stack application. The signup form is in the Frontend, and below is the POST method for it. const onSignUp = async (e) => { ...

Incorporate props within an event function

I'm currently facing an issue where I need to access props within an event handler. Below is a snippet of my code: class Dashboard extends Component { componentDidMount() { var grid = new Muuri('.grid', { //options... }); ...

Error encountered by React context: TypeError - Attempting to iterate over an object that is not iterable (unable to access property

I'm encountering this error: TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator)) whenever I attempt to handle state using useContext. The purpose here is to initialize "tokens" as an empty array [] on page load, and then ...

What could be causing an unidentified term to be included in the Vue SCSS compilation process

In my Vue application with TypeScript, I encountered an error during compilation that reads as follows: Failed to compile. ./src/style.scss (C:/../node_modules/css-loader/dist/cjs.js??ref--8-oneOf-1-1!C:/.../node_modules/vue-loader/lib/loaders/stylePostL ...

Manipulate classes based on scrolling (Wordpress)

Trying to manipulate a widget by adding and removing classes based on user scroll behavior has proven to be quite challenging. Specifically, I aim to add one class when the user scrolls down by 50px and remove it when they scroll back up. Website: Check o ...

Unable to transmit Props to Component - Fluctuating Behavior

I developed a React.js and Next.js application where I needed to pass the User object to all pages. My plan was to then pass this User component to the Head component in order to display different navigation options based on the user's role. Here is w ...

How can I ensure the end of the list is clearly visible?

I'm experiencing an issue where the last element in my list is getting cut off. When I check the console logs, I can see that it's rendering properly. If I remove the 'position: fixed' style, I can see the element at the bottom of the l ...

Is your script tag not functioning properly with Promises?

Below is the code snippet used to dynamically append scripts in the DOM using promises. This piece of code is executed within an iframe for A-frame technology and it is generated using Google Blockly (Block-based coding). export const appendScript = asy ...

Tips for including and excluding personalized Chips from input

Just started learning React/typescript, any assistance would be greatly appreciated Custom Chip (CC.chip) is a specialized Chip UI component that can be utilized as demonstrated below. const [isOpen, setIsOpen] = useState(true); const onClose = Re ...

My goal is to implement a confirmation modal prior to any route changes within Next.js framework

Below is the code block that I have: const onRouteChangeStart = React.useCallback(() => { if (formState.isDirty) { if (window.confirm('Confirmation message')) { return true; } NProgress.done(); throw "A ...

Enhancing Security Measures in a ReactJS Application on Heroku by Implementing Content Security

I recently encountered a frustrating issue with my CSP policy that I just can't seem to resolve. The setup involves a reactjs application running on Heroku within an expressjs environment. Here's a glimpse of my folder structure: ExpressApp ...

Grids designed in the style of Pinterest

Seeking advice on aligning divs in a Pinterest-style layout. Currently, my setup looks like this: https://i.sstatic.net/erlho.png But I want it to look more like this: https://i.sstatic.net/K9FnD.png Would greatly appreciate any tips or suggestions on ho ...

CSS Causing Scroll Bars to Appear When Browser is Maximized

I've noticed that the scroll bars are still visible even when I maximize the browser window. I don't see any reason for them to be there. There doesn't seem to be a height issue, so the vertical scrollbars shouldn't be appearing, right ...

How can you utilize CSS grid to position an alternate symbol alongside ordinary text?

Does anyone have suggestions on how to align an "alt symbol" with "regular text"? I'm struggling to center both elements without resorting to hacky solutions. Currently, I am using CSS grid, but I am open to exploring other solutions that are easy an ...

Search the database to retrieve a specific value from a multi-dimensional array

My database is structured as shown in the image below: I am attempting to retrieve tasks assigned to a specific user named "Ricardo Meireles" using the code snippet below: const getTasksByEmployee = async () => { setTasks([]); const query = qu ...

What steps should I take to correct the alignment of this grid using CSS?

I'm currently working on creating a grid layout for my website and I've almost achieved the desired result. However, I'm facing an issue with the size of the "Projects" title within the grid. I want it to stand out and be larger compared to ...