Skip to main content

Overview

A complete Vite + React single-page app. It creates one browser client with a client SDK key, reads new-checkout and search-variant with typed hooks, and calls identify after sign-in.

What’s in it

  • src/lib/shipd.ts creates the browser client from import.meta.env.VITE_SHIPD_SDK_KEY and binds typed hooks with createShipdReact<FlagDefinitions>().
  • src/main.tsx wraps the app in ShipdProvider.
  • src/App.tsx signs a user in and out. It calls identify on sign-in and reset on sign-out, and the hooks re-render.
  • The browser client caches the last good snapshot and a stable anonymous id in localStorage, so repeat visits render flags before the network responds.

Files

package.json
tsconfig.json
vite.config.ts
index.html
shipd.config.ts
.env.example
src/vite-env.d.ts
shipd push and shipd types gen write this file. Commit it and do not edit it by hand.
src/generated/flags.ts
src/lib/shipd.ts
src/main.tsx
Replace signIn with your auth library. The important part is calling identify with the user’s id and traits once you know them.
src/App.tsx
src/components/checkout-button.tsx
src/components/search-box.tsx

Run it

Open http://localhost:5173. Pushed flags serve their default. In the dashboard, add a rule to new-checkout that serves true when plan equals pro, then click Save environment. Click Sign in and the checkout link switches without a reload.

Adapt it

  • Call identify wherever your auth state settles (session restore, sign-in callback). Call reset on sign-out.
  • Pass per-component context only for values that aren’t about the user, such as useFlag("new-checkout", { attributes: { cartSize: 3 } }). Per-call attributes override identify traits.
  • The browser client adds country, region, city, and timezone attributes automatically. Pass automaticTraits: false to turn that off.
  • Gate UI with useReady() only where a flash of the default matters. The localStorage cache makes repeat visits ready almost immediately.

Next