[英]On click of pop up doesn't clear the state
So I have a popup that has some radio button and input fields. 因此,我有一个弹出窗口,其中包含一些单选按钮和输入字段。 There's a handleClick
function on the OK
button that does some work with the updated state and closes the popup. “ OK
按钮上有一个handleClick
函数,该函数可以处理更新后的状态并关闭弹出窗口。 I'm trying to clear the state properties once these operations are performed and the pop up closes. 一旦执行了这些操作并且弹出窗口关闭,我试图清除状态属性。
handleClick = () => {
if(this.state.val === "1") {
//do something
} else if (this.state.val === "2") {
//do something
}
//once if conditions are performed, clear the state
this.setState({val: "0"}); //this is what i do right now
}
Doing setState
isn't updating the state when I reopen the pop up. 当我重新打开弹出窗口时,执行setState
不会更新状态。
OK, try this add setstate, in the end, change the state of inputs/radio buttons to empty. 好的,尝试此添加setstate,最后将输入/单选按钮的状态更改为空。
handleClick = () => {
if(this.state.val === "1") {
//do something
} else if (this.state.val === "2") {
//do something
}
//once if conditions are performed, clear the state
this.setState({val: "" or null }); //keep it empty or null
}
and also the radio input add checked method and pass the state to it so that if it null/empty it will be unchecked 以及无线电输入添加选中的方法并将状态传递给它,以便如果它为null / empty则将不选中
<input type="radio" checked={this.state.val} />
Or 2nd method you can use reset() method. 或第二种方法,您可以使用reset()方法。
add a ref to your form element like this 像这样向您的表单元素添加一个引用
<form ref={(input) => this.resetForm = input}></form>
handleClick = () => {
if(this.state.val === "1") {
//do something
} else if (this.state.val === "2") {
//do something
}
//Use the ref to target the form and apply reset method to it
this.resetForm.reset();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.