{"id":13450566,"url":"https://github.com/venil7/react-usemiddleware","last_synced_at":"2025-08-10T15:06:46.008Z","repository":{"id":57347256,"uuid":"159827437","full_name":"venil7/react-usemiddleware","owner":"venil7","description":"React \u003e=16.7 hook, allowing to use standard Redux middleware with useReducer","archived":false,"fork":false,"pushed_at":"2018-12-02T19:21:40.000Z","size":454,"stargazers_count":17,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-23T03:47:37.501Z","etag":null,"topics":["hooks","javascript","react","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/react-usemiddleware","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/venil7.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":"2018-11-30T13:28:15.000Z","updated_at":"2025-07-17T16:50:50.000Z","dependencies_parsed_at":"2022-08-28T03:00:52.185Z","dependency_job_id":null,"html_url":"https://github.com/venil7/react-usemiddleware","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/venil7/react-usemiddleware","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/venil7%2Freact-usemiddleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/venil7%2Freact-usemiddleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/venil7%2Freact-usemiddleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/venil7%2Freact-usemiddleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/venil7","download_url":"https://codeload.github.com/venil7/react-usemiddleware/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/venil7%2Freact-usemiddleware/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269740332,"owners_count":24467753,"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-08-10T02:00:08.965Z","response_time":71,"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":["hooks","javascript","react","typescript"],"created_at":"2024-07-31T07:00:36.169Z","updated_at":"2025-08-10T15:06:45.972Z","avatar_url":"https://github.com/venil7.png","language":"JavaScript","funding_links":[],"categories":["Packages"],"sub_categories":[],"readme":"# React `useMiddleware` hook\n\n[![npm version](https://img.shields.io/npm/v/react-usemiddleware.svg?style=flat-square)](https://www.npmjs.com/package/react-usemiddleware)\n\n\nRedux compatible [middleware](https://redux.js.org/advanced/middleware) provider for React \u003e=16.7 [Hooks](https://reactjs.org/docs/hooks-intro.html)\n\n**react-useMiddleware** allows you to use [all existing Redux middlewares](https://github.com/xgrommx/awesome-redux#react---a-javascript-library-for-building-user-interfaces) with React's new feature called hooks.\nIt introduces new hook called `useMiddleware`, which is a wrapper around `useReducer`, but also allows you to pass an optional list of middlewares to be used.\n\n## Install\n```\n$ npm install react-usemiddleware --save\n$ yarn add react-usemiddleware\n```\n\n## API\n\nYou can use `useMiddleware` as a straight replacement for `useReducer`, and optionally pass 3rd parameter - an array of middlewares to be applied to a dispatch function.\n\n```\n const [state, dispatch] = useMiddleware(reducer, initialState, middlewares = []);\n\n```\n\nTakes 3 parameters:\n - `reducer`, same as passed into `useReducer` hook\n - `initialState`, same as passed into `useReducer` hook\n - `middlewares` - array of middlewares, eg, `[thunk, createLogger, saga]`\n\n## Example\n\n```\nimport React from \"react\";\nimport ReactDOM from \"react-dom\";\nimport { useReducer, useEffect, useState } from \"react\";\nimport { applyMiddleware } from \"redux\";\nimport thunk from \"redux-thunk\";\nimport { createLogger } from \"redux-logger\";\nimport useMiddleware from \"react-usemiddleware\";\n\nconst logger = createLogger();\nconst middlewares = [thunk, logger];\n\nconst initState = {\n  data: \"not loaded\"\n};\n\nconst reducer = (state, action) =\u003e {\n  switch (action.type) {\n    case \"LOAD\":\n      return { data: \"loading...\" };\n    case \"LOADED\":\n      return { data: action.payload };\n    default:\n      return state;\n  }\n};\n\nconst loadAction = () =\u003e async dispatch =\u003e {\n  dispatch({ type: \"LOAD\" });\n  const res = await fetch(\"https://jsonplaceholder.typicode.com/todos/1\");\n  const { title: payload } = await res.json();\n  dispatch(loadedAction(payload));\n};\n\nconst loadedAction = payload =\u003e {\n  return { type: \"LOADED\", payload };\n};\n\nfunction App() {\n  const [state, dispatch] = useMiddleware(reducer, initState, middlewares);\n  return (\n    \u003cdiv className=\"App\"\u003e\n      \u003cbutton onClick={() =\u003e dispatch(loadAction())}\u003eLOAD\u003c/button\u003e\n      \u003cspan\u003e{state.data}\u003c/span\u003e\n    \u003c/div\u003e\n  );\n}\n```\n\n## Live example\n\nA [live demo](https://codesandbox.io/s/48ovynqr97) can be found here\n\n![live demo](./demo.gif)\n\n\n## Contributions\n\nPlease open an [Issue](https://github.com/venil7/react-usemiddleware/issues) or a [PR](https://github.com/venil7/react-usemiddleware/pulls)\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvenil7%2Freact-usemiddleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvenil7%2Freact-usemiddleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvenil7%2Freact-usemiddleware/lists"}