Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

367
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!