我正在使用reactjs
registerMessage与用户对象无关,但我不得不将其置于状态以显示在render()中。发布时,我不想发布this.state,而是只希望属于用户的属性。
如何在没有registerMessage属性的情况下从this.state创建用户对象?
constructor() {
super();
this.state = {
username: '', //user
password: '', //user
name: '', //user
email: '', //user
admin: false, //user
registerMessage: '' // NOT USER
}
}
componentDidMount() { }
handleChange = (evt) => {
this.setState({
...this.state,
[evt.target.name]: evt.target.value
});
}
handleSubmit = (evt) => {
evt.preventDefault();
axios.post(global.apiBaseURL + "/user", this.state).then(response => {
this.setState({registerMessage: 'You were sucessfully registered. Please wait for the admin verification.'});
console.log(this.state);
})
.catch(error => {
console.error("Could not register");
});
}
html示例
<input type="text" name="username" value={this.state.name} onChange={this.handleChange} className="form-control" placeholder="Username" required/>