react-native-onyx
Version:
State management for React Native
61 lines (60 loc) • 3.33 kB
TypeScript
import type { DependencyList } from 'react';
import type { OnyxKey, OnyxValue } from './types';
type BaseUseOnyxOptions = {
/**
* 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;
};
type UseOnyxInitialValueOption<TInitialValue> = {
/**
* This value will be returned by the hook on the first render while the data is being read from Onyx.
*/
initialValue?: TInitialValue;
};
type UseOnyxSelector<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>> = (data: OnyxValue<TKey> | undefined) => TReturnValue;
type UseOnyxSelectorOption<TKey extends OnyxKey, TReturnValue> = {
/**
* 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 UseOnyxOptions<TKey extends OnyxKey, TReturnValue> = BaseUseOnyxOptions & UseOnyxInitialValueOption<TReturnValue> & UseOnyxSelectorOption<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?: BaseUseOnyxOptions & UseOnyxInitialValueOption<TReturnValue> & Required<UseOnyxSelectorOption<TKey, TReturnValue>>, dependencies?: DependencyList): UseOnyxResult<TReturnValue>;
declare function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(key: TKey, options?: BaseUseOnyxOptions & UseOnyxInitialValueOption<NoInfer<TReturnValue>>, dependencies?: DependencyList): UseOnyxResult<TReturnValue>;
export default useOnyx;
export type { FetchStatus, ResultMetadata, UseOnyxResult, UseOnyxOptions };