{"id":13727056,"url":"https://github.com/phryneas/use-local-slice","last_synced_at":"2025-07-27T03:33:57.671Z","repository":{"id":50462056,"uuid":"180014651","full_name":"phryneas/use-local-slice","owner":"phryneas","description":"A react hook to use reducers for local state in a typesafe way, with an API like createSlice from redux-starter-kit and immer integration.","archived":false,"fork":false,"pushed_at":"2023-01-15T15:28:55.000Z","size":647,"stargazers_count":45,"open_issues_count":4,"forks_count":6,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-07-20T00:57:25.400Z","etag":null,"topics":["library"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/use-local-slice","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/phryneas.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-07T19:41:37.000Z","updated_at":"2025-05-17T19:23:15.000Z","dependencies_parsed_at":"2023-02-09T22:35:13.715Z","dependency_job_id":null,"html_url":"https://github.com/phryneas/use-local-slice","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/phryneas/use-local-slice","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phryneas%2Fuse-local-slice","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phryneas%2Fuse-local-slice/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phryneas%2Fuse-local-slice/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phryneas%2Fuse-local-slice/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phryneas","download_url":"https://codeload.github.com/phryneas/use-local-slice/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phryneas%2Fuse-local-slice/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267294180,"owners_count":24065343,"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","status":"online","status_checked_at":"2025-07-27T02:00:11.917Z","response_time":82,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["library"],"created_at":"2024-08-03T01:03:37.738Z","updated_at":"2025-07-27T03:33:57.625Z","avatar_url":"https://github.com/phryneas.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# use-local-slice\n\nAn opinionated react hook to use reducers for local state\n\n- in a typesafe way\n- with an API like [createSlice](https://redux-starter-kit.js.org/api/createslice) from [redux-starter-kit](https://redux-starter-kit.js.org)\n- with [immer](https://github.com/mweststrate/immer) integration\n\n## How to use it\n\n```typescript\nconst [state, dispatchAction] = useLocalSlice({\n  slice: \"my slice\", // optional - will be displayed in the debug tools\n  initialState: { data: \"initial text\", someOtherValue: 5 },\n  reducers: {\n    concat: (state, action: { payload: string }) =\u003e {\n      // reducers are passed an immer draft of the current state, so you can directly modify values in the draft\n      state.data += action.payload;\n    },\n    toUpper: state =\u003e ({\n      // or you return a modified copy yourself\n      ...state,\n      data: state.data.toUpperCase()\n    })\n    // more reducers ...\n  }\n});\n```\n\nand in some callback:\n\n```typescript\ndispatchAction.concat(\"concatenate me!\");\n// or\ndispatchAction.toUpper();\n```\n\nuse-local-slice provides one dispatchAction method per reducer, and (for typescript users) ensures that these dispatchers are only called with correct payload types.\n\n## Edge case uses \u0026 good to know stuff\n\n- reducers can directly reference other local component state \u0026 variables without the need for a `dependencies` array. This is normal `useReducer` behaviour. You can read up on this on the overreacted blog: [Why useReducer Is the Cheat Mode of Hooks](https://overreacted.io/a-complete-guide-to-useeffect/#why-usereducer-is-the-cheat-mode-of-hooks)\n- you can exchange reducers for others between renders - as long as the keys of the `reducers` property do not change, you will get an identical instance of `dispatchAction`.\n- only renaming, adding or removing keys will get you a new `dispatchAction` instance\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphryneas%2Fuse-local-slice","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphryneas%2Fuse-local-slice","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphryneas%2Fuse-local-slice/lists"}