React Transition State

2.3.3 · active · verified Sun Apr 19

React-Transition-State is a lightweight, zero-dependency library providing a Hook-based API for managing component transition states in React applications. Its primary function is to facilitate CSS-driven animations and transitions by exposing a state machine that tracks a component's lifecycle through various transition phases (e.g., `preEnter`, `entering`, `entered`, `exiting`, `exited`). The library helps in seamlessly mounting and unmounting components from the DOM based on their transition status. The current stable version is 2.3.3, actively maintained with recent updates addressing SSR hydration and supporting modern React versions. It stands out for its minimal bundle size (~1KB post-treeshaking) and efficient single-render state transitions, offering a controlled alternative to libraries like `React Transition Group` without using derived state, which helps prevent common animation-related bugs.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a basic component transition using `useTransitionState`, showing how to toggle visibility and apply CSS classes for animation, including mounting and unmounting.

import React from 'react';
import { useTransitionState } from 'react-transition-state';

function Example() {
  const [state, toggle] = useTransitionState({
    timeout: 750,
    preEnter: true,
    unmountOnExit: true // Ensure component unmounts after exit transition
  });

  return (
    <div>
      {!state.isMounted && (
        <button onClick={() => toggle(true)}>Show Message</button>
      )}
      {state.isMounted && (
        <div className={`example ${state.status}`}>
          <h2>React Transition State Demo</h2>
          <p>This element will animate in and out of view.</p>
          <p>Current status: <code>{state.status}</code></p>
          <button onClick={() => toggle(false)}>Hide Message</button>
        </div>
      )}
    </div>
  );
}

export default Example;

/* Corresponding CSS for this example (not part of code block):
.example {
  transition: all 0.75s ease-in-out;
}
.example.preEnter,
.example.exiting {
  opacity: 0;
  transform: scale(0.5);
}
.example.exited {
  display: none;
}*/

view raw JSON →