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

v6.0.0

Published

A react hook for setting state after a specific delay, built with typescript in mind.

Downloads

11

Readme

use-sluggish-state

npm package Build Status Downloads Issues Commitizen Friendly Semantic Release Package Size

useSluggishState

A react hook for setting state after a specific delay, built with typescript in mind. The basic idea behind this is that you can schedule a state update and if you want perform a transition before the state update completes.

Install

npm install use-sluggish-state

Usage

import useSluggishState from 'use-sluggish-state';

const [isDarkMode, setIsDarkMode] = useSluggishState(true, 500);

console.log(isDarkMode);
// => true

setIsDarkMode(false);

console.log(isDarkMode);
// => true
// Darkmode is still true

console.log(isDarkMode);
// After 500 milliseconds
// => false

OR with typescript

import useSluggishState from 'use-sluggish-state';

const [isVisible, setIsVisible] = useSluggishState<boolean | undefined>(
  true,
  5000
);

// ERROR:
// Typescript should throw an error at this
setIsVisible('yes');

// SUCCESS:
// This should pass
setIsVisible(false);

If no generic is passed, the type is also inferred from the initial value, just the same as in react useState

import useSluggishState from 'use-sluggish-state';

const [isVisible, setIsVisible] = useSluggishState(true, 5000);

// ERROR:
// Typescript should also throw an error at this
setIsVisible('yes');

// SUCCESS:
// This should pass
setIsVisible(false);

Also allows a callback to be passed as the parameter of setState.

import useSluggishState from 'use-sluggish-state';

const [isDarkMode, setIsDarkMode] = useSluggishState(true, 100);

// Toggle dark mode on/off after 100 milliseconds
setIsDarkMode(prev => !prev);

delay can be specified for a specific setState action than that which was already set in the hook.

Note: this does not change the original delay for subsequent setState calls

import useSluggishState from 'use-sluggish-state';

const [showPopup, setShowPopup] = useSluggishState(true, 1000);

// Shows popup after 5 seconds
setShowPopup(true, 5000);

// Shows popup after 1 second
setShowPopup(true);

Calling setState with a value does not change the state in the already executing code.

import useSluggishState from 'use-sluggish-state';

const [names, setNames] = useSluggishState('Bob');

setNames(names + ', Bonnie');
setNames(names + ', Clyde', 2000);

console.log(names);
// => "Bob, Bonnie"
// => "Bob, Clyde"

However when called with a callback, the previous state since the last setState call can be accessed

import useSluggishState from 'use-sluggish-state';

const [owner, setOwner] = useSluggishState('Bob');

setNames(prev => prev + ', Bonnie');
setNames(prev => prev + ', Clyde', 2000);

console.log(owner);
// => "Bob, Bonnie"
// => "Bob, Bonnie, Clyde"

Note: the state updates occur in the order of the delay setState is called with

import useSluggishState from 'use-sluggish-state';

const [owner, setOwner] = useSluggishState('Bob');

setNames(prev => prev + ', Bonnie', 3000);
setNames(prev => prev + ', Clyde', 2000);

console.log(owner);
// => "Bob, Clyde"
// => "Bob, Clyde, Bonnie"

DEBOUNCE

Achieve Debounce in the simplest way

import useSluggishState from 'use-sluggish-state';

const SomeComponent = () => {
  const [searchQuery, setSearchQuery, _loading, finalQuery] =
    useSluggishState('gribble');

  const onChange = event => {
    setSearchQuery(event.target.value, 1000);
  };

  useEffect(() => {
    // fetch will be called only when user has stopped typing for 1 second
    fetch(`path/to/api?search=${searchQuery}`);
  }, [searchQuery]);

  return <input value={finalQuery} onChange={onChange} />;
};

API

const [state, setState, loading, finalState] = useSluggishState(value?, delay?)

state

The state variable initialized by the value property passed in the hook

Type: Infered from the value passed in. If no value is passed, it defaults to undefined. Also defined by passing in a generic e.g <boolean> like in the typescript example above.

setState

Function to trigger a state update. Works like the typical react setState.

Type:

Dispatch<React.SetStateAction<InferredType>>;
// Where `InferredType` is the type of the passed value or undefined

// OR

Dispatch<React.SetStateAction<DefinedType>>;
// Where `DefinedType` is the generic passed to `useStoreState` i.e boolean, string.

loading

Indicates whether a state update is in progress or not.

Type: Boolean

finalState

The instant updated state not affected by the delay.

Type: Same as in state above

value?

Required: NO Default: undefined Type: any

delay? (milliseconds)

Required: NO Default: 0 Type: number

type Dispatch

Definition:

type Dispatch<A> = (value: A, _delay?: number) => void;

Usage:

import { type Dispatch } from 'use-sluggish-state';

const customSetState: Dispatch<React.SetStateAction<InferredType>>;