UNPKG

qs-state-hook

Version:
43 lines (39 loc) 1.39 kB
import * as react from 'react'; declare type QsLocation = { search: string; }; interface QsStateCreatorOptions { commit?: (args: QsLocation) => void; location?: QsLocation; } declare function useQsStateCreator(options?: QsStateCreatorOptions): { <T>(def: QsStateDefinition<T>): QsStateHookReturn<T | null>; memo<M>(def: QsStateDefinition<M>, deps?: react.DependencyList): QsStateHookReturn<M | null>; }; interface QsStateDefinition<T> { /** Key name to use on the search string */ key: string; /** * Default value. Note that when a state value is default it won't go to the * search string. */ default: T | null; /** * Any transformation to apply to the value from the search string before * using it. Converting to number for example. */ hydrator?: (value?: string) => T | null; /** * Any transformation to apply to the value before adding it to the search. * Converting a number to string for example. */ dehydrator?: (value?: T | null) => string; /** * Validator function for the value. * If is an array should contain valid options. If it is a function should * return true or false. */ validator?: ((value?: T | null) => boolean) | Array<T>; } declare type QsStateHookReturn<T> = [T, (value: T | null) => void]; export { useQsStateCreator as default };