我试图在使用 Axios 创建表单提交后清除表单数据。消息处理良好,响应记录到页面,但每个文本字段中的数据在提交后仍保留在页面上。
我尝试添加一个resetForm
函数,我将表单设置回原来的空白状态,但这不起作用。
import React, { Component } from 'react';
import { Grid, Cell, Textfield, Button } from 'react-mdl';
import './Contact.css';
import axios from "axios";
class Contact extends Component {
constructor(props){
super(props);
this.state = {fullName: "", email: "", message: ""};
}
resetForm = () => {
this.baseState = this.state
this.setState(this.baseState)
}
handleForm = e => {
axios.post(
"https://formcarry.com/s/axiosID",
this.state,
{headers: {"Accept": "application/json"}}
)
.then(function (response) {
let successMessage = document.querySelector('.success-message');
successMessage.innerHTML = JSON.stringify(response.data.title);
})
.catch(function (error) {
let successMessage = document.querySelector('.success-message');
successMessage.innerHTML = JSON.stringify(error);
});
e.preventDefault();
}
handleFields = e => this.setState({ [e.target.name]: 'e.target.value' });
render() {
return (
<Grid>
<Cell col={6}>
<h2>Contact Me</h2>
<hr/>
<div style={{ width: '100%' }} className="contact-list">
<form onSubmit={this.handleForm}>
<Cell col={12}>
<Textfield type="text" id="fullName" name="fullName" className="full-name"
onChange={this.handleFields}
label="Full name"
floatingLabel
style={{width: '200px'}}
/>
</Cell>
<Cell col={12}>
{/* Textfield with floating label */}
<Textfield type="email" id="email" name="email" className="email-address"
onChange={this.handleFields}
label="Email address"
floatingLabel
style={{width: '200px'}}
/>
</Cell>
<Cell col={12}>
{/* Floating Multiline Textfield */}
<Textfield name="message" id="message" className="text-body"
onChange={this.handleFields}
label="Your message..."
rows={10}
style={{width: '400px'}}
/>
</Cell>
<Button raised accent ripple type="submit" onClick={this.resetForm}>Send</Button>
<div className="success-message">
<label></label>
</div>
</form>
</div>
</Cell>
</Grid>
)
}
}
export default Contact;
我真正想要的是文本字段fullName, email and message
提交表单后清除,但数据保留在页面上。
您不需要resetForm
函数(完全摆脱它),只需在handleForm中设置你的状态,如下所示:
更新:您还需要向每个输入添加 value 属性,使其成为受控输入,如下所示:
import React, { Component } from 'react';
import { Grid, Cell, Textfield, Button } from 'react-mdl';
import './Contact.css';
import axios from "axios";
class Contact extends Component {
constructor(props) {
super(props);
this.state = { fullName: "", email: "", message: "" };
}
handleForm = e => {
axios.post(
"https://formcarry.com/s/axiosID",
this.state,
{ headers: { "Accept": "application/json" } }
)
.then(function (response) {
let successMessage = document.querySelector('.success-message');
successMessage.innerHTML = JSON.stringify(response.data.title);
})
.catch(function (error) {
let successMessage = document.querySelector('.success-message');
successMessage.innerHTML = JSON.stringify(error);
});
e.preventDefault();
this.setState({fullName: '', email: '', message: ''}) // <= here
}
handleFields = e => this.setState({ [e.target.name]: e.target.value });
render() {
return (
<Grid>
<Cell col={6}>
<h2>Contact Me</h2>
<hr />
<div style={{ width: '100%' }} className="contact-list">
<form onSubmit={this.handleForm}>
<Cell col={12}>
<Textfield type="text" id="fullName" name="fullName" className="full-name"
onChange={this.handleFields}
value={this.state.fullName} // <= here
label="Full name"
floatingLabel
style={{ width: '200px' }}
/>
</Cell>
<Cell col={12}>
{/* Textfield with floating label */}
<Textfield type="email" id="email" name="email" className="email-address"
onChange={this.handleFields}
value={this.state.email} // <= here
label="Email address"
floatingLabel
style={{ width: '200px' }}
/>
</Cell>
<Cell col={12}>
{/* Floating Multiline Textfield */}
<Textfield name="message" id="message" className="text-body"
onChange={this.handleFields}
value={this.state.message} // <= here
label="Your message..."
rows={10}
style={{ width: '400px' }}
/>
</Cell>
<Button raised accent ripple type="submit">Send</Button>
<div className="success-message">
<label></label>
</div>
</form>
</div>
</Cell>
</Grid>
)
}
}
export default Contact;
作为旁注:查看 React refs 以获取 dom 元素...在此处阅读更多内容:https://reactjs.org/docs/refs-and-the-dom.html https://reactjs.org/docs/refs-and-the-dom.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)