react

typescript
import React, { Component, ReactNode } from 'react'

interface Props {
  children: ReactNode
  fallback?: ReactNode
  onError?: (error: Error, errorInfo: React.ErrorInfo) => void

Error boundaries for graceful error handling

react error-handling typescript
by Maya Patel 2 tabs
typescript
import { useEffect, useState } from 'react';

export function useLocalStorage<T>(
  key: string,
  initialValue: T
): [T, (value: T) => void] {

Persisting a Dark-Mode Theme Toggle with a React ThemeProvider and localStorage

react context hooks
by codesnips 4 tabs
javascript
import React, { createContext, useContext, useReducer, useState } from 'react';

// 1. Basic Context
const UserContext = createContext(null);

function UserProvider({ children }) {

State management with Context API and Redux patterns

react redux state-management
by Alex Chang 2 tabs
javascript
import { useState, useEffect, useCallback } from 'react';

export function useLocalStorage(key, initialValue) {
  const readValue = useCallback(() => {
    if (typeof window === 'undefined') return initialValue;
    try {

Persist a React Shopping Cart to localStorage with a Context Provider

javascript react context
by codesnips 3 tabs
typescript
import { useForm } from 'react-hook-form'
import { useState } from 'react'
import api from '@/services/api'

interface SignupFormData {
  email: string

React Hook Form with async validation

react forms validation
by Maya Patel 1 tab
typescript
import { useState } from 'react'
import { useQuery } from '@tanstack/react-query'
import { useDebounce } from '@/hooks/useDebounce'
import api from '@/services/api'

export function SearchInput() {

Debounced search with controlled inputs

react search debounce
by Maya Patel 1 tab
javascript
function encodeCursor(row) {
  if (!row) return null;
  const payload = JSON.stringify({ t: row.created_at, id: row.id });
  return Buffer.from(payload, 'utf8').toString('base64url');
}

Cursor-Paginated REST Endpoint With a React Load More Button

react pagination cursor
by codesnips 4 tabs
typescript
export type FetchState<T> =
  | { status: 'idle' }
  | { status: 'loading' }
  | { status: 'success'; data: T }
  | { status: 'error'; error: Error };

Discriminated-Union State Machine for a React Data-Fetching Hook

react hooks state-machine
by codesnips 3 tabs
typescript
import { memo } from 'react'
import { Post } from '@/types'

interface PostListItemProps {
  post: Post
  onLike: (id: string) => void

React memo for component optimization

react performance optimization
by Maya Patel 2 tabs
javascript
import { useEffect, useState } from "react";

export function useDebounce(value, delay = 300) {
  const [debounced, setDebounced] = useState(value);

  useEffect(() => {

Debounced Search Box With Live Autocomplete in React

react hooks debounce
by codesnips 3 tabs
typescript
export interface Todo {
  id: string;
  title: string;
  completed: boolean;
}

Optimistic UI Updates with Rollback Using React Query useMutation

react react-query optimistic-ui
by codesnips 3 tabs
javascript
const RETRYABLE_STATUS = new Set([408, 429, 500, 502, 503, 504]);

export function fullJitter(attempt, baseDelay = 300, maxDelay = 10_000) {
  const ceiling = Math.min(maxDelay, baseDelay * 2 ** attempt);
  return Math.random() * ceiling;
}

Fetch With Exponential Backoff and Full Jitter Retries

fetch retry exponential-backoff
by codesnips 3 tabs