{"id":13493788,"url":"https://github.com/jamiebuilds/create-react-context","last_synced_at":"2025-04-13T00:45:02.374Z","repository":{"id":41451525,"uuid":"113528385","full_name":"jamiebuilds/create-react-context","owner":"jamiebuilds","description":"Polyfill for the proposed React context API","archived":false,"fork":false,"pushed_at":"2021-06-07T04:07:59.000Z","size":337,"stargazers_count":690,"open_issues_count":9,"forks_count":75,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-03-12T21:02:40.424Z","etag":null,"topics":["context","contexttypes","polyfill","react"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":false,"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/jamiebuilds.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":"2017-12-08T03:45:12.000Z","updated_at":"2025-03-07T02:37:28.000Z","dependencies_parsed_at":"2022-09-10T23:41:45.230Z","dependency_job_id":null,"html_url":"https://github.com/jamiebuilds/create-react-context","commit_stats":null,"previous_names":["thejameskyle/create-react-context"],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamiebuilds%2Fcreate-react-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamiebuilds%2Fcreate-react-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamiebuilds%2Fcreate-react-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamiebuilds%2Fcreate-react-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jamiebuilds","download_url":"https://codeload.github.com/jamiebuilds/create-react-context/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245200684,"owners_count":20576674,"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","react"],"created_at":"2024-07-31T19:01:18.807Z","updated_at":"2025-03-28T02:17:57.151Z","avatar_url":"https://github.com/jamiebuilds.png","language":"JavaScript","readme":"# create-react-context\n\n\u003e Polyfill for the [proposed React context API](https://github.com/reactjs/rfcs/pull/2)\n\n## Install\n\n```sh\nyarn add 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// \u003cContext.Provider value={providedValue}\u003e{children}\u003c/Context.Provider\u003e\n// ...\n// \u003cContext.Consumer\u003e{value =\u003e children}\u003c/Context.Consumer\u003e\n```\n\n## Example\n\n```js\n// @flow\nimport React, { type Node } from 'react';\nimport createReactContext, { type Context } from '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\nunrelated React 16+ APIs. If you are using a version of React \u003c16, keep\nin 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","funding_links":[],"categories":["Libraries","JavaScript","List"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamiebuilds%2Fcreate-react-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjamiebuilds%2Fcreate-react-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamiebuilds%2Fcreate-react-context/lists"}