typescriptadvanced
useTransitionGroup Hook
Manages enter/exit animations for list items with staggered transitions.
typescriptPress ⌘/Ctrl + Shift + C to copy
import { useState, useEffect, useRef } from 'react';
interface TransitionItem<T> {
item: T;
state: 'entering' | 'entered' | 'exiting';
key: string;
}
export function useTransitionGroup<T extends { id: string }>(items: T[], duration = 300) {
const [rendered, setRendered] = useState<TransitionItem<T>[]>([]);
const prevItems = useRef<T[]>([]);
useEffect(() => {
const prevIds = new Set(prevItems.current.map(i => i.id));
const currentIds = new Set(items.map(i => i.id));
const entering = items.filter(i => !prevIds.has(i.id))
.map(item => ({ item, state: 'entering' as const, key: item.id }));
const staying = items.filter(i => prevIds.has(i.id))
.map(item => ({ item, state: 'entered' as const, key: item.id }));
const exiting = prevItems.current.filter(i => !currentIds.has(i.id))
.map(item => ({ item, state: 'exiting' as const, key: item.id }));
setRendered([...staying, ...entering, ...exiting]);
const timer = setTimeout(() => {
setRendered(prev => prev
.filter(i => i.state !== 'exiting')
.map(i => ({ ...i, state: 'entered' }))
);
}, duration);
prevItems.current = items;
return () => clearTimeout(timer);
}, [items, duration]);
return rendered;
}Use Cases
- Animated list additions/removals
- Toast notifications
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptintermediate
useFetch — Generic Data Fetching Hook
Custom React hook for data fetching with loading, error, and refetch states using the Fetch API.
Best for: API data fetching
#hooks#fetch
typescriptbeginner
useDebounce — Debounced Value Hook
Debounce any rapidly-changing value with a configurable delay. Useful for search inputs and resize handlers.
Best for: Search-as-you-type
#hooks#debounce
typescriptintermediate
useLocalStorage — Persistent State Hook
Sync React state with localStorage including SSR safety, JSON serialization, and cross-tab updates.
Best for: User preferences
#hooks#localstorage
typescriptbeginner
useClickOutside — Outside Click Detection
Detect clicks outside a referenced element to close dropdowns, modals, and popover menus.
Best for: Dropdown menus
#hooks#click-outside