Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

363
Visualizações
Facing event depreceated error in Javascript. Can anyone suggest what to do?

in my react code when I am trying to run the code it is showing the event is deprecated for the event.preventDefault(). Can anyone tell me what should I do?

import React, { useState } from "react";

function App() {
  const [task, setTask] = useState("");
  const [todo, setTodo] = useState([]);

  function createTodo() {
    event.preventDefault();
    setTodo((oldTodo) => {
      return [...oldTodo, task];
    });
  }

  return (
    <div>
      <h1>Todo App</h1>
      <form onSubmit={createTodo}>
        <input
          type="text"
          value={task}
          onChange={(e) => {
            setTask(e.target.value);
          }}
        />
        <button type="submit">Click me</button>
      </form>
      <ul>
        {todo.map((todos) => {
          return <li>{todos}</li>;
        })}
      </ul>
    </div>
  );
}

export default App;

ERROR in src\App.js Line 8:5: Unexpected use of 'event' no-restricted-globals

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You haven't made event an argument to createTodo, so it's being interpreted as a global, hence the warning.

about 4 years ago · Juan Pablo Isaza Relatório

0

You are using two kinds of events here. The non-deprecated one you used in this bit of code:

onChange={(e) => {
    // e is event!
    setTask(e.target.value);
}}

To clarify, the code above is really doing this:

onChange={(event) => {
    setTask(event.target.value);
}}

But since the event object is passed as argument you can name it anything you like in your function definition:

onChange={(foo) => {
    // foo is event!
    setTask(foo.target.value);
}}

However, for readability reasons it is best to give the event object an easily recognisable name such as event or e.

Now, since you already know how to use the non-deprecated event object all you need to do is do the same thing:

function createTodo(e) {
    e.preventDefault();
    setTodo((oldTodo) => {
        return [...oldTodo, task];
    });
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda