【发布时间】:2018-01-04 21:09:24
【问题描述】:
我正在使用带有 axios、thunk 和 promises 中间件的 react 和 redux 进行注册表单。首先,我想等待用户列表。然后我想检查是否存在具有该登录名和电子邮件的用户,如果不是发布用户。我在等待 api 获取完成时遇到问题,现在真的不知道如何链接它。
动作
export function fetchUsers(){
return function(dispatch){
dispatch({type:"FETCH_USERS"});
axios.get(url)
.then((response) => {
dispatch({type:"FETCH_USERS_FULLIFILED", payload: response.data});
})
.catch((err) => {
dispatch({type:"FETCH_USERS_ERROR", payload: err});
})
}
}
export function postUser(body){
return function(dispatch){
dispatch({type:"POST_USER"});
axios.post(url, body)
.then((response) => {
dispatch({type:"POST_USER_FULLFILED", payload: response.data});
})
.catch((err)=>{
dispatch({type:"POST_USER_ERROR", payload: err})
})
}
}
我想获取用户列表并检查用户何时单击提交按钮。我不能这样做,因为没有then() 方法
this.props.dispatch( fetchUsers()).then(()=>{
//checking my conditions
// if all is ok
this.props.dispatch(postUser(body))
})
【问题讨论】:
标签: javascript reactjs redux react-redux