Convert Figma logo to code with AI

reactjs logoreact-transition-group

An easy way to perform animations when a React component enters or leaves the DOM

10,155
650
10,155
237

Top Related Projects

23,338

Open source, production-ready animation and gesture library for React

49,575

JavaScript animation engine

A spring that solves your animation problems.

✌️ A spring physics based React animation library

🎊 A collection of animations for inline style libraries

Quick Overview

React Transition Group is a set of components for managing component states (including mounting and unmounting) over time, specifically designed for defining entering and exiting transitions. It exposes simple components useful for defining CSS transitions and animations with React.

Pros

  • Seamless integration with React applications
  • Provides flexible and customizable transition effects
  • Supports both CSS transitions and JavaScript animations
  • Lightweight and easy to use

Cons

  • Limited built-in animation options (mainly focuses on enter/exit transitions)
  • Can be overkill for simple transitions
  • Requires additional CSS or JavaScript for complex animations
  • Learning curve for beginners unfamiliar with React's component lifecycle

Code Examples

  1. Basic CSS Transition:
import { CSSTransition } from 'react-transition-group';

function Example({ in: inProp }) {
  return (
    <CSSTransition in={inProp} timeout={200} classNames="fade">
      <div>Fading content</div>
    </CSSTransition>
  );
}

This example shows a basic fade transition using CSSTransition.

  1. Transition Group for List:
import { TransitionGroup, CSSTransition } from 'react-transition-group';

function List({ items }) {
  return (
    <TransitionGroup>
      {items.map(({ id, text }) => (
        <CSSTransition key={id} timeout={500} classNames="fade">
          <li>{text}</li>
        </CSSTransition>
      ))}
    </TransitionGroup>
  );
}

This example demonstrates how to use TransitionGroup for animating list items.

  1. SwitchTransition for toggling between elements:
import { SwitchTransition, CSSTransition } from 'react-transition-group';

function ToggleExample({ state }) {
  return (
    <SwitchTransition>
      <CSSTransition
        key={state ? "Goodbye" : "Hello"}
        addEndListener={(node, done) => node.addEventListener("transitionend", done, false)}
        classNames='fade'
      >
        <button>
          {state ? "Goodbye" : "Hello"}
        </button>
      </CSSTransition>
    </SwitchTransition>
  );
}

This example shows how to use SwitchTransition for smooth transitions between two elements.

Getting Started

  1. Install the package:

    npm install react-transition-group
    
  2. Import the desired components:

    import { CSSTransition, TransitionGroup, SwitchTransition } from 'react-transition-group';
    
  3. Use the components in your React application, defining the necessary CSS classes for your transitions.

  4. For CSSTransition, make sure to define the following CSS classes:

    .fade-enter { opacity: 0; }
    .fade-enter-active { opacity: 1; transition: opacity 300ms ease-in; }
    .fade-exit { opacity: 1; }
    .fade-exit-active { opacity: 0; transition: opacity 300ms ease-in; }
    

Competitor Comparisons

23,338

Open source, production-ready animation and gesture library for React

Pros of Motion

  • More comprehensive animation capabilities, including gestures and 3D transforms
  • Declarative API that's easier to use for complex animations
  • Better performance optimization with automatic batching and hardware acceleration

Cons of Motion

  • Larger bundle size, which may impact initial load times
  • Steeper learning curve for developers new to animation libraries
  • Some advanced features may require a paid subscription to Framer

Code Comparison

React Transition Group:

<CSSTransition
  in={inProp}
  timeout={200}
  classNames="fade"
>
  <div>I'm a fading element</div>
</CSSTransition>

Motion:

<motion.div
  initial={{ opacity: 0 }}
  animate={{ opacity: 1 }}
  exit={{ opacity: 0 }}
  transition={{ duration: 0.2 }}
>
  I'm a fading element
</motion.div>

Summary

Motion offers more powerful animation capabilities and a more intuitive API compared to React Transition Group. However, it comes with a larger bundle size and potentially steeper learning curve. React Transition Group is lighter and simpler but may require more manual work for complex animations. The choice between the two depends on the project's specific animation needs and performance requirements.

49,575

JavaScript animation engine

Pros of Anime

  • Lightweight and fast, with a small file size
  • Supports a wide range of animation types, including SVG animations
  • Can be used with or without JavaScript frameworks

Cons of Anime

  • Less integrated with React ecosystem
  • May require more manual setup for complex transitions
  • Limited built-in support for enter/exit transitions

Code Comparison

React Transition Group:

<CSSTransition
  in={inProp}
  timeout={200}
  classNames="fade"
  unmountOnExit
>
  <div>I'm a fade transition!</div>
</CSSTransition>

Anime:

anime({
  targets: '.fade-element',
  opacity: [0, 1],
  duration: 200,
  easing: 'easeInOutQuad'
});

Summary

