CodShot
TSX Public

Deferred Search Results with useDeferredValue

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

#react #hooks #search #performance
TSX
'use client';

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

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

export function ProductSearch({ products }: { products: Product[] }) {
  const [query, setQuery] = useState('');
  const deferredQuery = useDeferredValue(query);

  const filteredProducts = useMemo(() => {
    const normalizedQuery = deferredQuery.trim().toLowerCase();

    if (!normalizedQuery) {
      return products;
    }

    return products.filter((product) =>
      product.name.toLowerCase().includes(normalizedQuery),
    );
  }, [deferredQuery, products]);

  const isUpdating = query !== deferredQuery;

  return (
    <section>
      <input
        value={query}
        onChange={(event) => setQuery(event.target.value)}
        placeholder="Search products"
      />

      <div style={{ opacity: isUpdating ? 0.55 : 1 }}>
        {filteredProducts.map((product) => (
          <article key={product.id}>{product.name}</article>
        ))}
      </div>
    </section>
  );
}

Snippet actions

CodShot user
CodShot user
Updated: 2026-08-06 18:36
Public snippets
0
Forks
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