{"id":13780979,"url":"https://github.com/dai-shi/react-context-global-state","last_synced_at":"2025-04-15T01:09:27.030Z","repository":{"id":33093210,"uuid":"151574655","full_name":"dai-shi/react-context-global-state","owner":"dai-shi","description":"[NOT MAINTAINED] Simple global state for React with Context API","archived":false,"fork":false,"pushed_at":"2023-01-04T21:38:23.000Z","size":834,"stargazers_count":23,"open_issues_count":17,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T01:09:21.335Z","etag":null,"topics":["context-api-react","react","state-management","stateless-components","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/react-context-global-state","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/dai-shi.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2018-10-04T13:18:49.000Z","updated_at":"2023-03-05T01:11:20.000Z","dependencies_parsed_at":"2023-01-14T23:19:41.876Z","dependency_job_id":null,"html_url":"https://github.com/dai-shi/react-context-global-state","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/dai-shi%2Freact-context-global-state","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dai-shi%2Freact-context-global-state/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dai-shi%2Freact-context-global-state/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dai-shi%2Freact-context-global-state/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dai-shi","download_url":"https://codeload.github.com/dai-shi/react-context-global-state/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248986312,"owners_count":21194025,"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-api-react","react","state-management","stateless-components","typescript"],"created_at":"2024-08-03T18:01:21.748Z","updated_at":"2025-04-15T01:09:26.999Z","avatar_url":"https://github.com/dai-shi.png","language":"JavaScript","funding_links":[],"categories":["Libraries","List"],"sub_categories":[],"readme":"# react-context-global-state\n\n[![Build Status](https://travis-ci.com/dai-shi/react-context-global-state.svg?branch=master)](https://travis-ci.com/dai-shi/react-context-global-state)\n[![npm version](https://badge.fury.io/js/react-context-global-state.svg)](https://badge.fury.io/js/react-context-global-state)\n[![bundle size](https://badgen.net/bundlephobia/minzip/react-context-global-state)](https://bundlephobia.com/result?p=react-context-global-state)\n\nSimple global state for React with Context API\n\n## Background\n\nReact v16.3 introduces a new Context API.\nContext API allows to pass values down to a component tree\nwithout explicit props.\n\nIn favor of writing all components stateless in \"thisless\" JavaScript,\nthis package is developed.\nThis package provides a simple way to define a global state\nwith Context API.\n\n## Install\n\n```bash\nnpm install react-context-global-state\n```\n\n## Usage\n\n```javascript\nimport React from 'react';\nimport { createGlobalState } from 'react-context-global-state';\n\nconst initialState = { count: 0 };\nconst { StateProvider, StateConsumer } = createGlobalState(initialState);\n\nconst Counter = () =\u003e (\n  \u003cStateConsumer name=\"count\"\u003e\n    {(value, update) =\u003e (\n      \u003cdiv\u003e\n        \u003cspan\u003eCounter: {value}\u003c/span\u003e\n        \u003cbutton onClick={() =\u003e update(v =\u003e v + 1)}\u003eClick\u003c/button\u003e\n      \u003c/div\u003e\n    )}\n  \u003c/StateConsumer\u003e\n);\n\nconst App = () =\u003e (\n  \u003cStateProvider\u003e\n    \u003cCounter /\u003e\n  \u003c/StateProvider\u003e\n);\n```\n\n## Examples\n\nThe [examples](examples) folder contains working examples.\nYou can run one of them with\n\n```bash\nPORT=8080 npm run examples:01_minimal\n```\n\nand open \u003chttp://localhost:8080\u003e in your web browser.\n\nYou can also try them in codesandbox.io:\n[01](https://codesandbox.io/s/github/dai-shi/react-context-global-state/tree/master/examples/01_minimal)\n[02](https://codesandbox.io/s/github/dai-shi/react-context-global-state/tree/master/examples/02_typescript)\n[03](https://codesandbox.io/s/github/dai-shi/react-context-global-state/tree/master/examples/03_actions)\n[04](https://codesandbox.io/s/github/dai-shi/react-context-global-state/tree/master/examples/04_fetch)\n[05](https://codesandbox.io/s/github/dai-shi/react-context-global-state/tree/master/examples/05_onmount)\n\n## Reference\n\n### Syntax\n```\ncreateGlobalState(initialState)\n```\n\n### Parameters\n`initialState`: an object like `{ name1: value1, name2: value2, ... }`\n\n### Return value\nAn object of `{ StateProvider, StateConsumer, setGlobalState }`\n\n`StateProvider`: a component to provide entire state\n\n`StateConsumer`: a component that receives a required `name` prop and invokes a child function prop whose signature is `(value, update) =\u003e {}` where `value` is the state value specified by the `name` and `update` is a function to update the value\n\nThe `update` function above is similar to `Component.prototype.setState`, and accepts either an updating function or a new value itself.\n\n`setGlobalState`: a function to update a value in global state from outside of components. It recevies a `name` and a `update` value (or function).\n\n`getGlobalState`: a function to get a value in global state from outside of components. It recevies a `name`.\n\n## Blogs\n\n- [React global state by Context API](https://blog.axlight.com/posts/react-global-state-by-context-api/)\n- [React global state by Context API for TypeScript](https://blog.axlight.com/posts/react-global-state-by-context-api-for-typescript/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdai-shi%2Freact-context-global-state","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdai-shi%2Freact-context-global-state","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdai-shi%2Freact-context-global-state/lists"}