Reactjs 在redux中发送后,如何返回操作结果?
我想使用已分派的操作结果并分派新操作。 这里有一个我想使用的代码示例Reactjs 在redux中发送后,如何返回操作结果?,reactjs,redux,react-redux,Reactjs,Redux,React Redux,我想使用已分派的操作结果并分派新操作。 这里有一个我想使用的代码示例 function mapDispatchToProps(dispatch) { return bindActionCreators({ addApplication(data, vacancy_id) { dispatch(candidateActions.addCandidate(data)) .then((newCandidate) =>
function mapDispatchToProps(dispatch) {
return bindActionCreators({
addApplication(data, vacancy_id) {
dispatch(candidateActions.addCandidate(data))
.then((newCandidate) => {
dispatch(applicationActions.addApplication(data.video, vacancy_id, newCandidate._id))
});
}
}, dispatch);
}
我如何在redux中做到这一点?我可以使用哪些库?您应该使用
thunk中间件
,该中间件将负责根据以前操作的结果分派操作。通过使用thunk中间件,动作创建者可以返回一个能够分派自己动作的函数
假设我有3个动作:
1. loginRequest - send credentials for login
2. loginSuccess - dispatch action if credentials are valid
3. loginFailure - dispatch action if login failed
我可以设置一个函数来执行以下操作
export function login(credentials){
return function(dispatch){
dispatch(loginRequest(credentials){
return fetch(`http://localhost:8000/login`)
.then(response => response.json())
.then(json =>
dispatch(loginSuccess(creds))
.catch(err =>
dispatch(loginFailure(err))
})
}
}
然后,您可以通过将登录功能包含在mapDispatchToProps
你也许应该去看看——为了更好地理解