【发布时间】:2019-03-02 18:25:03
【问题描述】:
我目前正在使用 redux / redux-thunk 像这样使用 api-sauce 获取用户
let authToken = await AsyncStorage.getItem('@TSQ:auth_token')
if (authToken) {
store.dispatch(fetchUser(authToken))
console.log('show login screen')
// dont worry, if the token is invalid, just send us to onboarding (api determines this)
loggedInView()
} else {
Onboarding ()
}
....
export const fetchUser = authToken => async dispatch => {
console.log('dispatching auth token')
console.log('here goes request')
let res = await api.get(`/auth/${authToken}`);
if (res.ok) {
console.log('have the user')
dispatch(
setUser(res.data)
)
} else {
dispatch({
type: 'SET_USER_DEFAULT'
})
}
}
运行此代码时,用户仍在加载,console.logs 不按顺序
`dispatching auth token`
`here goes request`
`show login screen`
为什么会这样?
【问题讨论】:
标签: redux react-redux axios redux-thunk