brust 2.0.0-alpha.66
Guide / Store

Store

A store is a named bag of signals that a loader, a native component and a React island all read and write. One name, one value, three surfaces.

Define a store

components/demos/team-store.ts
// components/demos/team-store.ts
import { defineStore, signal } from '@brust/core/store'

export const team = defineStore('team', () => ({
  members: signal<TeamMember[]>([]),
  max: signal(6),
}))

Keep the name free of . and [, and keep the store in its own module, imported by the components that use it. @brust/core/store is React-free.

Seed it on the server

On the server every request gets its own instance. Write it in a loader, and the page carries the values as a snapshot, so the first paint is already right, with or without JavaScript.

pages/TeamPage.tsx
// pages/TeamPage.tsx: the page loader seeds this request's store
export async function teamPageLoader({ req }: LoaderCtx) {
  team.members.set(teamFromCookie(req.cookies.team))
  team.max.set(MAX_TEAM)
  return { ... }
}

This site is built to ship as static files, so it has no request to seed from: its pages are painted with an empty team, and the browser fills the store from its own storage when the demo mounts.

Read it anywhere

useStore from @brust/core/store/react works in both tiers. In a native component the read becomes a live binding; in an island it is useSyncExternalStore over the same browser instance.

components/demos/TeamBadge.tsx
// components/demos/TeamBadge.tsx: native
import { useStore } from '@brust/core/store/react'
import { team } from './team-store'

export default function TeamBadge() {
  const s = useStore(team)
  return <p>Team <b>{s.members.length}</b> / {s.max}</p>
}
components/demos/TeamIsland.tsx
// components/demos/TeamIsland.tsx: react (useTransition makes it an island)
const { members, max } = useStore(team)   // plain values, not signals
useTransition()                           // a hook the native tier does not lower
const [open, toggle] = useReducer(flip, true)   // compiles natively; useTransition is what keeps the island

useStore alone compiles to the native tier. An island needs another React feature to be React, here useTransition. (useReducer compiles natively.)

The native tier reads three shapes: the whole view (const s = useStore(team)), a destructuring (const { members, max } = useStore(team)) and one field (const max = useStore(team).max). A custom hook may read a store as well, also a hook in another module, as long as that module imports the handle: export function useMax() { const max = useStore(team).max; return max } in lib/use-max.ts keeps the components that call it native. A hook whose module declares the store itself (defineStore beside the hook) makes its callers React islands: define the store in a module of its own and import it. Bind the read to a name first: return useStore(team).max is a hook call inside an expression, which is not lowered.

Try it

Three components, two tiers, one store. Add a Pokémon with the native picker or remove one in the React island: the native badge follows both, with no reload. The team is the one from the Actions page.

store “team”TeamBadge · native

native Team 0 / 6

store “team”TeamIsland · react
  • Empty. Add Pokémon on the Actions page or below.
store “team”TeamPicker · native

Team 0 / 6. Add a seventh to see the 409.

Rules and limits

  • In a native component, read only top-level signal(...) keys, write only in handlers and effects, and read through the view (s.members), never team.members() in render. Each rule has a named diagnostic, and breaking one falls back to React.
  • Map, Set and Date serialize to {}, a BigInt throws, functions and undefined are dropped.
  • An island that renders a computed of a store, on a page reached by a navigation, hydrates that computed with the value it had on the first page (the seed tag carries signals only), so it can mismatch the server markup. Render the signals and derive in the component.
  • A store that is already live keeps its client value across a navigation, except for the keys the new page's loaders wrote (.set() in a loader, even to the same value): those take the server's value before the new page mounts. A store the tab has not used yet seeds from the new page's snapshot.
  • ~w is reserved: a store member cannot use that name (the snapshot lists the written keys under it).

brust ships one store of its own, navigation, fed by the navigation events (state, url, to, trigger, status, error, id). Import the handle from @brust/core/navigation and read it through useStore like any other store, in a native component or an island. The name is reserved.

Note

A cached page carries its store snapshot. Do not cache pages that paint per-user store data, such as a team seeded from the reader's cookie.