react

typescript
import { useState, useEffect } from 'react'

export function useDebounce<T>(value: T, delay: number = 500): T {
  const [debouncedValue, setDebouncedValue] = useState<T>(value)

  useEffect(() => {

Custom hooks for reusable logic

react hooks typescript
by Maya Patel 3 tabs
typescript
import { createConsumer, Cable } from '@rails/actioncable'

let cable: Cable | null = null

export function getCable(): Cable {
  if (!cable) {

WebSocket integration with Action Cable

react actioncable websockets
by Maya Patel 3 tabs
typescript
import { useState, useMemo, useCallback } from 'react'
import { Post } from '@/types'
import { PostCard } from './PostCard'

interface FilteredPostsProps {
  posts: Post[]

Memoization in React with useMemo and useCallback

react performance memoization
by Maya Patel 1 tab
javascript
export class QueryCache {
  constructor() {
    this.entries = new Map();
  }

  getEntry(key) {

Building a Minimal useQuery Hook with a Shared Cache Provider in React

react hooks caching
by codesnips 4 tabs
typescript
import { useEffect, useState, useRef } from 'react'

interface UseInViewOptions {
  threshold?: number | number[]
  rootMargin?: string
  triggerOnce?: boolean

Intersection Observer API for visibility tracking

react performance intersection-observer
by Maya Patel 2 tabs
typescript
type Loader<T> = () => Promise<T>;

interface Entry<T> {
  value: T;
  expiresAt: number;
}

TTL Cache With In-Flight Request Deduplication for Async Calls

caching async deduplication
by codesnips 4 tabs
typescript
import { Controller, Sse, Headers } from '@nestjs/common';
import { interval, Observable } from 'rxjs';
import { map } from 'rxjs/operators';

interface FeedPayload {
  id: number;

Streaming Server-Sent Events into a Live React List with an EventSource Hook

react sse eventsource
by codesnips 3 tabs
typescript
import { useReducer } from 'react'

interface FormState {
  values: Record<string, any>
  errors: Record<string, string>
  touched: Record<string, boolean>

React useReducer for complex state logic

react hooks state-management
by Maya Patel 1 tab
typescript
export interface UploadHandle {
  promise: Promise<{ status: number; body: string }>;
  xhr: XMLHttpRequest;
}

export function uploadFile(

React File Upload with Live Progress Bar Using XMLHttpRequest

react file-upload xmlhttprequest
by codesnips 3 tabs
typescript
export interface Post {
  id: string;
  title: string;
  updatedAt: string;
}

Deduplicating Paginated API Results With a Normalized Entity Store and Reducer

react reducer normalization
by codesnips 3 tabs
typescript
export type Validator = (value: string) => string | null;

export const required = (message = 'This field is required'): Validator => {
  return (value) => (value.trim().length === 0 ? message : null);
};

Field-Level Form Validation with a Reusable useField Hook in React

react hooks forms
by codesnips 3 tabs
typescript
import { useEffect, useState } from "react";

export function useDebouncedValue<T>(value: T, delay = 800): T {
  const [debounced, setDebounced] = useState<T>(value);

  useEffect(() => {

Debounced Draft Autosave with React Query and a Saving-Status Indicator

react react-query autosave
by codesnips 4 tabs