我在这个简单问题上有些困惑!我想要简单地获取我的表单数据,验证它,提交并发送一个Post请求到Express API。但在此之前,我觉得我没有彻底理解如何完成这个过程。我看了这个问题和这些以及其他一些问题,但我不确定这是最好的方法。
我认为应该这样做:
我为注册页面创建一个React组件。(为演示而简化)
class SignupForm extends Component {
constructor(props){
super(props);
this.onSubmit = this.onSubmit.bind(this);
}
onSubmit(val){
debugger;
}
render(){
return (
<form onSUbmit={ (e)=> this.onSubmit(e) }>
<input type="text" />
<label></label>
<button type="submit">Submit</button>
</form>
)
}
}
当按钮被点击时,OnSubmit() 函数将被触发,其中它将接收JSON数据。{
"name": "Kanye",
"surname": "West",
"email":"yeezy@goodmusic.com",
"password":"notsurehowthiswillwork"
}
我可以通过触发我的SignupAction()
来进行AJAX调用以更新我的reducers。当涉及使用像react-redux-form 或者redux-form这样的库时,困惑会加倍。我只想要一个为name
, surname
, email
和password
的模型或者类似的东西,但是我觉得这些库过于复杂,因为它们开始有了自己的reducer。const store = createStore(combineForms({
user: initialUser,
}));
我的另一个选择是:
class SignupForm extends Component {
constructor(props){
super(props);
this.state.form = {
name : '',
surname: '',
email: '',
password: ''
}
}
onSubmit(e){
e.preventDefault();
this.props.SignupAction(this.state.form);
// then reset the state agian to ''
}
}
但是,我的问题是......这会影响性能吗?如果会,为什么?