简体   繁体   English

从按钮点击重置

[英]Reset from on button click

I am trying to reset values on the button click but not able to do so.我试图在单击按钮时重置值,但无法这样做。 Don't know what the issue is.不知道是什么问题。

This is my one of the field from form:这是我的表单字段之一:

       <Grid item xs={5} md={5} lg={5}>
          <InputLabel htmlFor="outlined-adornment-fName" required>
            First Name
          </InputLabel>
          <FormControl
            className={classes.formControl}
            variant="outlined"
            size="small"
          >
            <OutlinedInput
              id="outlined-adornment-fName"
              placeholder="First Name"
              defaultValue={firstName}
            />
          </FormControl>
        </Grid>

This is how i am setting form values:这就是我设置表单值的方式:

const [firstName, setFirstName] = React.useState('M');

This is my function for Reset:这是我的重置功能:

const handleClick = () => {
    setTimeout(() => {
      setFirstName('');
    }, 1000);
  };

This is my Reset button:这是我的重置按钮:

            <Button
              variant="contained"
              color="primary"
              onClick={handleClick}
              classes={{ root: classes.textTran }}
              style={{
                display: 'flex',
                width: '35%',
                textTransform: 'none',
                borderRadius: 20,
              }}
            >
              Reset
         </Button>
const handleClick = () => {
    setTimeout(() => {
      setFirstName(() => "");
    }, 1000);
  };

Give this a try试试这个

You have only mentioned defaultValue to OutlinedInput .您只提到了defaultValueOutlinedInput Add value and onChange properties so that the new model change will be reflected.添加valueonChange属性,以便反映新的模型更改。 You can remove setTimeout function.您可以删除setTimeout函数。

 const handleClick = () => { setFirstName(''); }

 <OutlinedInput id="outlined-adornment-fName" placeholder="First Name" value = { firstName } onChange={e => setFirstName(e.target.value)} />

I believe the issue here is that you set the default value of the intput to the state variable, but during runtime the input field doesn't track the state.我相信这里的问题是您将输入的默认值设置为状态变量,但在运行时输入字段不跟踪状态。 Value of the input has to be set to the corresponding state variable and an onChange-call has to be setup on order to update that specific variable.输入的值必须设置为相应的状态变量,并且必须设置 onChange-call 以更新该特定变量。 Look at the primitve example below,看下面的原始例子,

const DemoForm = () => {
  const [value, setValue] = React.useState('')

  const handleChange = (event) => setValue(event.target.value)  
  const handleReset = () => setValue('')

  return(
    <>
      <form>
        <input value={value} onChange={handleChange} />
      </form>
      <button onClick={handleReset} >Reset</button>
    </>
}

This will cause the state to update on each input change and re-render the input with the updated value, event when the reset button is clicked since it changes value as well.这将导致状态在每次输入更改时更新,并使用更新后的值重新渲染输入,当重置按钮被单击时事件,因为它也会更改值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM