QuickSnip

All snippets

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

Iterate over matching files and run a command on each.

#!/usr/bin/env bash
set -euo pipefail

for file in src/**/*.ts; do
  echo "Processing $file"
  npx prettier --write "$file"
done
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

SQL query to find rows with duplicate values in a column.

SELECT email, COUNT(*) AS occurrences
FROM users
GROUP BY email
HAVING COUNT(*) > 1
ORDER BY occurrences DESC;
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

Undo a git commit while keeping your changes staged.

# Undo the last commit but keep the changes staged
git reset --soft HEAD~1

# Or undo and unstage the changes (working tree preserved)
git reset HEAD~1
Updated Jul 17, 2026

Center a child element both horizontally and vertically.

.center {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}
Updated Jul 17, 2026

Idiomatic Rust file read with proper error propagation.

use std::fs;
use std::io;

fn read_config(path: &str) -> io::Result<String> {
    let contents = fs::read_to_string(path)?;
    Ok(contents)
}

+6 more lines…

Updated Jul 17, 2026

Python one-liner to flatten a list of lists.

def flatten(nested):
    """Flatten an arbitrarily nested list of lists."""
    return [
        item
        for element in nested
        for item in (flatten(element) if isinstance(element, list) else [element])
    ]

+1 more lines…

Updated Jul 17, 2026

Insert a row, or update it if the unique key already exists.

INSERT INTO users (email, name, updated_at)
VALUES ($1, $2, NOW())
ON CONFLICT (email)
DO UPDATE SET name = EXCLUDED.name, updated_at = NOW()
RETURNING id;
Updated Jul 17, 2026

Abort a fetch request if it takes longer than the given timeout.

export async function fetchWithTimeout(url, ms = 5000, options = {}) {
  const controller = new AbortController();
  const timer = setTimeout(() => controller.abort(), ms);
  try {
    const res = await fetch(url, { ...options, signal: controller.signal });
    return await res.json();
  } finally {
    clearTimeout(timer);

+2 more lines…

Updated Jul 17, 2026

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