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

useful-state-hooks

v1.3.3

Published

Useful React hooks written in Typescript

Downloads

38

Readme

Useful state hooks

Maintained Size Coverage License Issues

Useful React hooks written in Typescript. They handle the same type of states as the normal useState hook but with a few every day coding improvements. Each hooks is written to be as similar to the original useState hook as possible with the purpose of being ease to use.

Installation

npm install useful-state-hooks

Hooks

UseLocalStorageState

Syncs the state with localstorage. It has almost the same signature as the normal useState hook accepting primitives, dates, arrays, objects and functions as initial state.

const [count, setCount] = useLocalStorageState("count", 1);

It takes two arguments. First argument is the localstorage key, while the second argument is the initial state. The following rules apply:

  • Data stored in localstorage will be prioritized over initial state.
  • Date objects are revived (recreated from strings) when loaded from localstorage.
  • Passing in a new key between renders will result in the state being saved to the new place in local storage.

UseListState

Was created to ease the headaches caused by mutations when working with arrays. Instead of returning a set function as the second tuple value, it returns an object with multiple set functions which can be used to update the state without mutating it.

Use hook like this:

const [words, setWords] = useListState(['Lorem', 'Ipsum']);

Apart from the normal set function, there are four more functions that can be used to set the state.

  • add, update, remove and sort.

  • They will change the state without introducing duplicates.

When working with objects a second argument must be passed in to the hook, defining a key. The hook can then use that key to check equality of objects.

const [users, setUsers] = useListState([
  { id: 1, name: "My" }
  { id: 2, name: "Olivia" }
], 'id');

Add

Appends to the end of the list without introducing duplicates. Supports the spread operator as well.

setWords.add(['Foo']);
// ['Lorem', 'Ipsum', 'Foo']

setWords.add(...['Foo', 'Bas', 'Ipsum']);
// ['Lorem', 'Ipsum', 'Foo', 'Bas']

Remove

Removes all matching item from the list.

setWords.remove(['Lorem']);
// ['Ipsum']

Set

This is just the normal React set function re-exposed (which can introduce duplicates).

setWords.set(['New list']);
// ['New List']

Sort

Sorts an array ascending or descending.

setWords.sort('asc');
// ['Ipsum', 'Lorem']

It's also possible to sort an array of objects. When doing so a second argument must be passed in defining the key to sort by.

setUsers.sort('desc', 'name');

Update (objects only)

Uses the key that was passed in when calling the hook to update the correct object in the array.

const [users, setUsers] = useListState([
  { id: 1, name: 'My' }
], 'id');

setUsers.update({ id: 1, name: 'Olivia' });
// [{ id: 1, name: 'Olivia' }]

UseDebounceState

This hook is useful when you want to update a state often but not trigger related actions at same rate. Multiple state updates in a row within the delay window will only result in the callback being fired once. The callback will be invoked, with the latest state value, when no state updates has happened for the given delay.

const [query, setQuery] = useDebounceState((query) => {
  // Called when state updates has paused for 2 seconds.
}, 'useful react hooks', 2000);

Use case:
A user types into a field which updates the state as normal but other actions like calling an API or calculating expensive things might be better to do when the user stops typing for a short period.

It takes three arguments

  1. Debounce callback
  2. Initial state
  3. Delay (defaults to 1000 milliseconds).

Both the initial state and the delay are optional:

const [query, setQuery] = useDebounceState((query) => {
  // Call API or do expensive operations...
});

It's even possible to destruct a third value from the tuple - a debounce object.

The returned debounce object contains two functions, flush and cancel. Calling flush will call onDebounce immediately and calling cancel will prevent it from running - useful when your components are unmounting.

const [query, setQuery, debounce] = useDebounceState(
  (query) => {
    // Call API or do expensive operations...
  }
);

// ----- do -----

useEffect(() => {
  // Make sure callback runs before component unmounts.
  return debounce.flush
}, [])

// ----- or -----

useEffect(() => {
  // Prevent callback from running after component has unmounted.
  return debounce.cancel
}, [])

Support

If you find and bugs please report an issue so it can be fixed. Do you have any suggestions that would improve the hooks please let me now. Feel free to contribute in any way!

I hope this can be useful for you in some way, enjoy 😇