Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

use-settings

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-settings

Easily manage application settings in localStorage with a React hook.

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

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();

FAQs

Package last updated on 20 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc