Last active
January 23, 2026 20:18
-
-
Save alexanderdombroski/aa91cc7982645261959b455f16f62126 to your computer and use it in GitHub Desktop.
Scaffold context providers quickly. Name the file `use<Name>` | Created using SnippetStudio
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
Show hidden characters
| { | |
| "context-provider": { | |
| "prefix": "context-provider", | |
| "body": [ | |
| "", | |
| "import React, { createContext, useContext, type PropsWithChildren } from \"react\";", | |
| "", | |
| "type ${1:${TM_FILENAME_BASE/(?:use)?(.*)/${1:/capitalize}/}}Type = {", | |
| " $4", | |
| "}", | |
| "", | |
| "const $1 = createContext<$1Type>($5);", | |
| "", | |
| "export function $1Provder({ children }: PropsWithChildren) {", | |
| " $6", | |
| "", | |
| " return (", | |
| " <$1.Provider value={$7}>", | |
| " {children}", | |
| " </$1.Provider>", | |
| " );", | |
| "}", | |
| "", | |
| "export function ${2:$TM_FILENAME_BASE}(): $1Type {", | |
| " const context = useContext($1);", | |
| " if (!context) {", | |
| " throw new Error(\"$2 must be used within an $1Provider\");", | |
| " }", | |
| " return context;", | |
| "};", | |
| "" | |
| ], | |
| "description": "The Context Provider pattern in React allows you to share state or functions across a component tree without passing props manually at every level. It wraps components in a provider that supplies a context value, which child components can consume", | |
| "scope": "typescriptreact" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment