useClientDocument
constuseClientDocument: {<TTableDef>(table,id?,options?):UseClientDocumentResult<TTableDef>; <TTableDef>(table,id,options?):UseClientDocumentResult<TTableDef>; }
Defined in: packages/@livestore/react/src/useClientDocument.ts:50
Similar to React.useState but returns a tuple of [state, setState, id, query$] for a given table where …
stateis the current value of the row (fully decoded according to the table schema)setStateis a function that can be used to update the documentidis the id of the documentquery$is aLiveQuerythat e.g. can be used to subscribe to changes to the document
useClientDocument only works for client-document tables:
const MyState = State.SQLite.clientDocument({ name: 'MyState', schema: Schema.Struct({ showSidebar: Schema.Boolean, }), default: { id: SessionIdSymbol, value: { showSidebar: true } },})
const MyComponent = () => { const [{ showSidebar }, setState] = useClientDocument(MyState) return ( <div onClick={() => setState({ showSidebar: !showSidebar })}> {showSidebar ? 'Sidebar is open' : 'Sidebar is closed'} </div> )}If the table has a default id, useClientDocument can be called without an id argument. Otherwise, the id argument is required.
Call Signature
Section titled “Call Signature”<
TTableDef>(table,id?,options?):UseClientDocumentResult<TTableDef>
Type Parameters
Section titled “Type Parameters”TTableDef
Section titled “TTableDef”TTableDef extends Trait<any, any, any, { default: any; partialSet: boolean; }>
Parameters
Section titled “Parameters”TTableDef
typeof SessionIdSymbol | DefaultIdType<TTableDef>
options?
Section titled “options?”Partial<GetOrCreateOptions<TTableDef>>
Returns
Section titled “Returns”UseClientDocumentResult<TTableDef>
Call Signature
Section titled “Call Signature”<
TTableDef>(table,id,options?):UseClientDocumentResult<TTableDef>
Type Parameters
Section titled “Type Parameters”TTableDef
Section titled “TTableDef”TTableDef extends Trait<any, any, any, { default: any; partialSet: boolean; }>
Parameters
Section titled “Parameters”TTableDef
string | typeof SessionIdSymbol | DefaultIdType<TTableDef>
options?
Section titled “options?”Partial<GetOrCreateOptions<TTableDef>>
Returns
Section titled “Returns”UseClientDocumentResult<TTableDef>