npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-settings

v1.0.2

Published

Easily manage application settings in localStorage with a React hook.

Downloads

2

Readme

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