1

how to clear the input inside this code after submit with the button

export default function Form({addTask}) {
const [taskInp, setTaskInp] = useState("")
return (
    <div>
        <input
            type="text"
            placeholder="Write here"
            onChange={e=>{
                setTaskInp(e.target.value)
            }}
        />
        <button onClick={()=> {
            addTask(taskInp)
        }}>Add Task</button>
    </div>
)}

I would love to know what is the recommended way please

1

4 Answers 4

5

You can reset form elements when form submit.

  const handleSubmit = (event)=>{
    event.preventDefault();
    addTask(taskInp);
    event.target.reset();
  };

  return (
    <form onSubmit={handleSubmit}>
        <input
            type="text"
            placeholder="Write here"
            onChange={e=>{
              setTaskInp(e.target.value);
            }}
        />
        <button type="submit">Add Task</button>
    </form>
  );
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, Is there sorter way to do it like
This goes against the one way data flow paradigm encouraged by react. Ideally you should be reseting the component state and let react handle the re-render. That however means making the input a controlled component
You can also use Formik library. When onsubmit the form formik has the resetForm method. npmjs.com/package/formik
your code reset my lists every single submit
|
2
export default function Form({addTask}) {
const [taskInp, setTaskInp] = useState("")
return (
    <div>
        <input
            type="text"
            placeholder="Write here"
            value={taskInp}
            onChange={e=>{
                setTaskInp(e.target.value)
            }}
        />
        <button onClick={()=> {
            addTask(taskInp)
            setTaskInp("")
        }}>Add Task</button>
    </div>
)}

Comments

0
import React,{useRef} from "react";                               
                                                  
export default function Form(){                                     
 const formRef = useRef(null)
 handleForm = async(event)=>{
  //handle form logic
  ...
  formRef.current.reset();                 
 }
return (
  <>
   <form onSubmit={handleForm}>
    ....
   </form>                                            
  </>                                                 
)
}

Building on msfers answer and refining it for typescript users, this solution worked for me.

Comments

-2

normaly i do is just resetting the state after the onClick

  <button onClick={()=> {
      addTask(taskInp),
      setTaskInp("")
  }}>Add Task</button>

2 Comments

thanks, this way are not working
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.