useStore
useStore<
TSchema,TContext,TSyncPayloadSchema>(options):Store<TSchema,TContext> &ReactApi
Defined in: react/src/useStore.ts:54
Returns a store instance augmented with hooks (store.useQuery() and store.useClientDocument()) for reactive queries.
Type Parameters
Section titled “Type Parameters”TSchema
Section titled “TSchema”TSchema extends LiveStoreSchema<DbSchema, EventDefRecord>
The schema type for the store
TContext
Section titled “TContext”TContext = { }
TSyncPayloadSchema
Section titled “TSyncPayloadSchema”TSyncPayloadSchema extends Schema<any, any, never> = Schema<JsonValue, JsonValue, never>
Parameters
Section titled “Parameters”options
Section titled “options”RegistryStoreOptions<TSchema, TContext, TSyncPayloadSchema>
Returns
Section titled “Returns”Store<TSchema, TContext> & ReactApi
The loaded store instance augmented with React hooks
Example
Section titled “Example”function Issue() { // Suspends until loaded or returns immediately if already loaded const issueStore = useStore(issueStoreOptions('abc123')) const [issue] = issueStore.useQuery(queryDb(tables.issue.select()))
const toggleStatus = () => issueStore.commit( issueEvents.issueStatusChanged({ id: issue.id, status: issue.status === 'done' ? 'todo' : 'done', }), )
const preloadParentIssue = (issueId: string) => storeRegistry.preload({ ...issueStoreOptions(issueId), unusedCacheTime: 10_000, })
return ( <> <h2>{issue.title}</h2> <button onClick={() => toggleStatus()}>Toggle Status</button> <button onMouseEnter={() => preloadParentIssue(issue.parentIssueId)}>Open Parent Issue</button> </> )}Remarks
Section titled “Remarks”- Suspends until the store is loaded.
- Store is cached by its
storeIdin theStoreRegistry. Multiple calls with the samestoreIdreturn the same store instance. - Store is cached as long as it’s being used, and after
unusedCacheTimeexpires (default60_000ms in browser,Infinityin non-browser) - Default store options can be configured in
StoreRegistryconstructor. - Store options are only applied when the store is loaded. Subsequent calls with different options will not affect the store if it’s already loaded and cached in the registry.
Throws
Section titled “Throws”unknown - store loading error or if called outside <StoreRegistryProvider>