Skip to main content

useQuery()

Call Signature

function useQuery<RowType>(
query,
parameters?,
options?): QueryResult<RowType>

A hook to access the results of a watched query.

Type Parameters

Type ParameterDefault type
RowTypeany

Parameters

ParameterType
querystring | CompilableQuery<RowType>
parameters?any[]
options?AdditionalOptions

Returns

QueryResult<RowType>

Example

export const Component = () => {
// The lists array here will be a new Array reference whenever a change to the
// lists table is made.
const { data: lists } = useQuery('SELECT * from lists');

return <View>
{lists.map((l) => (
<Text key={l.id}>{JSON.stringify(l)}</Text>
))}
</View>
}

export const DiffComponent = () => {
// Providing a `rowComparator` results in the hook using an incremental query under the hood.
// An incremental query will only emit results when a change to the result set occurs.
// The internal array object references are maintained for unchanged rows.
// The returned lists array is read only when a `rowComparator` is provided.
const { data: lists } = useQuery('SELECT * from lists', [], {
rowComparator: {
keyBy: (item) => item.id,
compareBy: (item) => JSON.stringify(item)
}
});

return <View>
{lists.map((l) => (
<Text key={l.id}>{JSON.stringify(l)}</Text>
))}
</View>
}

Call Signature

function useQuery<RowType>(
query,
parameters?,
options?): ReadonlyQueryResult<RowType>

A hook to access the results of a watched query.

Type Parameters

Type ParameterDefault type
RowTypeany

Parameters

ParameterType
querystring | CompilableQuery<RowType>
parameters?any[]
options?DifferentialHookOptions<RowType>

Returns

ReadonlyQueryResult<RowType>

Example

export const Component = () => {
// The lists array here will be a new Array reference whenever a change to the
// lists table is made.
const { data: lists } = useQuery('SELECT * from lists');

return <View>
{lists.map((l) => (
<Text key={l.id}>{JSON.stringify(l)}</Text>
))}
</View>
}

export const DiffComponent = () => {
// Providing a `rowComparator` results in the hook using an incremental query under the hood.
// An incremental query will only emit results when a change to the result set occurs.
// The internal array object references are maintained for unchanged rows.
// The returned lists array is read only when a `rowComparator` is provided.
const { data: lists } = useQuery('SELECT * from lists', [], {
rowComparator: {
keyBy: (item) => item.id,
compareBy: (item) => JSON.stringify(item)
}
});

return <View>
{lists.map((l) => (
<Text key={l.id}>{JSON.stringify(l)}</Text>
))}
</View>
}