reactjs - 提交 React JS 后如何删除文本区域内的文本
问题描述
提交后无法删除文本区域内的文本
我试过 setState() 但它不会删除它只删除值的文本
handleSubmit(evt){
evt.preventDefault();
console.log(this.state);
// const data = {
// email:this.state.email,
// subject:this.state.subject,
// body:this.state.body,
// }
const url = 'http://localhost:5000/api/nodemailer';
axios.post( url, this.state)
.then((res)=> {
console.log(res);
this.setState({email:'',subject:'',body:''});
})
.catch(err => {console.log('not sent'+err)});
`
<div className="row">
<div className="input-field col s12">
<i className="material-icons prefix">mode_edit</i>
<textarea id={this.props.name} className="materialize-textarea" name={this.props.name} value={this.props.body} onChange={this.props.onChange} placeholder={this.props.body} ></textarea>
<label htmlFor={this.props.name}>{this.props.title}</label>
</div>
</div>
`
预期的结果是提交后看到字段值为空但我得到的结果是文本区域仍然有文本但值为空
解决方案
当你提交时,你改变了状态this.setState({email:'',subject:'',body:''});
但是,textarea 没有,value= this.state.body
而是有value={this.props.body}
所以试试这个:
<div className="row">
<div className="input-field col s12">
<i className="material-icons prefix">mode_edit</i>
<textarea id={this.props.name} className="materialize-textarea" name={this.props.name} value={this.state.bodyState} onChange={this.props.onChange} placeholder={this.state.bodyState} ></textarea>
<label htmlFor={this.props.name}>{this.props.title}</label>
</div>
</div>
this.setState({bodyState = this.props.value)};
const url = 'http://localhost:5000/api/nodemailer';
axios.post( url, this.state)
.then((res)=> {
console.log(res);
this.setState({bodyState :''});
})
推荐阅读
- keras - 任何人都可以帮助我使用 keras 库实现混合嵌入模型,如附图所示
- html - 倾斜的两列布局不是全角
- macos - 链接器在使用 PCL 时抱怨缺少 vtkCommon
- java - org.hibernate.PropertyAccessException: IllegalArgumentException 发生调用 com.dms.model.Group.groupId 的 getter
- vba - VBA自动过滤基于数组的动态日期
- c# - 来自新项目的文件出现 404 错误
- sql - 向 Power BI 授予 Azure SQL 权限的最佳方式?
- percentage - 使用GDAL和python计算像素值的统计
- hash - 通用散列的预期界限
- ios - 图像缩放与设备缩放的工作原理