BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/shadcn-hooks/create-context

create-context

shadcn-hooks/create-context
FreeUtility

Create a context object.

Install it

npx shadcn@latest add https://shadcn-hooks.com/r/create-context.json

Source

registry/lib/create-context.tsxshadcn-hooks.com/r/create-context.json
1import * as React from 'react'
2
3function createContext<ContextValueType extends object | null>(
4 rootComponentName: string,
5 defaultContext?: ContextValueType,
6) {
7 'use no memo'
8 const Context = React.createContext<ContextValueType | undefined>(
9 defaultContext,
10 )
11
12 const Provider: React.FC<ContextValueType & { children: React.ReactNode }> = (
13 props,
14 ) => {
15 const { children, ...context } = props
16 // Only re-memoize when prop values change
17
18 const value = React.useMemo(
19 () => context,
20 // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps, react-hooks/use-memo
21 Object.values(context),
22 ) as ContextValueType
23 return <Context value={value}>{children}</Context>
24 }
25
26 Provider.displayName = `${rootComponentName}Provider`
27
28 function useContext(consumerName: string) {
29 const context = React.use(Context)
30 if (context) return context
31 if (defaultContext !== undefined) return defaultContext
32 // if a defaultContext wasn't specified, it's a required context.
33 throw new Error(
34 `\`${consumerName}\` must be used within \`${rootComponentName}\``,
35 )
36 }
37
38 return [Provider, useContext] as const
39}
40
41/* -------------------------------------------------------------------------------------------------
42 * createContextScope
43 * -----------------------------------------------------------------------------------------------*/
44
45type Scope<C = any> = { [scopeName: string]: React.Context<C>[] } | undefined
46type ScopeHook = (scope: Scope) => { [__scopeProp: string]: Scope }
47interface CreateScope {
48 scopeName: string;
49 (): ScopeHook
50}
51
52function createContextScope(
53 scopeName: string,
54 createContextScopeDeps: CreateScope[] = [],
55) {
56 let defaultContexts: any[] = []
57
58 /* -----------------------------------------------------------------------------------------------
59 * createContext
60 * ---------------------------------------------------------------------------------------------*/
61
62 function createContext<ContextValueType extends object | null>(
63 rootComponentName: string,
64 defaultContext?: ContextValueType,
65 ) {
66 'use no memo'
67 const BaseContext = React.createContext<ContextValueType | undefined>(
68 defaultContext,
69 )
70 const index = defaultContexts.length
71 defaultContexts = [...defaultContexts, defaultContext]
72
73 const Provider: React.FC<
74 ContextValueType & {
75 scope: Scope<ContextValueType>
76 children: React.ReactNode
77// … truncated

Files it writes

  • registry/lib/create-context.tsx

Facts

Registry
shadcn-hooks
Type
registry:lib
Access
Free
Files written
1
Licence
the repository states none
First indexed
2026-08-03
Last confirmed
today

Go to the source

shadcn-hooks.com Debbl/shadcn-hooks on GitHub Raw registry item This item as JSON

More from shadcn-hooks

All 58 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
create-effect-with-targetcreate-effect-with-targetshadcn-hooksUtilitiesFree1 dep
is-browseris-browsershadcn-hooksUtilitiesFreeno deps
BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex