redux-thunk
Advanced tools
Comparing version 1.0.3 to 2.0.1
'use strict'; | ||
exports.__esModule = true; | ||
exports['default'] = thunkMiddleware; | ||
function thunkMiddleware(_ref) { | ||
@@ -9,7 +11,9 @@ var dispatch = _ref.dispatch; | ||
return function (action) { | ||
return typeof action === 'function' ? action(dispatch, getState) : next(action); | ||
if (typeof action === 'function') { | ||
return action(dispatch, getState); | ||
} | ||
return next(action); | ||
}; | ||
}; | ||
} | ||
module.exports = thunkMiddleware; | ||
} |
{ | ||
"name": "redux-thunk", | ||
"version": "1.0.3", | ||
"version": "2.0.1", | ||
"description": "Thunk middleware for Redux.", | ||
"main": "lib/index.js", | ||
"jsnext:main": "es/index.js", | ||
"files": [ | ||
"lib", | ||
"src" | ||
"es", | ||
"src", | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "babel src --out-dir lib", | ||
"prepublish": "rimraf lib && npm run build", | ||
"test": "mocha --compilers js:babel-core/register --reporter spec test/*.js" | ||
"clean": "rimraf lib dist es", | ||
"build": "npm run build:commonjs && npm run build:umd && npm run build:umd:min && npm run build:es", | ||
"prepublish": "npm run clean && npm run test && npm run build", | ||
"posttest": "npm run lint", | ||
"lint": "eslint src test", | ||
"test": "cross-env BABEL_ENV=commonjs mocha --compilers js:babel-core/register --reporter spec test/*.js", | ||
"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 webpack", | ||
"build:umd:min": "cross-env BABEL_ENV=commonjs NODE_ENV=production webpack" | ||
}, | ||
@@ -30,15 +40,35 @@ "repository": { | ||
"devDependencies": { | ||
"babel": "^6.1.18", | ||
"babel-cli": "^6.2.0", | ||
"babel-core": "^6.2.1", | ||
"babel-cli": "^6.6.5", | ||
"babel-core": "^6.6.5", | ||
"babel-eslint": "^5.0.0-beta4", | ||
"babel-plugin-add-module-exports": "^0.1.1", | ||
"babel-preset-es2015": "^6.1.18", | ||
"babel-preset-stage-0": "^6.1.18", | ||
"babel-loader": "^6.2.4", | ||
"babel-plugin-check-es2015-constants": "^6.6.5", | ||
"babel-plugin-transform-es2015-arrow-functions": "^6.5.2", | ||
"babel-plugin-transform-es2015-block-scoped-functions": "^6.6.5", | ||
"babel-plugin-transform-es2015-block-scoping": "^6.6.5", | ||
"babel-plugin-transform-es2015-classes": "^6.6.5", | ||
"babel-plugin-transform-es2015-computed-properties": "^6.6.5", | ||
"babel-plugin-transform-es2015-destructuring": "^6.6.5", | ||
"babel-plugin-transform-es2015-for-of": "^6.6.0", | ||
"babel-plugin-transform-es2015-function-name": "^6.5.0", | ||
"babel-plugin-transform-es2015-literals": "^6.5.0", | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.6.5", | ||
"babel-plugin-transform-es2015-object-super": "^6.6.5", | ||
"babel-plugin-transform-es2015-parameters": "^6.6.5", | ||
"babel-plugin-transform-es2015-shorthand-properties": "^6.5.0", | ||
"babel-plugin-transform-es2015-spread": "^6.6.5", | ||
"babel-plugin-transform-es2015-sticky-regex": "^6.5.0", | ||
"babel-plugin-transform-es2015-template-literals": "^6.6.5", | ||
"babel-plugin-transform-es2015-unicode-regex": "^6.5.0", | ||
"babel-plugin-transform-es3-member-expression-literals": "^6.5.0", | ||
"babel-plugin-transform-es3-property-literals": "^6.5.0", | ||
"chai": "^3.2.0", | ||
"cross-env": "^1.0.7", | ||
"eslint": "^1.10.2", | ||
"eslint-config-airbnb": "1.0.2", | ||
"eslint-plugin-react": "^4.1.0", | ||
"mocha": "^2.2.5", | ||
"rimraf": "^2.4.3" | ||
"rimraf": "^2.5.2", | ||
"webpack": "^1.12.14" | ||
} | ||
} |
Redux Thunk | ||
============= | ||
Thunk [middleware](http://rackt.github.io/redux/docs/advanced/Middleware.html) for Redux. | ||
Thunk [middleware](http://redux.js.org/docs/advanced/Middleware.html) for Redux. | ||
@@ -14,20 +14,11 @@ [![build status](https://img.shields.io/travis/gaearon/redux-thunk/master.svg?style=flat-square)](https://travis-ci.org/gaearon/redux-thunk) | ||
## What’s a thunk?! | ||
## Why Do I Need This? | ||
A [thunk](https://en.wikipedia.org/wiki/Thunk) is a function that wraps an expression to delay its evaluation. | ||
If you’re not sure whether you need it, you probably don’t. | ||
```js | ||
// calculation of 1 + 2 is immediate | ||
// x === 3 | ||
let x = 1 + 2; | ||
**[Read this for an in-depth introduction to thunks in Redux.](http://stackoverflow.com/questions/35411423/how-to-dispatch-a-redux-action-with-a-timeout/35415559#35415559)** | ||
// calculation of 1 + 2 is delayed | ||
// foo can be called later to perform the calculation | ||
// foo is a thunk! | ||
let foo = () => 1 + 2; | ||
``` | ||
## Motivation | ||
Redux Thunk [middleware](https://github.com/rackt/redux/blob/master/docs/advanced/Middleware.md) allows you to write action creators that return a function instead of an action. The thunk can be used to delay the dispatch of an action, or to dispatch only if a certain condition is met. The inner function receives the store methods `dispatch` and `getState()` as parameters. | ||
Redux Thunk [middleware](https://github.com/reactjs/redux/blob/master/docs/advanced/Middleware.md) allows you to write action creators that return a function instead of an action. The thunk can be used to delay the dispatch of an action, or to dispatch only if a certain condition is met. The inner function receives the store methods `dispatch` and `getState()` as parameters. | ||
@@ -71,2 +62,18 @@ An action creator that returns a function to perform asynchronous dispatch: | ||
## What’s a thunk?! | ||
A [thunk](https://en.wikipedia.org/wiki/Thunk) is a function that wraps an expression to delay its evaluation. | ||
```js | ||
// calculation of 1 + 2 is immediate | ||
// x === 3 | ||
let x = 1 + 2; | ||
// calculation of 1 + 2 is delayed | ||
// foo can be called later to perform the calculation | ||
// foo is a thunk! | ||
let foo = () => 1 + 2; | ||
``` | ||
## Installation | ||
@@ -78,3 +85,3 @@ | ||
Then, to enable Redux Thunk, use [`applyMiddleware()`](http://rackt.github.io/redux/docs/api/applyMiddleware.html): | ||
Then, to enable Redux Thunk, use [`applyMiddleware()`](http://redux.js.org/docs/api/applyMiddleware.html): | ||
@@ -86,8 +93,7 @@ ```js | ||
// create a store that has redux-thunk middleware enabled | ||
const createStoreWithMiddleware = applyMiddleware( | ||
thunk | ||
)(createStore); | ||
const store = createStoreWithMiddleware(rootReducer); | ||
// Note: this API requires redux@>=3.1.0 | ||
const store = createStore( | ||
rootReducer, | ||
applyMiddleware(thunk) | ||
); | ||
``` | ||
@@ -104,8 +110,8 @@ | ||
// applyMiddleware supercharges createStore with middleware: | ||
let createStoreWithMiddleware = applyMiddleware(thunk)(createStore); | ||
// Note: this API requires redux@>=3.1.0 | ||
const store = createStore( | ||
rootReducer, | ||
applyMiddleware(thunk) | ||
); | ||
// We can use it exactly like “vanilla” createStore. | ||
let store = createStoreWithMiddleware(rootReducer); | ||
function fetchSecretSauce() { | ||
@@ -112,0 +118,0 @@ return fetch('https://www.google.com/search?q=secret+sauce'); |
@@ -1,8 +0,9 @@ | ||
function thunkMiddleware({ dispatch, getState }) { | ||
return next => action => | ||
typeof action === 'function' ? | ||
action(dispatch, getState) : | ||
next(action); | ||
export default function thunkMiddleware({ dispatch, getState }) { | ||
return next => action => { | ||
if (typeof action === 'function') { | ||
return action(dispatch, getState); | ||
} | ||
return next(action); | ||
}; | ||
} | ||
module.exports = thunkMiddleware |
14707
8
99
264
32