{"_id":"@0y0/use-reducer-x","_rev":"1-0f72eedff53982fe384dd57e5a93675f","name":"@0y0/use-reducer-x","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@0y0/use-reducer-x","version":"1.0.0","description":"An alternative to React.useReducer that accepts middlewares to do some cool things before and after dispatch.","src":"src/index.js","main":"dist/index.cjs.js","module":"dist/index.esm.js","repository":{"type":"git","url":"https://github.com/o0y0o/use-reducer-x"},"author":{"name":"Jason Chung","email":"shiningjason1989@gmail.com"},"license":"MIT","publishConfig":{"access":"public"},"keywords":["react","react-hooks","hooks","use-reducer","use-state","redux","redux-middleware","middleware","dispatch"],"scripts":{"format":"prettier --write src/*.js","lint":"eslint src/*.js","test":"jest","build":"rollup -c"},"devDependencies":{"@babel/core":"^7.11.6","@babel/preset-env":"^7.11.5","@rollup/plugin-babel":"^5.2.1","@testing-library/react-hooks":"^3.4.1","babel-eslint":"^10.1.0","babel-jest":"^26.3.0","eslint":"^7.9.0","eslint-config-prettier":"^6.11.0","eslint-config-standard":"^14.1.1","eslint-plugin-import":"^2.22.0","eslint-plugin-node":"^11.1.0","eslint-plugin-promise":"^4.2.1","eslint-plugin-standard":"^4.0.1","jest":"^26.4.2","prettier":"^2.1.1","react":"^16.8.5","react-test-renderer":"^16.13.1","rollup":"^2.26.11"},"peerDependencies":{"react":">=16.8.0"},"licenseText":"The MIT License (MIT)\n\nCopyright (c) 2020 Jason Chung\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","_id":"@0y0/use-reducer-x@1.0.0","dist":{"shasum":"6fdfdb597c40e529f8f54c0e7ae408496d60c627","integrity":"sha512-L6VM3oWzHaZmnLKiVmflWD/YxmRtSAaSL59tXNHz46PzUhRsDDzkG028Y8J4FCLsSlMY6NFMqrVLU+TXwNluxw==","tarball":"https://registry.npmjs.org/@0y0/use-reducer-x/-/use-reducer-x-1.0.0.tgz","fileCount":7,"unpackedSize":7694,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfX0AOCRA9TVsSAnZWagAAH7QP+wc8CrmQKk8YFQpbcY6W\nw9+DPYSyhB2Xf27imZJ8NLQoCbSsgh855EWUpI5a/bdisSJFR9qfh6J3cW3l\nJYbWGza1QMheYDrRzvsUe2lOpVNyyJjtYlD0dUKUg3wW8BN8uJF32Nw7CWZx\nxXlJ8XcPG+hOds2sG44vJt3X0HwnHsL2M24Pub9FFaV45USfnX6KLBlLU6Cb\n9gWQzOlqPVuQI26rA+LbuiaGpXi0x7btgNss0YGmEzZM/qJNQctt5QpS55em\n22QbPdQK6z/MT+Lg0M9XvAiKjZC+G5802YQdbJnBz0hdxKG0Qu7vYDVqKyhz\nkaJFcgobpyzslF4UMhSxS2E0GWxckP1MsnIkLACQdG0mEjLc0/1St1xIhImN\noVEcbFdcKMvWvfMnV7pe04gGyWt0pgruZWI7qSgJ2WQeNWJe1bsNSfpALVXC\nltlgSISrWnHzDcFWqiiqMkvjBp3w82VQ4G56F98JrsK3XJIKBYtzfh5BuHb2\ngEVUnjzZrkxWxraPkj/XTUzN5gsK8REv5rCgjISHm8QnxccoTVhC8O74G9Wm\nsvha8/DAhSyIDgO+gYuGhhXc4fWa6F5Wq+bTMcmrt6jQPIQrmIcliR1AXI+e\nV4KcXgB0gfTMLp1fFJeHIFDDwutCOMFSioq+loFkz5SnVrB9mFFyaLO4l+Ay\nFjT7\r\n=p4UF\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDLPWLJ1qPLkTIx0zFQvwDHURTCOiOUXESjnjAwfubgIAIgZKiey7l8xFZUPci0rd2VqPsy6YFlNAlizWMFrAHxNAU="}]},"maintainers":[{"name":"shiningjason","email":"shiningjason1989@gmail.com"}],"_npmUser":{"name":"shiningjason","email":"shiningjason1989@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/use-reducer-x_1.0.0_1600077837604_0.1079119700914517"},"_hasShrinkwrap":false}},"time":{"created":"2020-09-14T10:03:57.388Z","1.0.0":"2020-09-14T10:03:57.720Z","modified":"2022-04-04T10:29:43.850Z"},"maintainers":[{"name":"shiningjason","email":"shiningjason1989@gmail.com"}],"description":"An alternative to React.useReducer that accepts middlewares to do some cool things before and after dispatch.","keywords":["react","react-hooks","hooks","use-reducer","use-state","redux","redux-middleware","middleware","dispatch"],"repository":{"type":"git","url":"https://github.com/o0y0o/use-reducer-x"},"author":{"name":"Jason Chung","email":"shiningjason1989@gmail.com"},"license":"MIT","readme":"# @0y0/use-reducer-x · [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/o0y0o/use-reducer-x/blob/master/LICENSE) [![npm](https://img.shields.io/npm/v/@0y0/use-reducer-x.svg)](https://www.npmjs.com/package/@0y0/use-reducer-x) ![Status](https://github.com/o0y0o/use-reducer-x/workflows/Package/badge.svg)\n\n`@0y0/use-reducer-x` is an alternative to `React.useReducer` that accepts middlewares to do some cool things before and after dispatch.\n\nInspired by [Redux Middleware](https://redux.js.org/api/applymiddleware).\n\n### 3-second quick look\n\n```js\nimport useReducerX from '@0y0/use-reducer-x'\n\nfunction App() {\n  const middlewares = [\n    ({ getState, dispatch }) => next => action => {\n      // do something before dispatch...\n      next(action)\n       // do something after dispatch...\n    }\n  ]\n  const [state, dispatch] = useReducerX(reducer, initialState, middlewares)\n  // ...\n}\n```\n\n## Installation\n\n```\nnpm install @0y0/use-reducer-x --save\n```\n\n## Real-world Usage\n\n```js\nimport React from 'react'\nimport useReducerX from '@0y0/use-reducer-x'\nimport thunkMiddleware from 'redux-thunk'\n\nfunction logMiddleware({ getState }) {\n  return next => action => {\n    console.log('Prev State:', getState())\n    console.log('Action:', action)\n    next(action)\n    console.log('Next State:', getState())\n  }\n}\n\nfunction gaMiddleware({ getState }) {\n  return next => action => {\n    window.ga && window.ga('send', 'event', 'Action', action.type)\n    next(action)\n  }\n}\n\nfunction useAppReducer(reducer, inititalState) {\n  return useReducerX(reducer, inititalState, [\n    thunkMiddleware,\n    logMiddleware,\n    gaMiddleware\n  ])\n}\n\nfunction counterReducer(state, action) {\n  switch (action.type) {\n    case '+1': return { count: state.count + 1 }\n    case '-1': return { count: state.count - 1 }\n    case '0': return { count: 0 }\n    default: return state\n  }\n}\n\nfunction resetCounterAfter1Second() {\n  return dispatch => setTimeout(() => { dispatch({ type: '0' }) }, 1000)\n}\n\nfunction App() {\n  const [state, dispatch] = useAppReducer(counterReducer, 0)\n  return (\n    <React.Fragment>\n      <button onClick={() => dispatch({ type: '+1' })}>+</button>\n      <button onClick={() => dispatch(resetCounterAfter1Second())}>Reset</button>\n      <button onClick={() => dispatch({ type: '-1' })}>-</button>\n      <br />\n      Count: {state.count}\n    </React.Fragment>\n  )\n}\n```\n\nTry the demo in [codesanbox](https://codesandbox.io/s/xono668ynz).\n\n## License\n\n[MIT](https://github.com/o0y0o/use-reducer-x/blob/master/LICENSE)\n","readmeFilename":"README.md"}