{"id":18132010,"url":"https://github.com/anater/usereduction","last_synced_at":"2025-04-15T23:09:55.770Z","repository":{"id":35800045,"uuid":"219317074","full_name":"anater/useReduction","owner":"anater","description":"useReducer without boilerplate","archived":false,"fork":false,"pushed_at":"2022-06-18T16:18:28.000Z","size":64,"stargazers_count":41,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T23:09:49.989Z","etag":null,"topics":["flux","react","react-hooks","reducer","redux"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/use-reduction","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anater.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-11-03T14:54:33.000Z","updated_at":"2024-10-31T12:53:20.000Z","dependencies_parsed_at":"2022-09-01T06:01:37.149Z","dependency_job_id":null,"html_url":"https://github.com/anater/useReduction","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/anater%2FuseReduction","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anater%2FuseReduction/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anater%2FuseReduction/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anater%2FuseReduction/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anater","download_url":"https://codeload.github.com/anater/useReduction/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249167445,"owners_count":21223506,"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":["flux","react","react-hooks","reducer","redux"],"created_at":"2024-11-01T12:10:01.964Z","updated_at":"2025-04-15T23:09:55.747Z","avatar_url":"https://github.com/anater.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# useReduction\n\n```javascript\nfunction App() {\n  const [count, actions] = useReduction(0, {\n    increment: (count, { payload }) =\u003e count + payload,\n    decrement: (count, { payload }) =\u003e count - payload\n  });\n\n  return (\n    \u003cdiv\u003e\n      \u003ch1\u003e{count}\u003c/h1\u003e\n      \u003cbutton onClick={() =\u003e actions.increment(2)}\u003e+2\u003c/button\u003e\n      \u003cbutton onClick={() =\u003e actions.decrement(2)}\u003e-2\u003c/button\u003e\n    \u003c/div\u003e\n  );\n}\n```\n\nuseReduction minimizes reducer code by automatically creating action creators from your reducer object.\n\nWrite less code by using objects instead of switch statements for defining reducers.\n\nDebugging is baked in. In debug mode, all dispatched actions are logged to the console.\n\n## Installation\n\n`npm install use-reduction`\n\n_Note_: React is a peer dependency. Its assumed you already have this installed for your project.\n\n## Usage\n\n```javascript\nimport useReduction from \"use-reduction\";\n// Call the hook with an initial state and a reducer object:\nconst initialState = 0;\nconst reducer = {\n  increment: (count, { payload }) =\u003e count + payload,\n  decrement: (count, { payload }) =\u003e count - payload\n};\n\nconst [state, actions] = useReduction(initialState, reducer);\n```\n\nThe hook returns the current state and an object with action creators. When an action creator is called, an action (`{ type, payload }`) will be dispatched to update your state.\n\n### Actions\n\nAction names are derived from reducer names to minimize boilerplate. Providing `useReduction` with a reducer `increment()` will generate an action creator also named `increment()` that dispatches an action with a `type` “increment” and uses the first argument as `payload`. `increment(1)` would use `1` as the `payload` provided to the reducer.\n\n```javascript\n// use actions to update count from previous example\nactions.increment(1)\n// count = 1\nactions.decrement(2)\n// count = -1\n```\n\n### Debugging\n\nPass `true` as the third argument in `useReduction` to enable debug mode. It will log the dispatched action to the console.\n\n```javascript\nconst [state, actions] = useReduction(initialState, reducerMap, true);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanater%2Fusereduction","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanater%2Fusereduction","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanater%2Fusereduction/lists"}