Explore

Explore snippets

Discover reusable code snippets, examples, fixes, and ideas shared by developers, students, and teams.

Public snippets by CodShot user
Clear
13 public snippets
Open
JavaScript
Public

URLSearchParams Filter State

Read, update, and remove page filters without manually concatenating query strings.

JavaScript Preview
function updateFilters(changes) {
  const url = new URL(window.location.href);

  Object.entries(changes).forEach(([key, value]) => {
    const normalizedValue = String(value ?? '').trim();

    if (normalizedValue === '') {
      url.searchParams.delete(key);
#javascript #urlsearchparams #filter #frontend
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #232
Open
TSX
Public

Next.js Route Error Boundary

Handle uncaught route segment errors and let the user retry without a full reload.

TSX Preview
// app/dashboard/error.tsx
'use client';

import { useEffect } from 'react';

type ErrorPageProps = {
  error: Error & {
    digest?: string;
  };
  reset: () => void;
};

export default function DashboardError({
  error,
  reset,
}: ErrorPageProps) {
#nextjs #error-handling #app-router #typescript
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #229
Open
TSX
Public

Next.js Server Action Form with useActionState

Connect a Next.js Server Action to a client form with validation and pending state.

TSX Preview
// app/actions.ts
'use server';

export type CreateUserState = {
  message: string;
  errors: {
    email?: string;
  };
};

export async function createUser(
  previousState: CreateUserState,
  formData: FormData,
): Promise<CreateUserState> {
#nextjs #server-actions #form #typescript
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #222
Open
TSX
Public

React Error Boundary Component

Catch rendering errors in a subtree and provide a retryable fallback UI.

TSX Preview
import {
  Component,
  type ErrorInfo,
  type ReactNode,
} from 'react';

type Props = {
  children: ReactNode;
  fallback?: ReactNode;
};

type State = {
  hasError: boolean;
};

export class ErrorBoundary extends Component<Props, State> {
  state: State = {
#react #error-handling #ui #typescript
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #221
Open
TSX
Public

Typed Context Reducer Pattern

Combine React context and useReducer with typed actions and a safe custom hook.

TSX Preview
'use client';

import {
  createContext,
  type ReactNode,
  useContext,
  useReducer,
} from 'react';

type CartState = {
  itemCount: number;
};

type CartAction =
  | { type: 'add'; quantity: number }
  | { type: 'clear' };
#react #context #reducer #typescript
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #220
Open
TSX
Public

Online Status with useSyncExternalStore

Subscribe to the browser online state through React useSyncExternalStore.

TSX Preview
'use client';

import { useSyncExternalStore } from 'react';

function subscribe(callback: () => void) {
  window.addEventListener('online', callback);
  window.addEventListener('offline', callback);

  return () => {
#react #hooks #state #browser-api
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #219
Open
TSX
Public

Reusable useDebouncedValue Hook

Delay a rapidly changing value before triggering searches, validation, or API requests.

TSX Preview
'use client';

import { useEffect, useState } from 'react';

export function useDebouncedValue<T>(
  value: T,
  delay = 300,
): T {
  const [debouncedValue, setDebouncedValue] = useState(value);

  useEffect(() => {
#react #hooks #debounce #utility
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #218
Open
TSX
Public

Lazy Component Loading with Suspense

Load a large component only when it is rendered and show a fallback during download.

TSX Preview
'use client';

import { lazy, Suspense, useState } from 'react';

const AnalyticsChart = lazy(
  () => import('./analytics-chart'),
);

export function AnalyticsPanel() {
  const [isOpen, setIsOpen] = useState(false);

  return (
    <section>
      <button
#react #suspense #lazy-loading #performance
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #216
Open
TSX
Public

Accessible Form Fields with useId

Generate stable IDs for reusable form controls and accessible help text.

TSX Preview
import { useId } from 'react';

type TextFieldProps = {
  label: string;
  name: string;
  helpText?: string;
};

export function TextField({
  label,
  name,
  helpText,
}: TextFieldProps) {
  const inputId = useId();
  const helpId = `${inputId}-help`;
#react #accessibility #form #typescript
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #215
Open
TSX
Public

Non-Blocking Tabs with useTransition

Switch expensive tab content without blocking urgent UI updates.

TSX Preview
'use client';

import { useState, useTransition } from 'react';

const tabs = ['overview', 'activity', 'settings'] as const;
type Tab = (typeof tabs)[number];

export function DashboardTabs() {
  const [activeTab, setActiveTab] = useState<Tab>('overview');
#react #hooks #performance #ui
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #214
Open
TSX
Public

Deferred Search Results with useDeferredValue

Keep a search input responsive while a larger result list updates at lower priority.

TSX Preview
'use client';

import { useDeferredValue, useMemo, useState } from 'react';

type Product = {
  id: number;
  name: string;
};

export function ProductSearch({ products }: { products: Product[] }) {
  const [query, setQuery] = useState('');
#react #hooks #search #performance
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #213
Open
TSX
Public

Optimistic Todo List with useOptimistic

Show a new todo immediately while the asynchronous save operation completes in the background.

TSX Preview
'use client';

import { startTransition, useOptimistic, useState } from 'react';

type Todo = {
  id: string;
  text: string;
  pending?: boolean;
};

type Props = {
  initialTodos: Todo[];
  createTodo: (text: string) => Promise<Todo>;
};
#react #hooks #state #optimistic-ui
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #212
Open
TSX
Public

React Form Validation with useActionState

Manage form validation, submission status, and server-style action results with React useActionState.

TSX Preview
'use client';

import { useActionState } from 'react';

type FormState = {
  message: string;
  errors: {
    email?: string;
  };
};

const initialState: FormState = {
  message: '',
  errors: {},
};

async function subscribe(
  previousState: FormState,
#react #hooks #form #validation
CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0 #211
CodShot AI Help
Ask about CodShot features, plans, workspace, AI limits, referrals, and public help topics.
Hi! I can help you understand how CodShot works. What would you like to know?
For account-specific or sensitive issues, please open a support ticket. Open support