Reactjs 在react with redux中调度操作
我不知道怎样才能轻松解决这个问题。我有一个表单,用户可以填写并将内容发送到我的数据库。表单有自己的状态(例如这里的用户名)。我不知道在调度redux操作后如何调用setState(向用户提供有关结果的消息)。 函数postDataBegin很简单,只显示微调器。我认为存在问题,因为此函数是同步的(如果不是,请告诉我)。我怎样才能修好它?我应该学什么Reactjs 在react with redux中调度操作,reactjs,asynchronous,redux,redux-thunk,Reactjs,Asynchronous,Redux,Redux Thunk,我不知道怎样才能轻松解决这个问题。我有一个表单,用户可以填写并将内容发送到我的数据库。表单有自己的状态(例如这里的用户名)。我不知道在调度redux操作后如何调用setState(向用户提供有关结果的消息)。 函数postDataBegin很简单,只显示微调器。我认为存在问题,因为此函数是同步的(如果不是,请告诉我)。我怎样才能修好它?我应该学什么 submitForm = e => { e.preventDefault(); const { username } = this.s
submitForm = e => {
e.preventDefault();
const { username } = this.state;
if (!question) {
this.setState({ messageToUser: "Fill the form" });
} else {
// onSubmit is redux action from props
this.props.onSubmit(username);
//HOW TO CALL THIS FUNCTION AFTER FUNC ABOVE?
this.setState({ messageToUser: "Send" });
}
};
<Form type="submit" onSubmit={this.submitForm}/>
export const postUserQuestion = username => dispatch => {
dispatch(postDataBegin());
return post(postUsernameAPI, username)
.then(res => dispatch(postUsernameAPISucceeded(res)))
.catch(error => dispatch(postUsernameAPIFailure(error)));
};
submitForm=e=>{
e、 预防默认值();
const{username}=this.state;
如果(!问题){
this.setState({messageToUser:“填写表单”});
}否则{
//onSubmit是从道具中重做动作
this.props.onSubmit(用户名);
//如何在上面的FUNC之后调用此函数?
this.setState({messageToUser:“Send”});
}
};
导出常量postureQuestion=username=>dispatch=>{
调度(postDataBegin());
回帖(postUsernameAPI,用户名)
。然后(res=>dispatch(positionernameapisuccessed(res)))
.catch(错误=>dispatch(错误));
};
您的操作返回一个承诺,因此您可以对其使用然后
方法来提供回调:
submitForm = e => {
e.preventDefault();
const { username } = this.state;
if (!question) {
this.setState({ messageToUser: "Fill the form" });
} else {
// onSubmit is redux action from props
this.props.onSubmit(username).then(() => {
this.setState({ messageToUser: "Send" });
});
}
};
尽管可能最好也将这些数据存储在reducer上,并在提交表单的同时对其进行更改。Hi,您必须学习reducer。调用dispatch reducer后,reducer会更新或设置新状态,您可以将此状态用于存储。是的,我知道,但我不想将reducer存储仅用于简单的消息-我的表单已经是有状态组件了。没有别的办法了?问题是,我的消息在表单组件中显示后,我点击提交按钮-我想让它等待。太好了!我知道这都是关于承诺的。非常感谢。