{"_id":"@bojanv55/redux-thunk","_rev":"1-2ec5d6014397bc61853e16729da06748","name":"@bojanv55/redux-thunk","dist-tags":{"latest":"2.3.0"},"versions":{"2.3.0":{"name":"@bojanv55/redux-thunk","version":"2.3.0","license":"MIT","description":"Thunk middleware for Redux.","repository":{"type":"git","url":"git+https://github.com/reduxjs/redux-thunk.git"},"bugs":{"url":"https://github.com/reduxjs/redux-thunk/issues"},"homepage":"https://github.com/reduxjs/redux-thunk","keywords":["redux","thunk","middleware","redux-middleware","flux"],"author":{"name":"Dan Abramov","email":"dan.abramov@me.com"},"main":"lib/index.js","module":"es/index.js","types":"src/index.d.ts","sideEffects":false,"scripts":{"clean":"rimraf lib dist es","prepare":"npm run clean && npm run lint && npm run test && npm run build","lint":"eslint src test","test":"cross-env BABEL_ENV=commonjs mocha --require @babel/register --reporter spec test/*.js","build":"npm run build:commonjs && npm run build:umd && npm run build:umd:min && npm run build:es","build:commonjs":"cross-env BABEL_ENV=commonjs babel src --out-dir lib","build:es":"cross-env BABEL_ENV=es babel src --out-dir es","build:umd":"cross-env BABEL_ENV=commonjs NODE_ENV=development npx webpack","build:umd:min":"cross-env BABEL_ENV=commonjs NODE_ENV=production npx webpack"},"husky":{"hooks":{"pre-commit":"lint-staged","pre-push":"npm test"}},"lint-staged":{"*.{js,ts}":["eslint --fix","prettier --write","git add"],"*.{json,md}":["prettier --write","git add"]},"prettier":{"arrowParens":"always","proseWrap":"always","singleQuote":true,"trailingComma":"all"},"peerDependencies":{"redux":"^4"},"devDependencies":{"@babel/cli":"^7.5.5","@babel/core":"^7.5.5","@babel/preset-env":"^7.5.5","@babel/register":"^7.5.5","@typescript-eslint/eslint-plugin":"^2.0.0","@typescript-eslint/parser":"^2.0.0","babel-loader":"^8.0.6","chai":"^4.2.0","cross-env":"^5.2.0","eslint":"^6.2.2","eslint-config-airbnb":"18.0.1","eslint-config-prettier":"^6.1.0","eslint-plugin-import":"^2.18.2","eslint-plugin-jsx-a11y":"^6.2.3","eslint-plugin-prettier":"^3.1.0","eslint-plugin-react":"^7.14.3","husky":"^3.0.4","lint-staged":"^9.2.5","mocha":"^6.2.0","prettier":"^1.18.2","redux":"^4","rimraf":"^3.0.0","typescript":"^3.6.2","typings-tester":"^0.3.2","webpack":"^4.39.3","webpack-cli":"^3.3.7"},"directories":{"test":"test"},"gitHead":"80296ec9ef49410e2eb29b3531ceac094d5e03bd","_id":"@bojanv55/redux-thunk@2.3.0","_nodeVersion":"13.8.0","_npmVersion":"6.14.7","dist":{"integrity":"sha512-ZvEAn3KwqRTN1GLwfv0JqnqV4BsCFg281b8Bkf4KEG/bAB6UwmJDS1rR/kzIVGG0TQGDXAlhev5gjftm63UwPQ==","shasum":"cd3e113a96a44bbea825250c347c6d9a9ef8ecc2","tarball":"https://registry.npmjs.org/@bojanv55/redux-thunk/-/redux-thunk-2.3.0.tgz","fileCount":9,"unpackedSize":26924,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfQE/ECRA9TVsSAnZWagAAmYwP/jNAB45FOcJAntOHCCG1\n13uWE6Ivt5LbUOH0XP6P+81/wVesbR1sYoDT0kGfTBAkOBS/M2FfO5rm0ImA\ndGXLoOagYYzrZb258EJQPK4K/iOXRZjRZmCNmU1GZZHs+zSsjWnFDZKkg1Nt\nLkNGrGnUL3mUIHu+jX/oIHVpW6ZA3/MCmazfR2r1ufzEbAsnOLVlTYJwHGkI\nffNlECnJgc959xGYlDIorHC8O9bOf+5j0QuRZ/7IhMNbjmo6KaLN3NRnO6Zc\nAWYbl7xj5tCBwmmY6qfb4N3PxeWONvaBU4ZzMuDtyFsXxK/bShncvv54SEmk\nW3LXiFKziGm5Qlwbw8WIkDUzAa/A1AyjEPlnxFqBB0V4/WdYhQHtz3u+wr/A\nO0eyEJ9gxHnAFAekCQyAgPaIntGIl9JORmghUEYcPQrxq69hMx/C4Tauh8Mj\nsCA9NtwXBu2D9QCs9N792QyZhxHd7OXgmtgxRo3Tusj6NzblKvotU5ogh5O2\n46X+n6Xzged2GEzZZNYD2oyqfdqMhHVz0c0QdZ3evAVlS3NMJXbvi1WRysXS\n+HvwuKS6adZdpVurWzudgTGC/ENLo+ybYI9l1QZMgMr0/GMQHT96k/J41gLB\n9DC3hiXWQ2tiF7ncy2xFdfjRc1chyaDIKEvOimeMsksDCPg2iqoIbqWdNJp7\n3+Tf\r\n=Kg/w\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIGo0j0xSaIsbzTQxRkJM5w06xMmcU84RGGnDi9dwKUUzAiAGozt2JUHgKKHuA+iA18uLbpt9mkBnMuLxagQJu2F1eg=="}]},"maintainers":[{"name":"bojanv55","email":"bojanv55@gmail.com"}],"_npmUser":{"name":"bojanv55","email":"bojanv55@gmail.com"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/redux-thunk_2.3.0_1598050243564_0.021108740461099895"},"_hasShrinkwrap":false}},"time":{"created":"2020-08-21T22:50:43.529Z","2.3.0":"2020-08-21T22:50:43.719Z","modified":"2022-04-04T19:59:21.240Z"},"maintainers":[{"name":"bojanv55","email":"bojanv55@gmail.com"}],"description":"Thunk middleware for Redux.","homepage":"https://github.com/reduxjs/redux-thunk","keywords":["redux","thunk","middleware","redux-middleware","flux"],"repository":{"type":"git","url":"git+https://github.com/reduxjs/redux-thunk.git"},"author":{"name":"Dan Abramov","email":"dan.abramov@me.com"},"bugs":{"url":"https://github.com/reduxjs/redux-thunk/issues"},"license":"MIT","readme":"# Redux Thunk\n\nThunk [middleware](https://redux.js.org/advanced/middleware) for Redux.\n\n[![build status](https://img.shields.io/travis/reduxjs/redux-thunk/master.svg?style=flat-square)](https://travis-ci.org/reduxjs/redux-thunk)\n[![npm version](https://img.shields.io/npm/v/redux-thunk.svg?style=flat-square)](https://www.npmjs.com/package/redux-thunk)\n[![npm downloads](https://img.shields.io/npm/dm/redux-thunk.svg?style=flat-square)](https://www.npmjs.com/package/redux-thunk)\n\n```js\nnpm install redux-thunk\n```\n\n## Note on 2.x Update\n\nMost tutorials today assume that you're using Redux Thunk 1.x. You may run into\nissues when you run their code with 2.x. **If you use Redux Thunk 2.x in\nCommonJS environment,\n[don’t forget to add `.default` to your import](https://github.com/reduxjs/redux-thunk/releases/tag/v2.0.0):**\n\n```diff\n- const ReduxThunk = require('redux-thunk')\n+ const ReduxThunk = require('redux-thunk').default\n```\n\nIf you used ES modules, you’re already all good:\n\n```js\nimport ReduxThunk from 'redux-thunk'; // no changes here 😀\n```\n\nAdditionally, since 2.x, we also support a\n[UMD build](https://unpkg.com/redux-thunk/dist/redux-thunk.min.js):\n\n```js\nconst ReduxThunk = window.ReduxThunk.default;\n```\n\nAs you can see, it also requires `.default` at the end.\n\n## Why Do I Need This?\n\nWith a plain basic Redux store, you can only do simple synchronous updates by\ndispatching an action. Middleware extends the store's abilities, and lets you\nwrite async logic that interacts with the store.\n\nThunks are the recommended middleware for basic Redux side effects logic,\nincluding complex synchronous logic that needs access to the store, and simple\nasync logic like AJAX requests.\n\nFor more details on why thunks are useful, see:\n\n- **Stack Overflow: Dispatching Redux Actions with a Timeout**  \n  http://stackoverflow.com/questions/35411423/how-to-dispatch-a-redux-action-with-a-timeout/35415559#35415559  \n  Dan Abramov explains the basics of managing async behavior in Redux, walking\n  through a progressive series of approaches (inline async calls, async action\n  creators, thunk middleware).\n\n- **Stack Overflow: Why do we need middleware for async flow in Redux?**  \n  http://stackoverflow.com/questions/34570758/why-do-we-need-middleware-for-async-flow-in-redux/34599594#34599594  \n  Dan Abramov gives reasons for using thunks and async middleware, and some\n  useful patterns for using thunks.\n\n- **What the heck is a \"thunk\"?**  \n  https://daveceddia.com/what-is-a-thunk/  \n  A quick explanation for what the word \"thunk\" means in general, and for Redux\n  specifically.\n\n- **Thunks in Redux: The Basics**  \n  https://medium.com/fullstack-academy/thunks-in-redux-the-basics-85e538a3fe60  \n  A detailed look at what thunks are, what they solve, and how to use them.\n\nYou may also want to read the\n**[Redux FAQ entry on choosing which async middleware to use](https://redux.js.org/faq/actions#what-async-middleware-should-i-use-how-do-you-decide-between-thunks-sagas-observables-or-something-else)**.\n\nWhile the thunk middleware is not directly included with the Redux core library,\nit is used by default in our\n**[`@reduxjs/toolkit` package](https://github.com/reduxjs/redux-toolkit)**.\n\n## Motivation\n\nRedux Thunk [middleware](https://redux.js.org/advanced/middleware)\nallows you to write action creators that return a function instead of an action.\nThe thunk can be used to delay the dispatch of an action, or to dispatch only if\na certain condition is met. The inner function receives the store methods\n`dispatch` and `getState` as parameters.\n\nAn action creator that returns a function to perform asynchronous dispatch:\n\n```js\nconst INCREMENT_COUNTER = 'INCREMENT_COUNTER';\n\nfunction increment() {\n  return {\n    type: INCREMENT_COUNTER,\n  };\n}\n\nfunction incrementAsync() {\n  return (dispatch) => {\n    setTimeout(() => {\n      // Yay! Can invoke sync or async actions with `dispatch`\n      dispatch(increment());\n    }, 1000);\n  };\n}\n```\n\nAn action creator that returns a function to perform conditional dispatch:\n\n```js\nfunction incrementIfOdd() {\n  return (dispatch, getState) => {\n    const { counter } = getState();\n\n    if (counter % 2 === 0) {\n      return;\n    }\n\n    dispatch(increment());\n  };\n}\n```\n\n## What’s a thunk?!\n\nA [thunk](https://en.wikipedia.org/wiki/Thunk) is a function that wraps an\nexpression to delay its evaluation.\n\n```js\n// calculation of 1 + 2 is immediate\n// x === 3\nlet x = 1 + 2;\n\n// calculation of 1 + 2 is delayed\n// foo can be called later to perform the calculation\n// foo is a thunk!\nlet foo = () => 1 + 2;\n```\n\nThe term [originated](https://en.wikipedia.org/wiki/Thunk#cite_note-1) as a\nhumorous past-tense version of \"think\".\n\n## Installation\n\n```bash\nnpm install redux-thunk\n```\n\nThen, to enable Redux Thunk, use\n[`applyMiddleware()`](https://redux.js.org/api/applymiddleware):\n\n```js\nimport { createStore, applyMiddleware } from 'redux';\nimport thunk from 'redux-thunk';\nimport rootReducer from './reducers/index';\n\n// Note: this API requires redux@>=3.1.0\nconst store = createStore(rootReducer, applyMiddleware(thunk));\n```\n\n## Composition\n\nAny return value from the inner function will be available as the return value\nof `dispatch` itself. This is convenient for orchestrating an asynchronous\ncontrol flow with thunk action creators dispatching each other and returning\nPromises to wait for each other’s completion:\n\n```js\nimport { createStore, applyMiddleware } from 'redux';\nimport thunk from 'redux-thunk';\nimport rootReducer from './reducers';\n\n// Note: this API requires redux@>=3.1.0\nconst store = createStore(rootReducer, applyMiddleware(thunk));\n\nfunction fetchSecretSauce() {\n  return fetch('https://www.google.com/search?q=secret+sauce');\n}\n\n// These are the normal action creators you have seen so far.\n// The actions they return can be dispatched without any middleware.\n// However, they only express “facts” and not the “async flow”.\n\nfunction makeASandwich(forPerson, secretSauce) {\n  return {\n    type: 'MAKE_SANDWICH',\n    forPerson,\n    secretSauce,\n  };\n}\n\nfunction apologize(fromPerson, toPerson, error) {\n  return {\n    type: 'APOLOGIZE',\n    fromPerson,\n    toPerson,\n    error,\n  };\n}\n\nfunction withdrawMoney(amount) {\n  return {\n    type: 'WITHDRAW',\n    amount,\n  };\n}\n\n// Even without middleware, you can dispatch an action:\nstore.dispatch(withdrawMoney(100));\n\n// But what do you do when you need to start an asynchronous action,\n// such as an API call, or a router transition?\n\n// Meet thunks.\n// A thunk in this context is a function that can be dispatched to perform async\n// activity and can dispatch actions and read state.\n// This is an action creator that returns a thunk:\nfunction makeASandwichWithSecretSauce(forPerson) {\n  // We can invert control here by returning a function - the \"thunk\".\n  // When this function is passed to `dispatch`, the thunk middleware will intercept it,\n  // and call it with `dispatch` and `getState` as arguments.\n  // This gives the thunk function the ability to run some logic, and still interact with the store.\n  return function(dispatch) {\n    return fetchSecretSauce().then(\n      (sauce) => dispatch(makeASandwich(forPerson, sauce)),\n      (error) => dispatch(apologize('The Sandwich Shop', forPerson, error)),\n    );\n  };\n}\n\n// Thunk middleware lets me dispatch thunk async actions\n// as if they were actions!\n\nstore.dispatch(makeASandwichWithSecretSauce('Me'));\n\n// It even takes care to return the thunk’s return value\n// from the dispatch, so I can chain Promises as long as I return them.\n\nstore.dispatch(makeASandwichWithSecretSauce('My partner')).then(() => {\n  console.log('Done!');\n});\n\n// In fact I can write action creators that dispatch\n// actions and async actions from other action creators,\n// and I can build my control flow with Promises.\n\nfunction makeSandwichesForEverybody() {\n  return function(dispatch, getState) {\n    if (!getState().sandwiches.isShopOpen) {\n      // You don’t have to return Promises, but it’s a handy convention\n      // so the caller can always call .then() on async dispatch result.\n\n      return Promise.resolve();\n    }\n\n    // We can dispatch both plain object actions and other thunks,\n    // which lets us compose the asynchronous actions in a single flow.\n\n    return dispatch(makeASandwichWithSecretSauce('My Grandma'))\n      .then(() =>\n        Promise.all([\n          dispatch(makeASandwichWithSecretSauce('Me')),\n          dispatch(makeASandwichWithSecretSauce('My wife')),\n        ]),\n      )\n      .then(() => dispatch(makeASandwichWithSecretSauce('Our kids')))\n      .then(() =>\n        dispatch(\n          getState().myMoney > 42\n            ? withdrawMoney(42)\n            : apologize('Me', 'The Sandwich Shop'),\n        ),\n      );\n  };\n}\n\n// This is very useful for server side rendering, because I can wait\n// until data is available, then synchronously render the app.\n\nstore\n  .dispatch(makeSandwichesForEverybody())\n  .then(() =>\n    response.send(ReactDOMServer.renderToString(<MyApp store={store} />)),\n  );\n\n// I can also dispatch a thunk async action from a component\n// any time its props change to load the missing data.\n\nimport { connect } from 'react-redux';\nimport { Component } from 'react';\n\nclass SandwichShop extends Component {\n  componentDidMount() {\n    this.props.dispatch(makeASandwichWithSecretSauce(this.props.forPerson));\n  }\n\n  componentDidUpdate(prevProps) {\n    if (prevProps.forPerson !== this.props.forPerson) {\n      this.props.dispatch(makeASandwichWithSecretSauce(this.props.forPerson));\n    }\n  }\n\n  render() {\n    return <p>{this.props.sandwiches.join('mustard')}</p>;\n  }\n}\n\nexport default connect((state) => ({\n  sandwiches: state.sandwiches,\n}))(SandwichShop);\n```\n\n## Injecting a Custom Argument\n\nSince 2.1.0, Redux Thunk supports injecting a custom argument using the\n`withExtraArgument` function:\n\n```js\nconst store = createStore(\n  reducer,\n  applyMiddleware(thunk.withExtraArgument(api)),\n);\n\n// later\nfunction fetchUser(id) {\n  return (dispatch, getState, api) => {\n    // you can use api here\n  };\n}\n```\n\nTo pass multiple things, just wrap them in a single object. \nUsing ES2015 shorthand property names can make this more concise.\n\n```js\nconst api = \"http://www.example.com/sandwiches/\";\nconst whatever = 42;\n\nconst store = createStore(\n  reducer,\n  applyMiddleware(thunk.withExtraArgument({ api, whatever })),\n);\n\n// later\nfunction fetchUser(id) {\n  return (dispatch, getState, { api, whatever }) => {\n    // you can use api and something else here\n  };\n}\n```\n\n## License\n\nMIT\n","readmeFilename":"README.md"}