{"id":19441494,"url":"https://github.com/dgca/react-use-dispatch-methods","last_synced_at":"2025-08-03T04:08:31.222Z","repository":{"id":57108126,"uuid":"279210576","full_name":"dgca/react-use-dispatch-methods","owner":"dgca","description":"Abstraction over useReducer to make writing reducer functions easier, cleaner, and more grokkable.","archived":false,"fork":false,"pushed_at":"2024-04-23T01:56:11.000Z","size":555,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-07T23:48:09.886Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dgca.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":"2020-07-13T04:31:46.000Z","updated_at":"2020-07-13T15:06:09.000Z","dependencies_parsed_at":"2022-08-20T17:11:04.080Z","dependency_job_id":null,"html_url":"https://github.com/dgca/react-use-dispatch-methods","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/dgca%2Freact-use-dispatch-methods","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgca%2Freact-use-dispatch-methods/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgca%2Freact-use-dispatch-methods/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgca%2Freact-use-dispatch-methods/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dgca","download_url":"https://codeload.github.com/dgca/react-use-dispatch-methods/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240625438,"owners_count":19831322,"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-11-10T15:35:50.260Z","updated_at":"2025-02-25T07:42:13.714Z","avatar_url":"https://github.com/dgca.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @dgca/react-use-dispatch-methods\n\n[![NPM](https://img.shields.io/npm/v/@dgca/react-use-dispatch-methods.svg)](https://www.npmjs.com/package/@dgca/react-use-dispatch-methods) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n\n## About\n\n`useDispatchMethods` is a wrapper over `useReducer` to make writing reducer functions easier, cleaner, and more grokkable.\n\nInstead of dispatching actions, and handling them in a handrolled reducer funciton, you pass `useDispatchMethods` an object of methods that take the current state and a payload, and return the new state, and it returns an object of methods that, when called, dispatch actions that do the things you just defined!\n\n## Install\n\n```bash\nnpm install --save @dgca/react-use-dispatch-methods\n```\n\n## Usage\n\n`useDispatchMethods(methods, initialState, init, dependencyArray)` takes four arguments:\n\n* `methods` - An object of pure functions. Each function's name will be the name you use to update the state later on. Each function will receive an object of `{state, payload}` as its argument, where `state` is the current state, and `payload` is the payload that was passed to the state (if an argument was passed).\n* `initialState` - The initial value of our state. See React's [docs](https://reactjs.org/docs/hooks-reference.html#specifying-the-initial-state) on specifying the initial state.\n* `init` - See React's [docs](https://reactjs.org/docs/hooks-reference.html#lazy-initialization) on lazy initialization.\n* `dependencyArray` - To avoid recreating the underlying objects that make this hook work, we memoize a couple things using `useMemo` and `useCallback`. If you _must_ modify your `methods`, pass a `dependencyArray` and we'll forward that onto `useMemo` and `useCallback`.\n\n`useDispatchMethods` returns an array of `[state, dispatch]` where `state` is the current state, and `dispatch` is an object of methods to use to update the state.\n\n## Example\n\nIn the example below, the `increment` and `decrement` functions show how to use the `state` destructured argument, and `setValue` shows how to use the `payload` argument.\n\n```jsx\nimport React from 'react'\nimport { useDispatchMethods } from '@dgca/react-use-dispatch-methods'\n\nconst Example = () =\u003e {\n  const [state, dispatch] = useDispatchMethods(\n    {\n      increment: ({ state }) =\u003e state + 1,\n      decrement: ({ state }) =\u003e state - 1,\n      setValue: ({ payload }) =\u003e payload\n    },\n    0\n  )\n\n  return (\n    \u003cdiv\u003e\n      \u003cp\u003eThe count is {state}\u003c/p\u003e\n      \u003cbutton onClick={dispatch.increment}\u003eIncrement\u003c/button\u003e\n      \u003cbutton onClick={dispatch.decrement}\u003eDecrement\u003c/button\u003e\n      \u003cinput\n        type='number'\n        value={state}\n        onChange={e =\u003e {\n          const num = parseInt(e.target.value, 10)\n          dispatch.setValue(num)\n        }}\n      /\u003e\n    \u003c/div\u003e\n  )\n}\n```\n\n## License\n\nMIT © [dgca](https://github.com/dgca)\n\n---\n\nThis hook is created using [create-react-hook](https://github.com/hermanya/create-react-hook).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgca%2Freact-use-dispatch-methods","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdgca%2Freact-use-dispatch-methods","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgca%2Freact-use-dispatch-methods/lists"}