QuickSnip

Tag: react

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

A TSX component using the useDebounce hook for search-as-you-type.

"use client";
import { useEffect, useState } from "react";

export function SearchBox({ onSearch }) {
  const [query, setQuery] = useState("");
  useEffect(() => {
    const t = setTimeout(() => onSearch(query), 300);
    return () => clearTimeout(t);

+3 more lines…

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