React hook to use data from a data provider. Subscribes to run a callback on a data provider's
data with specified selector on the specified data type that data provider serves.
Usage: Specify the data provider and the data type on the data provider with
useData('<data_provider>').<data_type> and use like any other React hook.
@example Subscribing to Verse data at JHN 11:35 on the 'quickVerse.quickVerse' data provider:
@paramdataProviderSource string name of data provider to get OR dataProvider (result of
useDataProvider if you want to consolidate and only get the data provider once)
@paramselector tells the provider what data this listener is listening for
WARNING: MUST BE STABLE - const or wrapped in useState, useMemo, etc. The reference must not be
updated every render
@paramdefaultValue the initial value to return while first awaiting the data
@paramsubscriberOptions various options to adjust how the subscriber emits updates
Note: this parameter is internally assigned to a ref, so changing it will not cause any hooks
to re-run with its new value. This means that subscriberOptions will be passed to the data
provider's subscribe<data_type> method as soon as possible and will not be updated again until
dataProviderSource or selector changes.
@returns[data, setData, isLoading]
data: the current value for the data from the data provider with the specified data type and
selector. This will be the defaultValue, the resolved data, or a PlatformError if the
data provider throws an error. You can call isPlatformError on this value to check if it
is an error.
setData: asynchronous function to request that the data provider update the data at this data
type and selector. Returns true if successful. Note that this function does not update the
data. The data provider sends out an update to this subscription if it successfully updates
data.
isLoading: whether the data with the data type and selector is awaiting retrieval from the data
provider
Throttling. This hook stops a runaway loop that would otherwise lock up the web view. If one
subscription receives — or resubscribes — about 100 times within a second, the hook drops its
subscription for a few seconds, then re-arms and resubscribes on its own. While throttled it
reports data as a PlatformError whose code is RESOURCE_EXHAUSTED, setData as
undefined, and isLoading as true, and it logs a warning naming the data type. Handle it as
you would any other unresolved state; the usual cause is a selector or dataProviderSource
that is rebuilt every render instead of being memoized. (subscriberOptions is held as a ref and
cannot cause this.) The error's message is developer-facing English and is not localized —
branch on the RESOURCE_EXHAUSTED code and supply your own localized text rather than rendering
message to users.
React hook to use data from a data provider. Subscribes to run a callback on a data provider's data with specified selector on the specified data type that data provider serves.
Usage: Specify the data provider and the data type on the data provider with
useData('<data_provider>').<data_type>and use like any other React hook.@example Subscribing to Verse data at JHN 11:35 on the
'quickVerse.quickVerse'data provider:@param
dataProviderSourcestring name of data provider to get OR dataProvider (result of useDataProvider if you want to consolidate and only get the data provider once)@param
selectortells the provider what data this listener is listening forWARNING: MUST BE STABLE - const or wrapped in useState, useMemo, etc. The reference must not be updated every render
@param
defaultValuethe initial value to return while first awaiting the data@param
subscriberOptionsvarious options to adjust how the subscriber emits updatesNote: this parameter is internally assigned to a
ref, so changing it will not cause any hooks to re-run with its new value. This means thatsubscriberOptionswill be passed to the data provider'ssubscribe<data_type>method as soon as possible and will not be updated again untildataProviderSourceorselectorchanges.@returns
[data, setData, isLoading]data: the current value for the data from the data provider with the specified data type and selector. This will be thedefaultValue, the resolved data, or a PlatformError if the data provider throws an error. You can call isPlatformError on this value to check if it is an error.setData: asynchronous function to request that the data provider update the data at this data type and selector. Returnstrueif successful. Note that this function does not update the data. The data provider sends out an update to this subscription if it successfully updates data.isLoading: whether the data with the data type and selector is awaiting retrieval from the data providerThrottling. This hook stops a runaway loop that would otherwise lock up the web view. If one subscription receives — or resubscribes — about 100 times within a second, the hook drops its subscription for a few seconds, then re-arms and resubscribes on its own. While throttled it reports
dataas a PlatformError whosecodeisRESOURCE_EXHAUSTED,setDataasundefined, andisLoadingastrue, and it logs a warning naming the data type. Handle it as you would any other unresolved state; the usual cause is aselectorordataProviderSourcethat is rebuilt every render instead of being memoized. (subscriberOptionsis held as a ref and cannot cause this.) The error'smessageis developer-facing English and is not localized — branch on theRESOURCE_EXHAUSTEDcode and supply your own localized text rather than renderingmessageto users.