Generate a React context with its provider and consumer hook, memoised, with a null default so a missing provider fails loudly.
Name the thing, not the wrapper. A Context or Provider suffix is removed, so Theme and ThemeContext give the same output.
One field per line, same syntax as a props list. A setter is generated for each. Supported types: string, number, boolean, string[], ReactNode, () => void, object.
theme-context.tsxTheme context, provider and consumer hook.
"use client";
import { createContext, useContext, useMemo, useState } from "react";
import type { ReactNode } from "react";
type ThemeState = {
mode: string;
compact: boolean;
};
type ThemeActions = {
setMode: (value: string) => void;
setCompact: (value: boolean) => void;
};
/* The default is the value a reader gets with no provider above it. It is
null here on purpose: a plausible-looking default turns "you forgot the
provider" into a silent wrong value, which is far harder to find than
an error. */
const ThemeContext = createContext<(ThemeState & ThemeActions) | null>(null);
export function ThemeProvider({ children }: { children: ReactNode }) {
const [state, setState] = useState<ThemeState>({
mode: "light",
compact: false,
});
/* Memoised, or every render of this provider hands its readers a new
object and re-renders all of them whether or not anything changed. */
const value = useMemo(
() => ({
...state,
setMode: (value: string) =>
setState((current) => ({ ...current, mode: value })),
setCompact: (value: boolean) =>
setState((current) => ({ ...current, compact: value })),
}),
[state],
);
/* React 19 renders a context directly: <ThemeContext value={…}> rather
than <ThemeContext.Provider value={…}>. The longer form still works. */
return <ThemeContext value={value}>{children}</ThemeContext>;
}
/* A hook rather than exporting the context. Readers cannot then subscribe
to it without the null check, and the context itself stays an
implementation detail that can be split later without changing callers. */
export function useTheme(): ThemeState & ThemeActions {
const value = useContext(ThemeContext);
/* Throwing here is the point of the null default: a component
rendered outside the provider fails immediately and says so,
rather than reading a default that looks plausible. */
if (value === null) {
throw new Error("useTheme must be used inside <ThemeProvider>.");
}
return value;
}
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 Context Generator runs entirely on your device — the context, provider and hook are assembled in your browser from templates built into the page, and the field names you type are not sent anywhere.
You’re on the Free plan. Compare plans
The context, the provider and the consumer hook are one unit, so they are generated together. Generating any one of them alone would be the shallow version of this: a context without a memoised provider value re-renders every reader on every render, and a context exported directly gets read without its null check.
Three decisions are made for you, each for a reason you can read on the page. The provider's value is memoised, because otherwise every render hands the readers a new object and re-renders all of them whether or not anything changed. The default value is null and the consumer hook throws, because a plausible-looking default turns "you forgot the provider" into a silently wrong value, which is far harder to find than an error. And the context is not exported — only the hook is — so it stays an implementation detail you can split later without changing callers.
React 19 lets a context be rendered directly: `<ThemeContext value={...}>` rather than `<ThemeContext.Provider value={...}>`. The output uses the shorter form and the comment says the longer one still works, because it reads like a mistake until you know it is not.
Two options change the shape rather than the style. A reducer puts every change through one function, so an update touching two fields lands as one render instead of two with an impossible state in between. Splitting state from actions means a component that only dispatches never re-renders when the state changes — worth it when many components dispatch and few read, and the notes say which case you are in.
The file carries the client directive, because it holds state and so cannot be a Server Component. Every variant is parsed by the real TypeScript parser in this site's test suite; nothing is compiled or run here.
So that a component rendered outside the provider fails immediately and says so. A default that looks plausible means the bug shows up later, somewhere else, as a wrong value rather than an error.
Because an object literal is a new object every render, and a new context value re-renders every reader. The memo is what makes the context cost nothing when nothing changed.
React 19 renders the context itself, so the shorter form is used. The longer one is not broken and still works — it is just a wrapper you no longer need.
Only if many components dispatch and few read. The split means a dispatch-only component never re-renders on a state change, at the cost of two contexts and two hooks to keep straight.
No, and it says so with the client directive at the top. It holds state. Render the provider inside a layout: the layout stays a Server Component and only the provider and its readers become client components.
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