Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

79
Vistas
Input form to clear once submitted using React

I created a simple to do list wherein the add and delete works. My problem is, after submitting a todo, the input box won't clear.

This is my app.js

const App = () => {
  const [toDoList, setToDoList] = useState([]);
  const [toDo, setToDo] = useState("");

  const handleToDo = (event) => {
    const { value } = event.target;
    setToDo(value);
  };

  const submit = () => {
    //const list = toDoList;
    const newItem = {
      id: 1 + Math.random(),
      value: toDo,
    };
    if (newItem.value && !toDoList.includes(newItem.value)) {
      toDoList.push(newItem);
      setToDoList([...toDoList]);
    }
  };

  return (
      <div className="container">
          Add an Item
          <br />
          <input
            type="text"
            placeholder="Type item here..."
            onChange={handleToDo}
          />
          <button className="add-btn btn-floating" onClick={submit}>
            <i class="material-icons"> + </i>
          </button>
          <br />
          <ul>
            ...display of todos here
          </ul>
        </div>
  );
  //}
};

export default App;

I'm confused as to where I should insert the useState so that the input would be reset.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The value of the input box must also be governed by the state. So the input should be like:

<input
   value={toDo}
   type="text"
   placeholder="Type item here..."
   onChange={handleToDo}
/>

Once You click on submit, reset the toDo to empty

const submit = () => {
//const list = toDoList;
const newItem = {
  id: 1 + Math.random(),
  value: toDo,
};
if (newItem.value && !toDoList.includes(newItem.value)) {
  toDoList.push(newItem);
  setToDoList([...toDoList]);
}
setToDo("");
};
about 4 years ago · Juan Pablo Isaza Denunciar

0

Your input seems to be half-controlled. You should also give the input a value property as such:

<input
 type="text"
 placeholder="Type item here..."
 onChange={handleToDo}
 value={toDo}
/>

And now you can clear out the input when a task is submitted:

const submit = () => {
    //rest of the code here...
    setToDo('') //this'll clear out the value of your input
 };
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can add a value to input element. And after submit, set it to an empty string. So:

const App = () => {
  const [toDoList, setToDoList] = useState([]);
  const [toDo, setToDo] = useState("");

  const handleToDo = (event) => {
    const { value } = event.target;
    setToDo(value);
  };

  const submit = () => {
    //const list = toDoList;
    const newItem = {
      id: 1 + Math.random(),
      value: toDo,
    };
    if (newItem.value && !toDoList.includes(newItem.value)) {
      toDoList.push(newItem);
      setToDoList([...toDoList]);
      setToDo("");
    }
  };

  return (
      <div className="container">
          Add an Item
          <br />
          <input
            type="text"
            value={toDo}
            placeholder="Type item here..."
            onChange={handleToDo}
          />
          <button className="add-btn btn-floating" onClick={submit}>
            <i class="material-icons"> + </i>
          </button>
          <br />
          <ul>
            {toDoList.map((todoLi) => (
              <li>{todoLi}</li>
            ))}
          </ul>
        </div>
  );
  //}
};

export default App;
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda