QuickSnip

Language: TypeScript

A React hook that debounces a rapidly-changing value.

import { useEffect, useState } from "react";

export function useDebounce<T>(value: T, delayMs = 300): T {
  const [debounced, setDebounced] = useState(value);
  useEffect(() => {
    const id = setTimeout(() => setDebounced(value), delayMs);
    return () => clearTimeout(id);
  }, [value, delayMs]);

+2 more lines…

Updated Jul 17, 2026

Safe JSON fetch

TypeScript

Fetch JSON with explicit error handling for non-OK responses.

export async function getJSON<T>(url: string): Promise<T> {
  const res = await fetch(url);
  if (!res.ok) {
    throw new Error(`Request failed: ${res.status} ${res.statusText}`);
  }
  return (await res.json()) as T;
}
Updated Jul 17, 2026

Persist React state to localStorage and keep it in sync.

import { useEffect, useState } from "react";

export function useLocalStorage(key, initial) {
  const [value, setValue] = useState(() => {
    try {
      const raw = window.localStorage.getItem(key);
      return raw ? JSON.parse(raw) : initial;
    } catch {

+8 more lines…

Updated Jul 17, 2026