useClientDocument
const
useClientDocument: {<TTableDef
>(table
,id?
,options?
):UseStateResult
<TTableDef
>; <TTableDef
>(table
,id
,options?
):UseStateResult
<TTableDef
>; }
Defined in: useClientDocument.ts:50
Similar to React.useState
but returns a tuple of [state, setState, id, query$]
for a given table where …
state
is the current value of the row (fully decoded according to the table schema)setState
is a function that can be used to update the documentid
is the id of the documentquery$
is aLiveQuery
that 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?
):UseStateResult
<TTableDef
>
Type Parameters
Section titled “Type Parameters”TTableDef
Section titled “TTableDef”TTableDef
extends Trait
<any
, any
, any
, { default
: { id
: string
| SessionIdSymbol
; value
: any
; }; partialSet
: boolean
; }>
Parameters
Section titled “Parameters”TTableDef
typeof SessionIdSymbol
| DefaultIdType
<TTableDef
>
options?
Section titled “options?”Partial
<GetOrCreateOptions
<TTableDef
>>
Returns
Section titled “Returns”UseStateResult
<TTableDef
>
Call Signature
Section titled “Call Signature”<
TTableDef
>(table
,id
,options?
):UseStateResult
<TTableDef
>
Type Parameters
Section titled “Type Parameters”TTableDef
Section titled “TTableDef”TTableDef
extends Trait
<any
, any
, any
, { default
: { id
: string
| SessionIdSymbol
| undefined
; value
: any
; }; partialSet
: boolean
; }>
Parameters
Section titled “Parameters”TTableDef
string
| typeof SessionIdSymbol
| DefaultIdType
<TTableDef
>
options?
Section titled “options?”Partial
<GetOrCreateOptions
<TTableDef
>>
Returns
Section titled “Returns”UseStateResult
<TTableDef
>