Created
January 9, 2026 18:12
-
-
Save jfacoustic/24f238af3e4d5551e93787eafc70601c to your computer and use it in GitHub Desktop.
Custom Jetbrains snippet for React Context Provider pattern.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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