react

typescript
import { useMemo } from "react";

export type SortDir = "asc" | "desc";
export interface SortConfig<T> {
  key: keyof T;
  dir: SortDir;

Memoized Async Search With a Cached Selector Hook in React

react hooks usememo
by codesnips 3 tabs
typescript
import React from 'react'
import ReactDOM from 'react-dom/client'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
import App from './App'
import './index.css'

React Query for server state management

react react-query state-management
by Maya Patel 2 tabs
typescript
export type ErrorCode =
  | 'VALIDATION'
  | 'UNAUTHENTICATED'
  | 'FORBIDDEN'
  | 'NOT_FOUND'
  | 'CONFLICT'

API error shape that frontend can rely on

typescript express react
by codesnips 4 tabs
javascript
import { useEffect, useState } from "react";

export function useDebouncedValue(value, delay = 300) {
  const [debounced, setDebounced] = useState(value);

  useEffect(() => {

Debounced Search Input in React with a Reusable useDebouncedValue Hook

react hooks debounce
by codesnips 3 tabs
javascript
export const initialCart = { items: {} };

export function cartReducer(state, action) {
  switch (action.type) {
    case 'ADD_ITEM': {
      const { product, qty = 1 } = action;

Persist and Hydrate a Shopping Cart with useReducer and localStorage

react hooks usereducer
by codesnips 3 tabs
javascript
const DEFAULTS = {
  baseDelay: 300,
  maxDelay: 8000,
  factor: 2,
  jitter: 0.5,
};

Retry Failed React Mutations with Exponential Backoff and a Status Component

react hooks retry
by codesnips 4 tabs
javascript
import { createContext, useContext, useEffect, useMemo, useState } from 'react';
import { fetchCurrentUser, postLogin, postLogout } from './api';

const AuthContext = createContext(null);

export function AuthProvider({ children }) {

Protecting React Routes with an Auth Context and a RequireAuth Wrapper

react react-router authentication
by codesnips 4 tabs
javascript
const BASE_URL = "/api/search";

export async function searchProducts(query, { signal } = {}) {
  const params = new URLSearchParams({ q: query, limit: "10" });
  const res = await fetch(`${BASE_URL}?${params}`, {
    signal,

Cancel Stale Autocomplete Requests with AbortController in a React Hook

react hooks abortcontroller
by codesnips 3 tabs
typescript
import { useCallback, useEffect, useReducer } from "react";

type State = { secondsLeft: number; isRunning: boolean };
type Action =
  | { type: "start"; seconds: number }
  | { type: "tick" }

Resend-Code Button With a Countdown Timer Using useEffect Cleanup

react hooks useeffect
by codesnips 3 tabs
php
<?php

namespace App\Events;

use App\Models\Order;
use Illuminate\Broadcasting\PrivateChannel;

Broadcasting Order Status Updates to a Private Channel with Laravel Echo

laravel broadcasting websockets
by codesnips 4 tabs
javascript
export const MAX_VISIBLE = 4;

const DEFAULTS = {
  variant: "info",
  duration: 4000,
};

Toast Notification Queue in React with Context and useReducer

react context-api usereducer
by codesnips 3 tabs
ruby
module MyApp
  class Application < Rails::Application
    config.load_defaults 6.1

    # API-only mode
    config.api_only = true

Rails API-only app setup for React frontend

rails react api
by Maya Patel 2 tabs