TanStack Hotkeys works in server-rendered and static React apps without hydration errors. Use the setup that matches how your app renders:
| App setup | What to configure |
|---|---|
| Browser-only SPA (such as Vite) | Nothing needed |
| SSR rendered per request | Pass detectPlatform(userAgent) to HotkeysProvider (details) |
| Static or prerendered pages | Render labels in a client-only wrapper, or allow Linux fallback until hydration (details) |
| React Server Components passing appHotkeys | Render AppHotkeysProvider instead of HotkeysProvider (details) |
Because a server has no navigator, server-rendered labels default to 'linux' (Ctrl+S). Without telling the provider which operating system sent the request, macOS users see Ctrl+S in server HTML before switching to ⌘ S on the client.
To render the right labels from the first byte:
import { HotkeysProvider, detectPlatform } from '@tanstack/react-hotkeys'
<HotkeysProvider defaultOptions={{ platform: detectPlatform(userAgent) }}>
<App />
</HotkeysProvider>import { useFormatForDisplay } from '@tanstack/react-hotkeys'
function Toolbar() {
const formatForDisplay = useFormatForDisplay()
return (
<ul>
{commands.map((command) => (
<li key={command.id}>
{command.name} <kbd>{formatForDisplay(command.hotkey)}</kbd>
</li>
))}
</ul>
)
}Note
Caching. Pages personalized by User-Agent shouldn't be shared across users in a CDN cache unless you vary or key the cache on platform. Vary: User-Agent works, though keying directly by 'mac', 'windows', or 'linux' avoids splitting cache entries by browser version.
createIsomorphicFn reads the request header on the server and navigator on the client, so no route loader is needed:
// src/routes/__root.tsx
import { Outlet } from '@tanstack/react-router'
import { createIsomorphicFn } from '@tanstack/react-start'
import { getRequestHeader } from '@tanstack/react-start/server'
import { HotkeysProvider, detectPlatform } from '@tanstack/react-hotkeys'
const getPlatform = createIsomorphicFn()
.server(() => detectPlatform(getRequestHeader('user-agent')))
.client(() => detectPlatform())
function RootComponent() {
return (
<HotkeysProvider defaultOptions={{ platform: getPlatform() }}>
<Outlet />
</HotkeysProvider>
)
}See the TanStack Start example.
Read headers() in your root layout (a Server Component) and pass the resolved platform into HotkeysProvider (a Client Component):
// app/layout.tsx
import { headers } from 'next/headers'
import { HotkeysProvider, detectPlatform } from '@tanstack/react-hotkeys'
export default async function RootLayout({ children }: { children: React.ReactNode }) {
const userAgent = (await headers()).get('user-agent')
return (
<html lang="en">
<body>
<HotkeysProvider defaultOptions={{ platform: detectPlatform(userAgent) }}>
{children}
</HotkeysProvider>
</body>
</html>
)
}Call useFormatForDisplay() inside 'use client' components. For Server Components, call formatForDisplay directly (see Format outside a component). Because calling headers() opts routes into dynamic rendering, see Static and prerendered pages if a route must stay static.
See the Next.js example.
Detect the platform in the root loader and pass loaderData.platform into HotkeysProvider:
// app/root.tsx
import { Outlet } from 'react-router'
import { HotkeysProvider, detectPlatform } from '@tanstack/react-hotkeys'
import type { Route } from './+types/root'
export function loader({ request }: Route.LoaderArgs) {
return { platform: detectPlatform(request.headers.get('user-agent')) }
}
export default function App({ loaderData }: Route.ComponentProps) {
return (
<HotkeysProvider defaultOptions={{ platform: loaderData.platform }}>
<Outlet />
</HotkeysProvider>
)
}See the React Router example.
Each React island is a separate React root, so render HotkeysProvider inside the island and pass platform from an on-demand page (prerender = false or output: 'server'):
---
// src/pages/index.astro
import { detectPlatform } from '@tanstack/react-hotkeys'
import Shortcuts from '../components/Shortcuts'
export const prerender = false
const platform = detectPlatform(Astro.request.headers.get('user-agent'))
---
<Shortcuts client:load platform={platform} />// src/components/Shortcuts.tsx
import { HotkeysProvider } from '@tanstack/react-hotkeys'
import type { HotkeyPlatform } from '@tanstack/react-hotkeys'
export default function Shortcuts({ platform }: { platform: HotkeyPlatform }) {
return (
<HotkeysProvider defaultOptions={{ platform }}>
<CheatSheet />
</HotkeysProvider>
)
}See the Astro example.
To skip rendering labels during SSR, wrap the label component so it renders only after hydration:
'use client'
import { useSyncExternalStore } from 'react'
import { useFormatForDisplay } from '@tanstack/react-hotkeys'
import type { Hotkey } from '@tanstack/react-hotkeys'
const subscribe = () => () => {}
function ClientOnly({ children, fallback = null }: { children: React.ReactNode; fallback?: React.ReactNode }) {
const hydrated = useSyncExternalStore(subscribe, () => true, () => false)
return hydrated ? children : fallback
}
// Formats inside the client-only boundary rather than in the parent
function ShortcutLabel({ hotkey }: { hotkey: Hotkey }) {
const formatForDisplay = useFormatForDisplay()
return <kbd>{formatForDisplay(hotkey)}</kbd>
}
<ClientOnly fallback={<kbd> </kbd>}>
<ShortcutLabel hotkey="Mod+S" />
</ClientOnly>Make sure useFormatForDisplay() runs inside the child component. Otherwise the parent already formatted it on the server. Built-in helpers like TanStack Router's <ClientOnly>, next/dynamic with ssr: false, and Astro's client:only="react" work the same way. Give the fallback a fixed width placeholder to prevent layout shift.
Pages generated at build time, including prerendered TanStack Start or React Router routes, Next.js output: 'export', and default static Astro pages, have no request headers. Without a platform, useFormatForDisplay shows 'linux' labels until hydration and switches on the client. To hide labels until hydration, wrap them in a client-only component as shown above.
In development, formatting a label on the server without a platform logs a one-time warning so you catch the 'linux' fallback even if you develop on Linux. The check is stripped from production builds.
In React Server Component frameworks like the Next.js App Router, passing appHotkeys from a Server Component directly into HotkeysProvider won't initialize the client app-hotkeys store, because defineAppHotkeys only ran on the server.
Render AppHotkeysProvider instead. It accepts the same props and initializes the client store automatically:
// app/layout.tsx
import { AppHotkeysProvider } from '@tanstack/react-hotkeys'
import { appHotkeys } from './app-hotkeys'
import type { ReactNode } from 'react'
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html>
<body>
<AppHotkeysProvider appHotkeys={appHotkeys}>{children}</AppHotkeysProvider>
</body>
</html>
)
}AppHotkeysProvider also accepts saved customHotkeys from cookies or a database. Because functions can't cross the server and client boundary, render the provider from a 'use client' module that imports the declarations if you pass onCustomHotkeysChange or functions inside meta.