{"id":13835308,"url":"https://github.com/StringEpsilon/mini-create-react-context","last_synced_at":"2025-07-10T07:31:35.146Z","repository":{"id":45995430,"uuid":"179731414","full_name":"StringEpsilon/mini-create-react-context","owner":"StringEpsilon","description":"(A smaller) polyfill for the react context API","archived":true,"fork":true,"pushed_at":"2021-11-22T04:02:21.000Z","size":2674,"stargazers_count":34,"open_issues_count":20,"forks_count":11,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-09T05:47:31.777Z","etag":null,"topics":["context","contexttypes","polyfill","ponyfill","react","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"jamiebuilds/create-react-context","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/StringEpsilon.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-04-05T18:04:29.000Z","updated_at":"2023-01-28T05:12:17.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/StringEpsilon/mini-create-react-context","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"purl":"pkg:github/StringEpsilon/mini-create-react-context","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StringEpsilon%2Fmini-create-react-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StringEpsilon%2Fmini-create-react-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StringEpsilon%2Fmini-create-react-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StringEpsilon%2Fmini-create-react-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/StringEpsilon","download_url":"https://codeload.github.com/StringEpsilon/mini-create-react-context/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StringEpsilon%2Fmini-create-react-context/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264471425,"owners_count":23613674,"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","contexttypes","polyfill","ponyfill","react","typescript"],"created_at":"2024-08-04T14:00:59.227Z","updated_at":"2025-07-10T07:31:32.409Z","avatar_url":"https://github.com/StringEpsilon.png","language":"TypeScript","readme":"# mini-create-react-context\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://packagephobia.now.sh/result?p=mini-create-react-context\"\u003e\n\t\u003cimg alt=\"npm install size\" src=\"https://packagephobia.now.sh/badge?p=mini-create-react-context\"\u003e\n\u003c/a\u003e\n\u003ca href=\"https://bundlephobia.com/result?p=mini-create-react-context@latest\"\u003e\n\t\u003cimg alt=\"npm bundle size\" src=\"https://img.shields.io/bundlephobia/min/mini-create-react-context/latest.svg?style=flat-square\"\u003e\n\u003c/a\u003e\n\u003ca href=\"https://www.npmjs.com/package/mini-create-react-context\"\u003e\n    \u003cimg alt=\"npm\" src=\"https://img.shields.io/npm/v/mini-create-react-context.svg?style=flat-square\"\u003e\n\u003c/a\u003e\n\u003c/p\u003e\n\n\u003e (A smaller) Polyfill for the [React context API](https://github.com/reactjs/rfcs/pull/2)\n\n## Install\n\n```sh\nnpm install mini-create-react-context\n```\n\nYou'll need to also have `react` and `prop-types` installed.\n\n## API\n\n```js\nconst Context = createReactContext(defaultValue);\n/*\n\t\u003cContext.Provider value={providedValue}\u003e\n\t\t{children}\n\t\u003c/Context.Provider\u003e\n\n\t...\n\n\t\u003cContext.Consumer\u003e\n\t\t{value =\u003e children}\n\t\u003c/Context.Consumer\u003e\n*/\n```\n\n## Example\n\n```js\n// @flow\nimport React, { type Node } from 'react';\nimport createReactContext, { type Context } from 'mini-create-react-context';\n\ntype Theme = 'light' | 'dark';\n// Pass a default theme to ensure type correctness\nconst ThemeContext: Context\u003cTheme\u003e = createReactContext('light');\n\nclass ThemeToggler extends React.Component\u003c\n  { children: Node },\n  { theme: Theme }\n\u003e {\n  state = { theme: 'light' };\n  render() {\n    return (\n      // Pass the current context value to the Provider's `value` prop.\n      // Changes are detected using strict comparison (Object.is)\n      \u003cThemeContext.Provider value={this.state.theme}\u003e\n        \u003cbutton\n          onClick={() =\u003e {\n            this.setState(state =\u003e ({\n              theme: state.theme === 'light' ? 'dark' : 'light'\n            }));\n          }}\n        \u003e\n          Toggle theme\n        \u003c/button\u003e\n        {this.props.children}\n      \u003c/ThemeContext.Provider\u003e\n    );\n  }\n}\n\nclass Title extends React.Component\u003c{ children: Node }\u003e {\n  render() {\n    return (\n      // The Consumer uses a render prop API. Avoids conflicts in the\n      // props namespace.\n      \u003cThemeContext.Consumer\u003e\n        {theme =\u003e (\n          \u003ch1 style={{ color: theme === 'light' ? '#000' : '#fff' }}\u003e\n            {this.props.children}\n          \u003c/h1\u003e\n        )}\n      \u003c/ThemeContext.Consumer\u003e\n    );\n  }\n}\n```\n\n## Compatibility\n\nThis package only \"ponyfills\" the `React.createContext` API, not other unrelated React 16+ APIs. If you are using a version of React \u003c16, keep in mind that you can only use features available in that version.\n\nFor example, you cannot pass children types aren't valid pre React 16:\n\n```js\n\u003cContext.Provider\u003e\n  \u003cdiv/\u003e\n  \u003cdiv/\u003e\n\u003c/Context.Provider\u003e\n```\n\nIt will throw `A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.` because `\u003cContext.Provider\u003e` can only receive a single child element. To fix the error just wrap everyting in a single `\u003cdiv\u003e`:\n\n```js\n\u003cContext.Provider\u003e\n  \u003cdiv\u003e\n    \u003cdiv/\u003e\n    \u003cdiv/\u003e\n  \u003c/div\u003e\n\u003c/Context.Provider\u003e\n```\n\n## Size difference to the original:\n|            | original | **mini**\n|------------|----------|-----\n|install size| [**50 kB**](https://packagephobia.now.sh/result?p=create-react-context) | [140 kB](https://packagephobia.now.sh/result?p=mini-create-react-context)\n|minified    | [3.3 kB](https://bundlephobia.com/result?p=create-react-context) | [**2.3kB**](https://bundlephobia.com/result?p=mini-create-react-context)\n|minzip      | 1.3 kB   | **1.0kB**\n","funding_links":[],"categories":["目录"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FStringEpsilon%2Fmini-create-react-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FStringEpsilon%2Fmini-create-react-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FStringEpsilon%2Fmini-create-react-context/lists"}