[英]React Js, onChange {setState} event not working
I'm working my practice app using ReactJs and Django.我正在使用 ReactJs 和 Django 工作我的练习应用程序。 My problem is when I update the Data via Axios "onChange" event not working.
我的问题是当我通过 Axios“onChange”事件更新数据时不起作用。
updateForm.js更新表单.js
class Contact extends Component {
constructor(props) {
super(props);
this.state = {
Name: '',
Contact: ''
};
this.handleChange = this.handleChange.bind(this);
this.handleformsubmit = this.handleformsubmit.bind(this);
}
handleformsubmit = (event, requestType, id) => {
const Name = event.target.elements.Name.value;
const Contact = event.target.elements.Contact.value;
switch (requestType) {
case 'put':
return axios.put(`http://127.0.0.1:8000/Contact/${id}/`, {
Name: Name,
Contact: Contact
})
.then(res => console.log(res))
.catch(error => console.log(error));
}
}
handleChange(event) {
this.setState({ Name: event.target.value });
this.setState({ Contact: event.target.value });
}
render() {
const Name = this.state.Item_no;
const Contact = this.state.Supplier;
return (
<Form onSubmit={(event) => this.handleformsubmit(
event,
this.props.requestType,
this.props.id)}>
<div className="container">
<GridContainer component="span">
<Card>
<CardHeader color="primary">
<h4>Update Contact Info</h4>
</CardHeader>
<CardBody>
<GridContainer>
<GridItem sm={12} md={6}>
<CustomInput
labelText="Name"
type="text"
id="Name"
name="Name"
formControlProps={{
fullWidth: true
}}
value={Name}
onChange={this.handleChange}
/>
</GridItem>
<GridItem sm={12} md={6}>
<CustomInput
labelText="Contact"
name="Contact"
type="text"
id="Contact"
formControlProps={{
fullWidth: true
}}
value={Contact}
onChange={this.handleChange}
/>
</GridItem>
I already done searching and tried the possible solution's but nothing works.我已经完成了搜索并尝试了可能的解决方案,但没有任何效果。 Nothing wrong with "put" actions but on my onChange event not trigger when I update the data.
“放置”操作没有任何问题,但在我更新数据时不会触发我的 onChange 事件。 Help me how to work this.Thank you!
帮我解决这个问题。谢谢!
从您的代码中,我可以看到onChange
函数只是更新新状态,并且只有在您提交表单之前才会调用函数handleformsubmit
It looks like your input value is controlled, which refuses any changes from handleChange看起来您的输入值受到控制,拒绝来自 handleChange 的任何更改
Try value={this.state.Name}尝试值={this.state.Name}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.