{"id":19170622,"url":"https://github.com/bennetthardwick/react-merged-context","last_synced_at":"2025-05-07T15:53:21.658Z","repository":{"id":57334521,"uuid":"313582817","full_name":"bennetthardwick/react-merged-context","owner":"bennetthardwick","description":"A simple library for creating a context provider that merges with its parent's values.","archived":false,"fork":false,"pushed_at":"2020-11-17T12:02:14.000Z","size":315,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-20T00:38:08.738Z","etag":null,"topics":["react","react-context","react-hooks","react-native","reactjs"],"latest_commit_sha":null,"homepage":"https://react-merged-context.netlify.app/","language":"TypeScript","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/bennetthardwick.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":"2020-11-17T10:24:23.000Z","updated_at":"2022-07-11T18:52:58.000Z","dependencies_parsed_at":"2022-09-05T08:01:52.059Z","dependency_job_id":null,"html_url":"https://github.com/bennetthardwick/react-merged-context","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bennetthardwick%2Freact-merged-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bennetthardwick%2Freact-merged-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bennetthardwick%2Freact-merged-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bennetthardwick%2Freact-merged-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bennetthardwick","download_url":"https://codeload.github.com/bennetthardwick/react-merged-context/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252912117,"owners_count":21824059,"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":["react","react-context","react-hooks","react-native","reactjs"],"created_at":"2024-11-09T09:54:40.442Z","updated_at":"2025-05-07T15:53:21.599Z","avatar_url":"https://github.com/bennetthardwick.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-merged-context\n\nA simple library for creating a context provider that merges together the context values throughtout the React tree.\n\nCheck it out in action at [react-merged-context.netlify.app](https://react-merged-context.netlify.app/).\n\n## Installation\n\nThis package lives in [npm](https://www.npmjs.com/get-npm). To install the latest stable version, run the following command:\n\n```bash\nnpm install react-merged-context\n```\n\nOr if you're using [yarn](https://classic.yarnpkg.com/en/docs/install/):\n\n```bash\nyarn add react-merged-context\n```\n\n## Usage\n\nYou can create a merged context provider by passing your context to the `createMergedProvider` method.\nThis will return a \"merged context provider\" which you can use \n\n```ts\nimport { createContext } from 'react';\nimport { createMergedProvider } from 'react-merged-context';\n\nconst MyContext = createContext({ name: 'Sarah' })\nconst MyContextProvider = createMergedProvider(MyContext);\n\nconst element = \u003cMyContextProvider value={...}\u003e\n...\n\u003c/MyContextProvider\u003e;\n```\n\n### Getting the value\n\nSince `createMergedProvider` just creates a \"Provider\" component, you can retrieve the value from the context using the normal methods - either through `Context.Consumer` or `useContext`.\n\n### Objects\n\nMerged context providers can be used to apply a diff to the context.\n\n```ts\nimport { createContext } from 'react';\nimport { createMergedProvider } from 'react-merged-context';\n\nconst MyContext = createContext({ name: 'Bennett', age: 22 })\nconst MyContextProvider = createMergedProvider(MyContext);\n\nconst element = \u003cMyContextProvider value={{ name: 'Sarah' }}\u003e\n  \u003cMyContext.Consumer\u003e\n    ({ name, age }) =\u003e {\n      // `name` is 'Sarah' but `age` is still 22\n    }\n  \u003c/MyContext.Consumer\u003e\n\u003c/MyContextProvider\u003e;\n```\n\n### Arrays\n\nMerged context providers can also be used to concat arrays.\n\n```ts\nimport { createContext } from 'react';\nimport { createMergedProvider } from 'react-merged-context';\n\nconst MyContext = createContext([ 1, 2, 3 ])\nconst MyContextProvider = createMergedProvider(MyContext);\n\nconst element = \u003cMyContextProvider value={[ 4, 5, 6 ]}\u003e\n  \u003cMyContext.Consumer\u003e\n    (values) =\u003e {\n      // values is [ 1, 2, 3, 4, 5, 6 ]\n    }\n  \u003c/MyContext.Consumer\u003e\n\u003c/MyContextProvider\u003e;\n```\n\n### Resetting with React context providers\n\nYou can use normal context providers to reset the value of the context.\n\n```ts\nimport { createContext } from 'react';\nimport { createMergedProvider } from 'react-merged-context';\n\nconst MyContext = createContext([ 1, 2, 3 ])\nconst MyContextProvider = createMergedProvider(MyContext);\n\nconst element = \u003cMyContext.Provider value={[]}\u003e\n  \u003cMyContextProvider value={[ 4, 5, 6 ]}\u003e\n    \u003cMyContext.Consumer\u003e\n      (values) =\u003e {\n        // values is [ 4, 5, 6 ]\n      }\n    \u003c/MyContext.Consumer\u003e\n  \u003c/MyContextProvider\u003e;\n\u003c/MyContext.Provider\u003e\n```\n\n## Example\n\nYou can view the example live at [react-merged-context.netlify.app](https://react-merged-context.netlify.app/).\n\nTo run the example locally:\n1. navigate to `example/`\n2. install the dependencies by running `yarn`\n3. run `yarn start` to start the dev server\n4. navigate to `localhost:1234`\n\n## License\n\n[MIT](https://github.com/bennetthardwick/react-merged-context/blob/master/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbennetthardwick%2Freact-merged-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbennetthardwick%2Freact-merged-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbennetthardwick%2Freact-merged-context/lists"}