如何解决我正在尝试创建用户名,密码和管理员选择器重置选项
我想要的是能够重设活动用户的用户名,密码并更改用户的管理员状态。但是,当我更改变量的状态时,更改后的状态不会添加到Patch请求中。我想我将范围缩小到我创建的onChange事件的问题。但是我不知道为什么我的状态没有被保存。
export default class UpdateUser extends Component{
constructor(props){
super(props);
this.onChangeuserName = this.onChangeuserName.bind(this);
this.onChangepassword = this.onChangepassword.bind(this);
this.onChangeAdminStatus = this.onChangeAdminStatus.bind(this)
this.updateUser = this.updateUser.bind(this);
this.deleteUser = this.deleteUser.bind(this);
this.state={
currentUser:{
id:null,userName: '',password: '',adminStatus:false
},message: '',};
}
componentDidMount()
{
this.getUser(this.props.match.params.id);
}
componentWillUnmount()
{
this.setState = (state,callback) =>
{
return;
};
}
onChangeuserName(e){
const userName = e.target.value;
this.setState(function (prevState)
{
return{
currentUser:
{
...prevState.currentUser,userName:userName
}
};
});
}
onChangepassword(e){
const password = e.target.value;
this.setState(function (prevState)
{
return{
currentUser:
{
...prevState.currentUser,password:password
}
};
});
}
onChangeAdminStatus(e)
{
const adminStatus = e.target.checked;
console.log(e.target.checked);
this.setState(function(prevState)
{
return{
currentUser:
{
...prevState.currentUser,adminStatus:adminStatus
}
};
});
}
getUser(id)
{
UserDataService.get(id)
.then(response=>
{
this.setState({
currentUser:response.data
});
console.log(response.data);
})
.catch(e=>
{
console.log(e);
});
}
updateUser()
{
UserDataService.update(
this.state.currentUser._id,this.state.currentUser
)
.then(response =>
{
console.log(response.data);
this.setState({
message: "The User's credentials were updated successfully!"
});
})
.catch(e=>
{
console.log(e);
});
}
deleteUser(){
UserDataService.delete(this.state.currentUser._id)
.then(response =>{
console.log(response.data);
this.props.history.push('/users');
})
.catch(e=>
{
console.log(e);
})
}
render()
{
const{currentUser} = this.state;
return(
<div className="col-md-6">
{currentUser ? (
<div className="edit-form">
<div className="d-flex justify-content-center">
</div>
<h4>Update User</h4>
<form>
<div className="form-row d-flex justify-content-center">
<div className="form-group col-md-6">
<label for="userName">Username: </label>
<input type="text" className="form-control" id="userName" value={currentUser.userName} onChange={this.onChangeuserName} placeholder="Username" name="userName"></input>
</div>
</div>
<div className="form-row d-flex justify-content-center">
<div class="form-group col-md-6">
<label for="password">Password: </label>
<input type="text" className="form-control" id="password" onChange={this.onChangepassword} placeholder="New Password" name="password"></input>
</div>
</div>
<div className="form-row d-flex justify-content-center">
<button onClick={this.updateUser} type="button" className="badge badge-success">Update</button>
<button onClick={this.deleteUser} type="button" className="badge badge-danger mr-2">Delete</button>
<input onChange={this.onChangeAdminStatus} className="AdminCheck" type="checkbox" id="AdminCheck" />
<label for="AdminCheck">Make User an Admin</label>
<div>
<br/>
<p>{this.state.message}</p>
</div>
</div>
</form>
</div>
) : (
<div>
<br/>
<p>Please click on a Tutorial...</p>
</div>
)}
</div>
)
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。