use-settings
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

useSettings

Easily manage application settings in localStorage with a React hook.

Features

  • Declaritive approach to mutating settings.
  • NextJS friendly
  • Helper functions for easily managing setting types, like toggleSetting

Installation

yarn add use-settings

or

npm install use-settings

Usage

First, you need to wrap any consuming components with the SettingsProvider. We use this so that you can set a global localStorage key (among other things).

import { SettingsProvider } from 'use-settings';

const App = () => {
    return (
        <SettingsProvider name='my-settings-key' initial={{
            color: 'cyan'
        }}>
            // the rest of your components
        </SettingsProvider>
    );
}

Now, just import the useSettings hook in any component that needs it:

import useSettings from 'use-settings';

const MyComponent = () => {
    const { settings, updateSetting } = useSettings();

    // ...
}

Docs

This section will explain the provider and the hook and its exported values.

SettingsProvider

We don't recommend wrapping your entire <App /> directly with the SettingsProvider. Even though the props you pass into it probably won't change, we don't want to re-render the entire app unnecessarily. Therfore, keep the SettingsProvider as close to the components that need the hook as possible. That is, if your navigation bar is the only component that needs the hook, wrap it with the provider, not anything above it.

The provider accepts two props: name and initial

name

This is the name (or key value) of the settings object in localStorage. This sets the value globally.

initial

This is the initial value of the settings object. It's used in the event that the existing settings aren't found in localStorage and in the reset function explained later.

useSettings

All of the values returned from the hook can be seen below:

const {
    settings,
    updateSetting,
    toggleSetting,
    reset
} = useSettings();

settings

The current settings object. You can access items with dot notation or by the object key pattern:

console.log(settings.color);

or

console.log(settings['color']);

Note: Do not try to update a value directly. Remember, state should never be mutated directly in React. It may seem to work, but you'll run into problems later.

updateSettings

Allows you to modify an arbitrary amount of settings in object format.

updateSettings({
    color: 'cyan',
    index: 100
});

The resulting object is merged with the one you provide. Therefore, if your settings object looks like:

{
    dark: true,
    color: 'green',
    index: 1000,
}

and you call updateSettings with:

updateSettings({
    color: 'cyan',
    index: 100
});

then the dark key in the settings is not overwritten.

updateSetting

A function that modifies a single setting value.

updateSetting('color', 'cyan');

toggleSetting

A function that toggles a setting between true and false. If no value is found (if it doesn't exist in the settings object yet), it'll default to true initially.

toggleSetting('dark');

reset

A function that overwrites the existing settings object with the value you provide. If you don't provide a value, it defaults to the initial object you gave the SettingsProvider

reset({
    color: 'green'
});

or simply:

reset();

Readme

Keywords

none

Package Sidebar

Install

npm i use-settings

Weekly Downloads

1

Version

1.0.2

License

MIT

Unpacked Size

22.3 kB

Total Files

18

Last publish

Collaborators

  • infinium