Generate a custom React hook — debounce, fetch, local storage, media query — with the cleanup, abort handling and hydration care included.
Given a use prefix if it has none. Without it the React lint rules do not treat the function as a hook and stop checking the rules of hooks inside it.
Functional setters, so two updates in one event both land.
A named export survives a rename and autocompletes. A default export is what a Next.js page or a lazy import needs.
use-toggle.tsBoolean toggle
import { useCallback, useState } from "react";
/* The setters take the previous value rather than reading the current
one from the closure. Two calls in the same event would otherwise
both see the stale value and the second would undo the first. */
export function useToggle(initial = false) {
const [on, setOn] = useState(initial);
const toggle = useCallback(() => {
setOn((current) => !current);
}, []);
const turnOn = useCallback(() => setOn(true), []);
const turnOff = useCallback(() => setOn(false), []);
return { on, toggle, turnOn, turnOff, setOn };
}
Written against React 19. Nothing here is compiled, type-checked or run: the output is assembled from templates, and the only machine check it gets is that every variant of it is put through the real TypeScript parser in this site’s test suite.
React Hook Generator runs entirely on your device — the hook is assembled in your browser from templates built into the page, and the name you type is not sent anywhere.
You’re on the Free plan. Compare plans
The six hooks here are the ones whose correct version is not the obvious one. Each is generated with the part people leave out, and a comment saying what that part is for.
A debounce without a cleanup is not a debounce. Without clearing the pending timer every value still arrives, just late — which looks like it works until you watch the network tab. A fetch hook without an AbortController leaks the request when the component unmounts, and one that does abort has to then ignore its own cancellation rather than reporting it as an error, or you get a failure flash on every navigation.
A localStorage hook that reads during render causes a hydration mismatch, because the server has no localStorage and renders something different. Reading in an effect fixes it, and the generated hook exposes whether it has loaded yet so a component can wait rather than flash the fallback. Every access is wrapped, because localStorage throws in private browsing and wherever site data is blocked.
A media-query hook copied from an older example uses `addListener`, which is deprecated. And a fetch hook with three separate pieces of state can represent loading and an error at the same time, which cannot happen and which every render then has to defend against — so the generated one holds a single value.
Where the generated hook is the wrong answer, the notes say so. For real data fetching a library that handles caching, retries and deduplication is almost always better, and this is for the case where you want none of that.
Because the React lint rules identify a hook by its name. Without the prefix they stop checking the rules of hooks inside the function, so a conditional hook call goes unreported.
It is the whole mechanism. Clearing the pending timer is what makes each new value cancel the last one; without it every value in a burst still arrives, only delayed.
Because an abort means the effect was replaced, not that anything failed. Reporting it as an error flashes a failure on every navigation and every dependency change.
Because the server cannot, so the server and the client would render different things — a hydration mismatch. It reads in an effect and tells you when it has, so you can wait instead of flashing the fallback.
Probably not. A library that handles caching, deduplication and retries will serve you better. This is for when you deliberately want none of that, and the notes on the page say the same.
If a tool here saved you some time, you can put something towards the hosting.Buy me a coffee, on Ko-fi, opens in a new tab