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.
npm install react-transition-stateVerified import paths — ran on the pinned version, not inferred.
Demonstrates a basic component transition using `useTransitionState`, showing how to toggle visibility and apply CSS classes for animation, including mounting and unmounting.
Migrate all calls from `useTransition` to `useTransitionState`.
Update usage from `const [status, toggle] = useTransitionState(...)` to `const [{ status, isMounted }, toggle] = useTransitionState(...)` and access `status` and `isMounted` from the object.Upgrade to version 2.3.3 or newer to resolve known SSR hydration bugs. Ensure `setTimeout` calls are correctly wrapped if custom timing logic is used in SSR environments.
Use the `isMounted` property from the hook's return object to conditionally render the component, especially if `unmountOnExit: true` is set. Pass `$status` or similar prop to styled components and define styles based on it.
Replace `useTransition` with `useTransitionState` in your imports and usage.
Destructure the returned object to access `status` and `isMounted`: `const [{ status, isMounted }, toggle] = useTransitionState(...)`.Upgrade `react-transition-state` to version 2.3.3 or higher. If using custom `setTimeout` logic, ensure it's handled correctly in both client and server environments.