简体   繁体   中英

how to make my textfield empty after button click

This is my function

 const clicker = (input) => {
            setoutput((prev) => {
                return [...prev, input];
            });
        };

I passed above function as a prop

<Createcard click={clicker} />

this is my component where im using that function

const Createcard = (props) => {
  const [input, setInput] = useState({
    titel: "",
    content: "",
  });
  const setInputs = (event) => {
    const { name, value } = event.target;
    setInput((prevData) => {
      return {
        ...prevData,
        [name]: value,
      };
    });
  };
  // here I am passing my state value in function

  const clik = () => {
    props.click(input);
  };
  return (
    <div>
      <Card className={classes.root}>
        <TextField
          id="standard-basic"
          name="titel"
          value={input.titel}
          label="Titel"
          onChange={setInputs}
          placeholder="set"
          type="text"
        />
        <TextField
          id="standard-basic1"
          name="content"
          value={input.content}
          label="Content"
          onChange={setInputs}
        />
        // here i want to make my textfield empty after button click
        <Button className={classes.wid} onClick={clik}>
          <AddCircleOutlineIcon />
        </Button>
      </Card>
    </div>
  );
};

You can try to do something like this

const clik = () => {
  props.click(input);
  setInput({ titel: "", content: "" });
};

Just set the state properties to their initial value on your clik method

 setInput({ titel: "", content: "" });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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