⚛️

React

Reusable React hooks, components, and patterns for modern frontend development.

20 snippets

Showing 20 of 20 snippets

typescriptintermediate

useFetch — Generic Data Fetching Hook

Custom React hook for data fetching with loading, error, and refetch states using the Fetch API.

#hooks#fetch
typescriptbeginner

useDebounce — Debounced Value Hook

Debounce any rapidly-changing value with a configurable delay. Useful for search inputs and resize handlers.

#hooks#debounce
typescriptintermediate

useLocalStorage — Persistent State Hook

Sync React state with localStorage including SSR safety, JSON serialization, and cross-tab updates.

#hooks#localstorage
typescriptbeginner

useClickOutside — Outside Click Detection

Detect clicks outside a referenced element to close dropdowns, modals, and popover menus.

#hooks#click-outside
typescriptbeginner

useMediaQuery — Responsive Breakpoint Hook

Subscribe to CSS media query changes and get a boolean flag for responsive rendering logic in React.

#hooks#responsive
typescriptintermediate

Error Boundary with Fallback UI

Class-based error boundary component that catches render errors and displays a customizable fallback UI.

#error-boundary#error-handling
typescriptintermediate

Portal-Based Modal Component

Accessible modal component using React portals with focus trapping, Escape key close, and backdrop click.

#modal#portal
typescriptintermediate

Infinite Scroll with Intersection Observer

Load more items as the user scrolls using IntersectionObserver. No external libraries required.

#infinite-scroll#intersection-observer
typescriptadvanced

Optimistic Update Pattern

Apply UI changes immediately before server confirmation with automatic rollback on failure.

#optimistic-update#state
typescriptadvanced

Context + useReducer State Management

Type-safe global state pattern combining React Context with useReducer for complex state without external libs.

#context#reducer
typescriptintermediate

useIntersectionObserver Hook

Track element visibility with the Intersection Observer API for lazy loading and scroll animations.

#hooks#intersection-observer
typescriptbeginner

usePrevious Hook

Track the previous value of any state or prop using a ref-based hook for comparison logic.

#hooks#state
typescriptbeginner

useWindowSize Hook

Track browser window dimensions with debounced resize handling for responsive component logic.

#hooks#responsive
typescriptbeginner

useCopyToClipboard Hook

Copy text to clipboard with success state and automatic reset timer using the Clipboard API.

#hooks#clipboard
typescriptadvanced

Virtualized List Component

Render large lists efficiently by only rendering visible items with calculated scroll positioning.

#virtualization#performance
typescriptintermediate

Drag & Drop Sortable List

Reorder list items with native HTML drag and drop events — no external library required.

#drag-drop#sortable
typescriptadvanced

Compound Component Pattern

Build flexible compound components using React Context for implicit parent-child communication.

#patterns#compound-component
typescriptintermediate

Render Prop Pattern

Share stateful logic between components using the render prop pattern for maximum flexibility.

#patterns#render-prop
typescriptintermediate

useFormValidation Hook

Lightweight form validation hook with field-level errors, touched tracking, and submit handling.

#hooks#forms
typescriptbeginner

Image Lazy Load Component

Lazy load images with a blur-up placeholder effect using Intersection Observer and CSS transitions.

#images#lazy-loading