以下是我的商店创建,我在redux-thunk
功能
applyMiddleware
import { createStore, combineReducers, applyMiddleware } from 'redux'
// import { composeWithDevTools } from 'remote-redux-devtools'
import logger from 'redux-logger'
import thunk from 'redux-thunk'
import promise from 'redux-promise-middleware'
import tokenReducer from './reducers/tokenReducer'
const rootReducer = combineReducers({
tokenReducer
})
const middleware = applyMiddleware(logger, promise, thunk)
let store = createStore(rootReducer, middleware)
export default store
然后我将我的组件连接到商店和mapDispatchToProps
function mapDispatchToProps (dispatch) {
return {
getToken: () => dispatch(fetchTokenFromStorage())
}
}
export default connect(mapStateToProps, mapDispatchToProps)(SplashScreen)
这是我的函数代码fetchTokenFromStorage
export function fetchTokenFromStorage () {
return (dispatch) => {
dispatch(getToken())
AsyncStorage.getItem('AuthToken')
.then(function (token) {
dispatch(getTokenSuccess(token))
})
.catch(function () {
dispatch(getTokenFailure())
})
}
}
执行this.props.getToken()
后
在我的控制台中,操作如下所示:
行动ƒ(派遣){
调度(为gettoken());
_reactNative.AsyncStorage.getItem('AuthToken')。then(function(token){
调度(getTokenSuccess(令牌));
})。catch(function(){
派遣(得到......
这是我的函数fetchTokenFromStorage
代码。 React将我的函数转换为操作类型名称。为什么呢?
答案 0 :(得分:1)
thunk必须是applyMiddleware
const middleware = applyMiddleware(thunk, logger, promise)