{"id":13431674,"url":"https://github.com/use-global-hook/use-global-hook","last_synced_at":"2025-03-16T12:31:01.679Z","repository":{"id":42721326,"uuid":"179318505","full_name":"use-global-hook/use-global-hook","owner":"use-global-hook","description":null,"archived":false,"fork":false,"pushed_at":"2023-03-05T10:12:33.000Z","size":174,"stargazers_count":656,"open_issues_count":33,"forks_count":59,"subscribers_count":17,"default_branch":"master","last_synced_at":"2025-03-09T04:03:02.635Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/use-global-hook.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-04-03T15:28:31.000Z","updated_at":"2025-03-03T19:06:09.000Z","dependencies_parsed_at":"2024-06-18T12:38:38.617Z","dependency_job_id":"6aa7fa85-b25d-4f75-a033-1d7f45b7b463","html_url":"https://github.com/use-global-hook/use-global-hook","commit_stats":{"total_commits":57,"total_committers":20,"mean_commits":2.85,"dds":0.8771929824561404,"last_synced_commit":"23c701e78598c54aa702777234b031511ff7619c"},"previous_names":["andregardi/use-global-hook"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/use-global-hook%2Fuse-global-hook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/use-global-hook%2Fuse-global-hook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/use-global-hook%2Fuse-global-hook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/use-global-hook%2Fuse-global-hook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/use-global-hook","download_url":"https://codeload.github.com/use-global-hook/use-global-hook/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242821495,"owners_count":20190654,"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":[],"created_at":"2024-07-31T02:01:04.950Z","updated_at":"2025-03-16T12:31:01.398Z","avatar_url":"https://github.com/use-global-hook.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# use-global-hook\n\nEasy state management for react using hooks in less than 1kb.\n\n------------\nTable of Contents\n* [Install](#install)\n* [Example](#minimal-example)\n* [Complete Examples](#complete-examples)\n* [Using TypeScript](#using-typescript)\n\n### Install:\n\n```sh\nnpm i use-global-hook\n```\n\nor\n\n```sh\nyarn add use-global-hook\n```\n\n### Minimal example:\n```jsx\nimport React from 'react';\nimport globalHook from 'use-global-hook';\n\nconst initialState = {\n  counter: 0,\n};\n\nconst actions = {\n  addToCounter: (store, amount) =\u003e {\n    const newCounterValue = store.state.counter + amount;\n    store.setState({ counter: newCounterValue });\n  },\n};\n\nconst useGlobal = globalHook(initialState, actions);\n\nconst App = () =\u003e {\n  const [globalState, globalActions] = useGlobal();\n  return (\n    \u003cdiv\u003e\n      \u003cp\u003e\n        counter:\n        {globalState.counter}\n      \u003c/p\u003e\n      \u003cbutton type=\"button\" onClick={() =\u003e globalActions.addToCounter(1)}\u003e\n        +1 to global\n      \u003c/button\u003e\n    \u003c/div\u003e\n  );\n};\n\nexport default App;\n```\n\n\u003e Note: if \"useGlobal\" returns an object from state then any update of the object properties will also lead to re-render for consumers of the entire object.\n\n------------\n\n\n### Complete examples:\n#### [Several counters, one value](https://codesandbox.io/s/v6zz2nwow5 \"CodeSandBox\")\nAdd as many counters as you want, it will all share the same global value.\nEvery time one counter add 1 to the global value, all counters will render.\nThe parent component won't render again.\n\n\n------------\n\n\n#### [Asynchronous ajax requests](https://codesandbox.io/s/wqvykj5497 \"CodeSandBox\")\nSearch GitHub repos by username.\nHandle the ajax request asynchronously with async/await.\nUpdate the requests counter on every search.\n\n\n------------\n\n\n#### [Avoid unnecessary renders](https://codesandbox.io/s/several-counters-pdbsy \"CodeSandBox\")\nMap a subset of the global state before use it.\nThe component will only re-render if the subset is updated.\n\n------------\n\n\n#### [Connecting to a class component](https://codesandbox.io/s/connect-a-class-component-rgbf1 \"CodeSandBox\")\nHooks can't be used inside a class component.\nWe can create a Higher-Order Component that connects any class component with the state.\nWith the connect() function, state and actions become props of the component.\n\n\n------------\n\n\n#### [Immutable state with Immer.js integration](https://codesandbox.io/s/immer-integration-e1hpj \"CodeSandBox\")\nAdd Immer.js lib on your hook options to manage complex immutable states.\nMutate a state draft inside a setState function.\nImmer will calculate the state diff and create a new immutable state object.\n\n\n------------\n\n### Using TypeScript\n\nInstall the TypeScript definitions from DefinitelyTyped\n```\nnpm install @types/use-global-hook\n```\n\nExample implementation\n```typescript\nimport globalHook, { Store } from 'use-global-hook';\n\n// Defining your own state and associated actions is required\ntype MyState = {\n  value: string;\n};\n\n// Associated actions are what's expected to be returned from globalHook\ntype MyAssociatedActions = {\n  setValue: (value: string) =\u003e void;\n  otherAction: (other: boolean) =\u003e void;\n};\n\n// setValue will be returned by globalHook as setValue.bind(null, store)\n// This is one reason we have to declare a separate associated actions type\nconst setValue = (\n  store: Store\u003cMyState, MyAssociatedActions\u003e,\n  value: string\n) =\u003e {\n  store.setState({ ...store.state, value });\n  store.actions.otherAction(true);\n};\n\nconst otherAction = (\n  store: Store\u003cMyState, MyAssociatedActions\u003e,\n  other: boolean\n) =\u003e { /* cool stuff */ };\n\nconst initialState: MyState = {\n  value: \"myString\"\n};\n\n// actions passed to globalHook do not need to be typed\nconst actions = {\n  setValue,\n  otherAction\n};\n\nconst useGlobal = globalHook\u003cMyState, MyAssociatedActions\u003e(\n  initialState,\n  actions\n);\n\n// Usage\nconst [state, actions] = useGlobal\u003cMyState, MyAssociatedActions\u003e();\n\n// Subset\nconst [value, setValue] = useGlobal\u003cstring, (value: string) =\u003e void\u003e(\n  (state: MyState) =\u003e state.value,\n  (actions: MyAssociatedActions) =\u003e actions.setValue\n);\n\n// Without declaring type, useGlobal will return unknown\nconst [state, actions] = useGlobal(); // returns [unknown, unknown]\n\n// Happy TypeScripting!\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuse-global-hook%2Fuse-global-hook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuse-global-hook%2Fuse-global-hook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuse-global-hook%2Fuse-global-hook/lists"}