freeCodeCamp/guide/chinese/redux/redux-thunk/index.md

75 lines
2.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: Redux Thunk
localeTitle: Redux Thunk
---
## Redux Thunk
Redux Thunk是一个中间件允许您在Redux 1中返回函数而不仅仅是动作。这允许延迟的行动包括使用承诺。
我们使用这个中间件的原因是因为并非所有我们执行的操作都是同步的而某些操作必然是非同步的比如使用axios发送get请求。这将需要一些时间简单的redux不会考虑到这种行为。因此Redux-thunk通过允许我们异步调度动作来解决问题这样我们就可以让这些承诺得到解决。
例1
```javascript
const INCREMENT_COUNTER = 'INCREMENT_COUNTER';
function increment() {
return {
type: INCREMENT_COUNTER
};
}
function incrementAsync() {
return dispatch => {
setTimeout(() => {
// Yay! Can invoke sync or async actions with `dispatch`
dispatch(increment());
}, 1000);
};
}
```
例2
```javascript
const GET_CURRENT_USER = 'GET_CURRENT_USER';
const GET_CURRENT_USER_SUCCESS = 'GET_CURRENT_USER_SUCCESS';
const GET_CURRENT_USER_FAILURE = 'GET_CURRENT_USER_FAILURE';
const getUser = () => {
return (dispatch) => { //nameless functions
// Initial action dispatched
dispatch({ type: GET_CURRENT_USER });
// Return promise with success and failure actions
return axios.get('/api/auth/user').then(
user => dispatch({ type: GET_CURRENT_USER_SUCCESS, user }),
err => dispatch({ type: GET_CURRENT_USER_FAILURE, err })
);
};
};
```
### 安装和设置
可以使用命令行使用`npm install redux-thunk --save`或`yarn add redux-thunk`安装Redux Thunk。因为它是Redux工具所以您还需要设置Redux。安装后使用`applyMiddleware()`启用它:
```javascript
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers/index';
const store = createStore(
rootReducer,
applyMiddleware(thunk)
);
```
### 参考
* [Redux Thunk GitHub Repo](https://github.com/reduxjs/redux-thunk)
* [Redux中间件](https://redux.js.org/advanced/middleware)
### 来源
1. [增量计数器示例引自Redux Thunk Documentation10/02/201](#https://github.com/reduxjs/redux-thunk)