CodShot
TSX Public

Non-Blocking Tabs with useTransition

Switch expensive tab content without blocking urgent UI updates.

#react #hooks #performance #ui
TSX
'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');
  const [isPending, startTransition] = useTransition();

  function selectTab(tab: Tab) {
    startTransition(() => {
      setActiveTab(tab);
    });
  }

  return (
    <section>
      <nav aria-label="Dashboard sections">
        {tabs.map((tab) => (
          <button
            key={tab}
            type="button"
            aria-pressed={activeTab === tab}
            onClick={() => selectTab(tab)}
          >
            {tab}
          </button>
        ))}
      </nav>

      <div aria-busy={isPending}>
        {isPending && <p>Updating view…</p>}
        <ExpensivePanel tab={activeTab} />
      </div>
    </section>
  );
}

function ExpensivePanel({ tab }: { tab: Tab }) {
  return <div>Current tab: {tab}</div>;
}

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