Skip to main content

Overview

In this guide you add typed hooks to a React app: bind them once with createShipdReact, provide a browser client, identify the signed-in user, and read flags in components. Hooks re-render when a new snapshot arrives or the identity changes. This page builds on Browser. For the App Router, also read Next.js.

Bind hooks once

Call createShipdReact in one client-only module and import hooks from there. Hooks from a different createShipdReact call can’t read this provider.
src/lib/shipd-react.ts
Keep this out of src/generated/flags.ts. Generated files contain types only, so server code can import FlagDefinitions without pulling in React.

Create the client and provider

src/main.tsx
Create the client once, outside components. Creating it during render starts a new client on every render.

Identify from your auth layer

src/components/IdentifyUser.tsx
Render <IdentifyUser /> inside ShipdProvider. Every hook re-renders with the new identity. See Identify users.

Read flags

src/components/CheckoutNav.tsx
Keys are type-checked against the flag type. useFlag("search-variant") and useVariant("new-checkout") are compile errors. All hooks throw if they’re called outside ShipdProvider.

Handle the loading state

Before the first snapshot loads, hooks return defaults. useReady() is false until the snapshot or cached snapshot loads, or readyTimeoutMs elapses. Use it to show a skeleton instead of briefly rendering the default path. If a default is an acceptable first render (for example, false hides an unreleased feature), you can skip useReady(). The component re-renders when the snapshot arrives.

Next