{"id":26775509,"url":"https://github.com/todesstoss/create-use-context","last_synced_at":"2026-04-11T17:24:53.050Z","repository":{"id":34898397,"uuid":"188054381","full_name":"todesstoss/create-use-context","owner":"todesstoss","description":"A helper method which wraps original React useContext method in a type-safe manner providing NonNullable context value. Will throw if used outside of Provider","archived":false,"fork":false,"pushed_at":"2024-09-26T13:55:23.000Z","size":150,"stargazers_count":7,"open_issues_count":1,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-16T00:57:44.568Z","etag":null,"topics":["context","hooks","react","react-context","react-hooks"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/todesstoss.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-22T14:23:05.000Z","updated_at":"2024-08-30T11:00:09.000Z","dependencies_parsed_at":"2023-01-15T10:04:46.312Z","dependency_job_id":null,"html_url":"https://github.com/todesstoss/create-use-context","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/todesstoss%2Fcreate-use-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/todesstoss%2Fcreate-use-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/todesstoss%2Fcreate-use-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/todesstoss%2Fcreate-use-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/todesstoss","download_url":"https://codeload.github.com/todesstoss/create-use-context/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249178216,"owners_count":21225349,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["context","hooks","react","react-context","react-hooks"],"created_at":"2025-03-29T03:18:37.838Z","updated_at":"2026-04-11T17:24:53.043Z","avatar_url":"https://github.com/todesstoss.png","language":"JavaScript","readme":"# create-use-context\n\nA small helper around React `useContext` for context objects that use a **sentinel default** (`EMPTY_CONTEXT_VALUE`, defined in [`src/createUseContext.ts`](https://github.com/todesstoss/create-use-context/blob/master/src/createUseContext.ts)). The hook throws if the tree is not under a matching `Provider`. TypeScript types the hook’s return as your real value type only by using [`Exclude`](https://www.typescriptlang.org/docs/handbook/utility-types.html#excludeuniontype-excludedmembers) to drop the sentinel from the union—unlike `NonNullable`, which only removes `null` and `undefined`.\n\n## Installation\n\nThis package is **ESM-only** (`import` / bundlers). It does not ship CommonJS; use `import` or a bundler that resolves ESM.\n\nUsing pnpm:\n\n```sh\npnpm add create-use-context\n```\n\nUsing npm:\n\n```sh\nnpm install create-use-context\n```\n\nUsing Yarn:\n\n```sh\nyarn add create-use-context\n```\n\n## Usage\n\nDeclare context as `YourValue | typeof EMPTY_CONTEXT_VALUE`, pass `EMPTY_CONTEXT_VALUE` to `createContext` as the default (no provider), then wrap with `createUseContext`. The returned hook is typed like `() =\u003e YourValue`: you can still use `null` or `undefined` inside `YourValue` if you need them; the sentinel is only for “no provider mounted.”\n\n```tsx\nimport {\n  createContext,\n  useState,\n  type Dispatch,\n  type ReactNode,\n  type SetStateAction,\n} from 'react';\nimport { createUseContext, EMPTY_CONTEXT_VALUE } from 'create-use-context';\n\nconst INITIAL_COUNT = 0;\n\nexport interface CounterContextValue {\n  counter: number;\n  setCounter: Dispatch\u003cSetStateAction\u003cnumber\u003e\u003e;\n}\n\nexport const CounterContext = createContext\u003c\n  CounterContextValue | typeof EMPTY_CONTEXT_VALUE\n\u003e(EMPTY_CONTEXT_VALUE);\n\nCounterContext.displayName = 'CounterContext';\n\nexport function CounterContextProvider({ children }: { children: ReactNode }) {\n  const [counter, setCounter] = useState(INITIAL_COUNT);\n\n  return (\n    \u003cCounterContext.Provider value={{ counter, setCounter }}\u003e\n      {children}\n    \u003c/CounterContext.Provider\u003e\n  );\n}\n\nexport const useCounterContext = createUseContext(CounterContext);\n\nexport function CounterApp() {\n  const { counter, setCounter } = useCounterContext();\n\n  return (\n    \u003cbutton type=\"button\" onClick={() =\u003e setCounter(counter + 1)}\u003e\n      {counter} — add one\n    \u003c/button\u003e\n  );\n}\n\nexport function App() {\n  return (\n    \u003cCounterContextProvider\u003e\n      \u003cCounterApp /\u003e\n    \u003c/CounterContextProvider\u003e\n  );\n}\n```\n\n## Development\n\nRequires **Node.js ≥ 20.19** (for the tsdown toolchain).\n\n```sh\npnpm install\npnpm typecheck\npnpm test\npnpm build\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftodesstoss%2Fcreate-use-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftodesstoss%2Fcreate-use-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftodesstoss%2Fcreate-use-context/lists"}