use-user-settings
v1.0.2
Published
This is a package with use-user-settings. Hook that implements useState-like behaviour working with local storage
Downloads
23
Maintainers
Readme
use-user-settings
Package that allows to easily work with user settings, storing them to local storage Just replace your useState with useUserSettings, and add second parameter - unique key for saving
Pretty simple to use:
import { useUserSettings } from 'use-user-settings';
const Component = () => {
const [view, setView] = useUserSettings('compact', 'main_page_view_type');
return <View type={view} onTypeChange={setView} data={data} />;
};
Params as they are
type UseUserSettingsType = <T>(
defaultValue?: T | (() => T),
key?: string,
prefix?: string
) => [T, (value: SetStateAction<T>) => void];
Params:
defaultValue
- just likeuseState
- you can provide with value or with getter functionkey
- local storage key that will identify the settingprefix
- default =uus_p_
. if you need to specify prefix for your setting keys. Might be useful if you want to wrap this hook in your own like this:
import { useUserSettings } from 'use-user-settings';
export const useOurUserSettings = <T>(
defaultValue?: T | (() => T),
key?: string
) => {
return useUserSettings(defaultValue, key, 'my_project_1');
};