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.
npm install use-sluggish-state
import { useDelayedState } from 'use-sluggish-state';
const [isDarkMode, setIsDarkMode] = useDelayedState(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 { useDelayedState } from 'use-sluggish-state';
const [isVisible, setIsVisible] = useDelayedState<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 { useDelayedState } from 'use-sluggish-state';
const [isVisible, setIsVisible] = useDelayedState(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 { useDelayedState } from 'use-sluggish-state';
const [isDarkMode, setIsDarkMode] = useDelayedState(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 { useDelayedState } from 'use-sluggish-state';
const [showPopup, setShowPopup] = useDelayedState(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 { useDelayedState } from 'use-sluggish-state';
const [names, setNames] = useDelayedState("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 { useDelayedState } from 'use-sluggish-state';
const [owner, setOwner] = useDelayedState("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 { useDelayedState } from 'use-sluggish-state';
const [owner, setOwner] = useDelayedState("Bob");
setNames((prev) => prev + ", Bonnie", 3000);
setNames((prev) => prev + ", Clyde", 2000);
console.log(owner);
// => "Bob, Clyde"
// => "Bob, Clyde, Bonnie"
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.
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.
Required: NO
Default: undefined
Type: any
Required: NO
Default: 0
Type: number
Definition:
type Dispatch<A> = (value: A, _delay?: number) => void;