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

2.2 KiB
Raw Blame History

title localeTitle
Redux Thunk Redux Thunk

Redux Thunk

Redux Thunk是一个中间件允许您在Redux 1中返回函数而不仅仅是动作。这允许延迟的行动包括使用承诺。

我们使用这个中间件的原因是因为并非所有我们执行的操作都是同步的而某些操作必然是非同步的比如使用axios发送get请求。这将需要一些时间简单的redux不会考虑到这种行为。因此Redux-thunk通过允许我们异步调度动作来解决问题这样我们就可以让这些承诺得到解决。

例1

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

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 --saveyarn add redux-thunk安装Redux Thunk。因为它是Redux工具所以您还需要设置Redux。安装后使用applyMiddleware()启用它:

import { createStore, applyMiddleware } from 'redux'; 
 import thunk from 'redux-thunk'; 
 import rootReducer from './reducers/index'; 
 
 const store = createStore( 
  rootReducer, 
  applyMiddleware(thunk) 
 ); 

参考

来源

  1. 增量计数器示例引自Redux Thunk Documentation10/02/201