reactjs - 如何使用映射处理 formChange
问题描述
我的作业有问题,我需要编辑该字段,但该字段不可编辑,因为 formData 中的值!== 名称,并且映射成功,但在编辑该字段时,该字段仍然不可编辑。我已经尝试了所有的可能性,但仍然没有为我工作
状态
constructor(props) {
super(props);
const token = localStorage.getItem("token");
let isLogedIn = true
if (token === null ) {
isLogedIn = false
}
this.state = {
post : [],
post2 : [],
filteredData: [],
clientId : '',
value : '',
formData : {
userName : '',
username : '',
firstName : '',
middleName : '',
lastName : '',
email : '',
phone : '',
expiredDate : '',
activeRoleName : '',
activeBranchName: ''
},
isUpdate : false,
isLogedIn,
currentPage: 1,
todosPerPage: 5,
isChecked : false,
isSearch : false,
modal : false,
startIndex : 0,
endIndex : 5
}
this.handleEdit = this.handleEdit.bind(this);
this.handleClick = this.handleClick.bind(this);
}
句柄表格
handleForm = (event) => {
let formDataNew = {...this.state.formData};
formDataNew[event.target.name] = event.target.defaultValue;
this.setState ({
formData : formDataNew,
})
}
映射
{(this.state.post2.map((h,i) => {
if ( `${h.username}` === `${this.state.formData.username}`) {
return (<FormGroup>
<FormGroup>
<Label htmlFor="middleName">Middle Name</Label>
<Input onChange={this.handleForm} key = {i} defaultValue= {h.middleName} type="text" name = "middleName" className="form-control" placeholder="Middle Name" required="" />
</FormGroup>
<FormGroup>
<Label htmlFor="lastName">Last Name</Label>
<Input onChange={this.handleForm} key = {i} defaultValue= {h.lastName} type="text" name = "lastName" className="form-control" placeholder="Last Name" required="" />
</FormGroup>
<FormGroup>
<Label htmlFor="email">Email</Label>
<Input onChange={this.handleForm} key = {i} defaultValue= {h.email} type="text" name = "email" className="form-control" placeholder="Email" required="" />
</FormGroup>
<FormGroup>
<Label htmlFor="phone">Phone Number</Label>
<Input onChange={this.handleForm} defaultValue={h.phone} type="text" name = "phone" className="form-control" placeholder="Phone Number" required="" />
</FormGroup>
<FormGroup>
<Label for="expiredDate">Expired Date</Label>
<Input
type="date"
name="expiredDate"
onChange = {this.handleForm}
defaulValue = {moment(this.state.formData.expiredDate).format("YYYY-MM-DD")}
placeholder="date placeholder"
min = {moment().format("YYYY-MM-DD")}/>
</FormGroup>
</FormGroup>
)
}} ))}
post2内部
getPostAPI2 = (data, username) => {
const token = localStorage.getItem('token');
const { post } = this.state
axios.post(`http://53.20.11.10:33/users/get`, { username } ,
{
headers:
{
"Content-Type" : "application/json",
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36",
"AUTHENTICATION-KEY" : token,
}
})
.then((res)=>{
let response = res;
if(response){
this.setState ((prevState) => ({
post2 : [response.data.response],
modal: !prevState.modal,
formData : data,
formData : {
...data,
username : username
}
}));
}
},this)
.catch( (error) => {
if (error) {
error.response.data.errors.map(e => {
alert(e.errorMessage)
this.setState({
formData : {
username : ''
}
})
})
}
}
)
}
我应该在代码中更改什么才能更改字段?
解决方案
推荐阅读
- java - 随机存取字符串数组
- mongodb - MongoDB:实现TTL后,不再有权限查询DB
- java - 如何将整个视图(水平和垂直滚动)保存为图像或 pdf?
- javascript - Vue:vuetify 如何在不导入的情况下使其组件可用?
- javascript - 最多 3 个整数和 2 个小数的正则表达式
- javascript - 多次运行 Chromeless headless
- coupon - 如何将“添加到钱包”按钮添加到网站以将优惠券添加到 Apple Wallet 和 Google Pay 帐户
- python - Python - google-cloud 库 - Google Cloud Shell 中的错误
- forms - Xamarin Forms:GetAsync 在 SearchBar 事件处理程序中不起作用
- tsql - TSQL中不同手动输入的全名之间的模糊匹配算法?