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-state-plus

v1.0.4

Published

An extension of React's useState hook.

Downloads

5

Readme

useState+

useState+ is an extension of React's useState hook.

This package aims to improve the ease of state manipulation in React using a custom hook, and make state management more robust within functional components.

See it in action here!

Features

  • Single Line State Initialization Instead of calling useState on multiple lines, developers can pass a single object with key value pairs of the initial state just like in class components.

  • Getter and Setter Functions Developers who prefer using methods that mimic encapsulation syntax similar to those used in languages like Java can now use getters and setters to retrieve and set state, so naming conventions are consistent across the codebase.

  • Setting multiple state variables in a single call Instead of calling state setting functions one by one in event handlers, developers can pass any form of state that they want in a single object like in class components to mutate the state.

  • Built-in history navigation and state reset functions Perhaps the strongest feature of this library, these functions allow simple resetting of state without needing to remember the values of the initial state. In addition, functions that allow for navigation of history in the state without the use of an exernal router are available. This is a feature that can be beneficial for those who develop standalone Electron apps or large single page applications.

How to use

Here is how to initialize state using useState+:


    import useStatePlus from 'use-state-plus';

    interface MyInterface {
      ticker: number;
    }

    const { ticker } = useStatePlus<MyInterface>({ ticker: 0 });

    //You call getter and setter methods to manipulate state like this:

    const tickerVal = ticker.get();

    return (
        <div>
            <button onClick={() => ticker.set(tickerVal + 1)}>
                Clicked {tickerVal} Times.
                </button>
            <button onClick={() => ticker.reset()}>
                Click to Reset.
                </button>
        </div>
    )

You can also use methods included in useState+ to control multiple variables in state like this:


    import { useStatePlus } from 'use-state-plus';

    interface MyInterface {
      ticker: number;
      switcher: string;
    }

    const { ticker, switcher, getAll, multiSet, resetAll } = useStatePlus<MyInterface>({ ticker: 0, switcher: 'off' });

    //You call the getAll method to condense getters into one call:

    const state = getAll();

    return (
        <div>
            <button
                onClick={() => {
                    multiSet({
                        ticker: state.ticker + 1,
                        switcher: state.switcher === 'off' ? 'on' : 'off'
                    })
                }}>
                Clicked {state.ticker} Times. Switch is {state.switcher}.
            </button>
            <button onClick={() => resetAll()}>
                Click to Reset State.
            </button>
        </div>
    )

Additional history navigation and state reset function usage:


    import { useStatePlus } from 'use-state-plus';

    interface MyInterface {
      ticker: number;
      switcher: string;
    }

    const { ticker, switcher, multiReset, goBack, goForward } = useStatePlus<MyInterface>({ ticker: 0, switcher: 'off' });

    return (
        <div>
            <button onClick={() => { multiReset(['switcher', 'ticker'])}}>
                Reset two state variables.
            </button>
            <button onClick={() => { goBack() }}>
                Rewind one step.
            </button>
            <button onClick={() => { goForward(2) }}>
                Fast forward two steps.
            </button>
        </div>
    )

A lite version of this library can be imported as useStatePlusLite for those who run into performance issues when using the history management feature.