UNPKG

react-native-onyx

Version:

State management for React Native

51 lines (50 loc) 2.63 kB
import type { DependencyList } from 'react'; import type { OnyxKey, OnyxValue } from './types'; type UseOnyxSelector<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>> = (data: OnyxValue<TKey> | undefined) => TReturnValue; type UseOnyxOptions<TKey extends OnyxKey, TReturnValue> = { /** * Determines if this key in this subscription is safe to be evicted. */ canEvict?: boolean; /** * If set to `false`, then no data will be prefilled into the component. */ initWithStoredValues?: boolean; /** * If set to `true`, data will be retrieved from cache during the first render even if there is a pending merge for the key. */ allowStaleData?: boolean; /** * If set to `false`, the connection won't be reused between other subscribers that are listening to the same Onyx key * with the same connect configurations. */ reuseConnection?: boolean; /** * If set to `true`, the key can be changed dynamically during the component lifecycle. */ allowDynamicKey?: boolean; /** * If the component calling this is the one loading the data by calling an action, then you should set this to `true`. * * If the component calling this does not load the data then you should set it to `false`, which means that if the data * is not there, it will log an alert, as it means we are using data that no one loaded and that's most probably a bug. */ canBeMissing?: boolean; /** * This will be used to subscribe to a subset of an Onyx key's data. * Using this setting on `useOnyx` can have very positive performance benefits because the component will only re-render * when the subset of data changes. Otherwise, any change of data on any property would normally * cause the component to re-render (and that can be expensive from a performance standpoint). * @see `useOnyx` cannot return `null` and so selector will replace `null` with `undefined` to maintain compatibility. */ selector?: UseOnyxSelector<TKey, TReturnValue>; }; type FetchStatus = 'loading' | 'loaded'; type ResultMetadata<TValue> = { status: FetchStatus; sourceValue?: NonNullable<TValue> | undefined; }; type UseOnyxResult<TValue> = [NonNullable<TValue> | undefined, ResultMetadata<TValue>]; declare function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(key: TKey, options?: UseOnyxOptions<TKey, TReturnValue>, dependencies?: DependencyList): UseOnyxResult<TReturnValue>; export default useOnyx; export type { FetchStatus, ResultMetadata, UseOnyxResult, UseOnyxOptions, UseOnyxSelector };