Skip to content

Instantly share code, notes, and snippets.

@jfacoustic
Created January 9, 2026 18:12
Show Gist options
  • Select an option

  • Save jfacoustic/24f238af3e4d5551e93787eafc70601c to your computer and use it in GitHub Desktop.

Select an option

Save jfacoustic/24f238af3e4d5551e93787eafc70601c to your computer and use it in GitHub Desktop.
Custom Jetbrains snippet for React Context Provider pattern.
import { createContext, useContext, useState, ReactNode } from 'react';
interface $NAME$State {
$END$
}
interface $NAME$ContextProps {
$NAME$: $NAME$State;
set$NAME$: (newState: $NAME$State) => void;
}
const $NAME$Context = createContext<$NAME$ContextProps| undefined>(undefined);
export function $NAME$Provider({ children }: { children: ReactNode }) {
const [ $NAME$, set$NAME$ ] = useState<$NAME$State>({});
return (
<$NAME$Context.Provider value={{
$NAME$,
set$NAME$,
}}>
{children}
</$NAME$Context.Provider>
);
}
export function use$NAME$() : $NAME$ContextProps {
const context = useContext<$NAME$ContextProps | undefined>($NAME$Context);
if (!context) {
throw new Error('use$NAME$ must be used within a $NAME$Provider');
}
return context;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment