Skip to main content

useGetState

function useGetState<T>(initial: T | (() => T)): readonly [T, Dispatch<SetStateAction<T>>, () => T];

useState with a third item, getState, which returns the latest value. It sees each update as soon as setState is called, before the re-render, so a callback created on an earlier render, such as a listener added once on mount, never reads a stale value. Functional updates apply to that latest value. initial can be a function, called once. setState and getState never change.

Example​

import { useEffect } from "react";
import { useGetState } from "react-honey-hooks";

function Clicks() {
const [count, setCount, getCount] = useGetState(0);

useEffect(() => {
const id = setInterval(() => {
console.log("Clicks so far:", getCount());
}, 5000);
return () => {
clearInterval(id);
};
}, [getCount]);

return (
<button
onClick={() => {
setCount((current) => current + 1);
}}
>
Clicked {count} times
</button>
);
}