UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

47 lines 1.5 kB
/** * Read and write a value from the Store * * useState-like hook using the global Store for persistence. * Each time a store value is changed, all components using this value will be re-rendered. * * @param {string} key Name of the store key. Separate with dots to namespace, e.g. 'posts.list.columns'. * @param {any} defaultValue Default value * * @return {Object} A value and a setter for the value, in an array - just like for useState() * * @example * import { useStore } from 'react-admin'; * * const PostList = () => { * const [density] = useStore('posts.list.density', 'small'); * * return ( * <List> * <Datagrid size={density}> * ... * </Datagrid> * </List> * ); * } * * // Clicking on this button will trigger a rerender of the PostList! * const ChangeDensity: FC<any> = () => { * const [density, setDensity] = useStore('posts.list.density', 'small'); * * const changeDensity = (): void => { * setDensity(density === 'small' ? 'medium' : 'small'); * }; * * return ( * <Button onClick={changeDensity}> * {`Change density (current ${density})`} * </Button> * ); * }; */ export declare const useStore: <T = any>(key: string, defaultValue?: T | undefined) => useStoreResult<T>; export type useStoreResult<T = any> = [ T, (value: T | ((value: T) => void), defaultValue?: T) => void ]; //# sourceMappingURL=useStore.d.ts.map