MCPcopy
hub / github.com/chakra-ui/ark / createContext

Function createContext

packages/react/src/utils/create-context.ts:19–49  ·  view source on GitHub ↗
(options: CreateContextOptions<T> = {})

Source from the content-addressed store, hash-verified

17}
18
19export function createContext<T>(options: CreateContextOptions<T> = {}) {
20 const {
21 name,
22 strict = true,
23 hookName = 'useContext',
24 providerName = 'Provider',
25 errorMessage,
26 defaultValue,
27 } = options
28
29 const Context = createReactContext<T | undefined>(defaultValue)
30
31 Context.displayName = name
32
33 function useContext() {
34 const context = useReactContext(Context)
35
36 if (!context && strict) {
37 const error = new Error(errorMessage ?? getErrorMessage(hookName, providerName))
38 error.name = 'ContextError'
39 if (hasProp(Error, 'captureStackTrace') && isFunction(Error.captureStackTrace)) {
40 Error.captureStackTrace(error, useContext)
41 }
42 throw error
43 }
44
45 return context
46 }
47
48 return [Context.Provider, useContext, Context] as CreateContextReturn<T>
49}

Calls

no outgoing calls

Tested by

no test coverage detected