React Transition Group is specifically designed for React applications, offering seamless integration with React components and lifecycle methods. It provides a declarative way to define enter/exit transitions.

Anime, on the other hand, is a more general-purpose animation library that can be used in various contexts, including React applications. It offers a wider range of animation capabilities but may require more setup when used with React.

The choice between the two depends on the specific needs of your project, with React Transition Group being more suitable for React-specific transitions, while Anime offers more flexibility for complex animations across different environments.

A spring that solves your animation problems.

Pros of react-motion

  • Physics-based animations for more natural and fluid motion
  • Declarative API for defining animations
  • Supports complex, interruptible animations

Cons of react-motion

  • Steeper learning curve due to spring-based animation concepts
  • May require more code for simple transitions
  • Performance can be an issue with complex animations

Code Comparison

react-transition-group:

<CSSTransition
  in={inProp}
  timeout={300}
  classNames="fade"
>
  <div>I'm a fade transition!</div>
</CSSTransition>

react-motion:

<Motion
  defaultStyle={{ opacity: 0 }}
  style={{ opacity: spring(1) }}
>
  {interpolatingStyle => (
    <div style={interpolatingStyle}>I'm a fade transition!</div>
  )}
</Motion>

Summary

react-transition-group is simpler to use for basic transitions and is officially maintained by the React team. It's ideal for straightforward enter/exit animations and CSS-based transitions.

react-motion offers more advanced, physics-based animations that can result in more natural-looking motion. It's better suited for complex, interactive animations but may require more effort to implement and optimize.

Choose react-transition-group for simplicity and ease of use, or react-motion for more sophisticated, fluid animations in your React applications.

✌️ A spring physics based React animation library

Pros of react-spring

  • More powerful and flexible animation system
  • Physics-based animations for more natural-looking motion
  • Supports complex, chained animations and gestures

Cons of react-spring

  • Steeper learning curve due to its more complex API
  • Larger bundle size compared to react-transition-group
  • May be overkill for simple transitions

Code Comparison

react-transition-group:

<CSSTransition
  in={inProp}
  timeout={300}
  classNames="fade"
  unmountOnExit
>
  <div>I'm a fade transition!</div>
</CSSTransition>

react-spring:

const props = useSpring({ opacity: toggle ? 1 : 0 })
return (
  <animated.div style={props}>
    I will fade in and out
  </animated.div>
)

react-transition-group is simpler and more straightforward for basic transitions, while react-spring offers more control and flexibility for complex animations. react-spring's physics-based approach allows for more natural-looking animations, but it comes with a steeper learning curve. For projects requiring simple transitions, react-transition-group may be sufficient, while react-spring is better suited for applications needing more advanced animation capabilities.

🎊 A collection of animations for inline style libraries

Pros of react-animations

  • Provides a wide range of pre-defined animations
  • Easy to use with CSS-in-JS libraries like styled-components
  • Lightweight and focused solely on animation definitions

Cons of react-animations

  • Lacks built-in transition components
  • Requires additional setup with CSS-in-JS libraries
  • Less flexibility for complex, multi-step animations

Code Comparison

react-transition-group:

<CSSTransition
  in={inProp}
  timeout={200}
  classNames="fade"
>
  <div>I'm a fade transition!</div>
</CSSTransition>

react-animations:

import styled, { keyframes } from 'styled-components';
import { fadeIn } from 'react-animations';

const fadeInAnimation = keyframes`${fadeIn}`;
const FadeDiv = styled.div`animation: 1s ${fadeInAnimation};`;

Summary

react-transition-group is a more comprehensive solution for managing component transitions, offering built-in components and hooks for complex animations. It's part of the React ecosystem and provides fine-grained control over transition stages.

react-animations, on the other hand, focuses on providing a library of pre-defined animations that can be easily integrated with CSS-in-JS solutions. It's simpler to use for basic animations but may require additional setup and lacks the built-in transition management of react-transition-group.

Choose react-transition-group for more control and complex transitions, or react-animations for quick, pre-defined animations in CSS-in-JS projects.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

react-transition-group npm

ATTENTION! To address many issues that have come up over the years, the API in v2 and above is not backwards compatible with the original React addon (v1-stable).

For a drop-in replacement for react-addons-transition-group and react-addons-css-transition-group, use the v1 release. Documentation and code for that release are available on the v1-stable branch.

We are no longer updating the v1 codebase, please upgrade to the latest version when possible

A set of components for managing component states (including mounting and unmounting) over time, specifically designed with animation in mind.

Documentation

TypeScript

TypeScript definitions are published via DefinitelyTyped and can be installed via the following command:

npm install @types/react-transition-group

Examples

Clone the repo first:

git@github.com:reactjs/react-transition-group.git

Then run npm install (or yarn), and finally npm run storybook to start a storybook instance that you can navigate to in your browser to see the examples.

NPM DownloadsLast 30 Days