Files
Devour/devour_data/docs/use-context.json
T
Tomas Dvorak 898a3c303f update
2026-02-24 10:33:59 +01:00

17 lines
2.2 KiB
JSON

{
"id": "a50e5a1a2b581058e78e563a",
"source": "solid:signals",
"type": "github-document",
"title": "use-context",
"content": "---\ntitle: useContext\nuse_cases: \u003e-\n consuming context, accessing global state, avoiding prop drilling, theme\n access, auth state, shared data\ntags:\n - context\n - providers\n - global-state\n - hooks\n - consumption\nversion: '1.0'\ndescription: \u003e-\n Access context values with useContext to consume data from parent providers.\n Avoid prop drilling and access shared state throughout your component tree.\n---\n\nUsed to grab context within a context provider scope to allow for deep passing of props without having to pass them through each Component function.\nIt's therefore used in conjunction with [`createContext`](/reference/component-apis/create-context) to consume the data from a Provider scope and thus avoid passing data through intermediate components (prop drilling).\n\n```ts\nconst [state, { increment, decrement }] = useContext(CounterContext)\n```\n\n## Recommended usage\n\nIt is often a good idea to wrap `useContext` in a function like so:\n\n```ts title=\"/context/counter-component.tsx\"\nfunction useCounterContext() {\n\tconst context = useContext(CounterContext)\n\n\tif (!context) {\n\t\tthrow new Error(\"useCounterContext: cannot find a CounterContext\")\n\t}\n\n\treturn context\n}\n```\n\nSee the API reference of [createContext](/reference/component-apis/create-context) the API on how to generate a Provider scope.\nAnd check the [Context concepts](/concepts/context) for more information on how to architecture your contexts.\n\n## Type signature\n\n```ts\nimport { type Context } from \"solid-js\"\n\nfunction useContext\u003cT\u003e(context: Context\u003cT\u003e): T\n\n```",
"url": "https://github.com/solidjs/solid-docs/blob/HEAD/src/routes/reference/component-apis/use-context.mdx",
"metadata": {
"path": "src/routes/reference/component-apis/use-context.mdx",
"repo": "solidjs/solid-docs",
"repo_url": "https://github.com/solidjs/solid-docs.git",
"size": 1545,
"source_type": "github"
},
"hash": "6779409e69928136c91a586f9a93b283b7a2c56bd04c373ded8f6608ddfd472b",
"timestamp": "2026-02-23T11:43:00.188864108+01:00"
}