useAsyncChoices

Source
import { useAsyncChoices } from "@prestojs/viewmodel";
useAsyncChoices(props)

Hook that makes it easy to use an AsyncChoices definition.

This hook does the following:

1) Calls useListProps to get any dependencies for list

2) Calls list and passes through the dependencies and a query object representing any query parameters for the async call

3) Calls useRetrieveProps to get any dependencies for retrieve

4) If there is a current value retrieve is called and is passed the dependencies returned above

5) An object is returned with the available choices in choices, the result of 2 in list and result of 4 in selected

ParameterTypeDescription
props.accumulatePagesboolean

Whether to accumulate pages as more results are fetched. For example if the first page of results is returned, then the next page is fetched then the combined results for the first two pages will be resolved.

This resets whenever query changes or if pagination state changes to anything other than the next page.

If this is true you must specify paginator.

props.queryRecord

Any query string parameters for the request

props.trigger"MANUAL"|"DEEP"

When to trigger the fetch. Defaults to DEEP which means whenever a deep equality check on query or paginator state fails it will refetch.

If set to MANUAL nothing will happen until it changes to DEEP or you call the returned run function. You can use this to defer execution until the value is required.

Defaults to 'DEEP'.

*props.asyncChoicesAsyncChoicesInterface

See [AsyncChoices](doc:AsyncChoices]

props.listOptionsRecord

Any extra options to pass through to list

These will be available in both useListProps and list under the listOptions key

props.onRetrieveError
Function

If provided this function will be called whenever asyncChoices.retrieve errors

You can use this to do things like unset a value if it no longer exists.

props.onRetrieveSuccess
Function

If provided this function will be called whenever a value is successfully resolved using asyncChoices.retrieve.

props.retrieveOptionsRecord

Any extra options to pass through to retrieve

These will be available in both useRetrieveProps and retrieve under the retrieveOptions key

props.valueValueT[]|ValueT|null

The currently selected choice(s) - if any

When specified the corresponding label(s) will be resolved automatically.

UseAsyncChoicesReturn
KeyTypeDescription
choicesReturnType
listUseAsyncListingReturn
selectedUseAsyncValueReturn