Answers for "preventdefault not working react"

0

e.preventdefault is not a function react

You need to pass the event object to handleDelete function when you make use of Arrow function as done in your implementation.

You can think of an arrow function like a function that calls another function to which you need to pass the arguments. Event object is a parameter to the arrow function and you indeed need to pass this on to the handleDelete function

onClick={(e) => this.handleDelete(e, i)}
However after this change you still need to bind the deleteTodos function in the parent, since the context of this inside this function won't be that of the React class component, you can do it like

deleteTodos = (i) =>  {
   var lists = this.state.listArr;
   lists.splice(i, 1);
   this.setState({listArr: lists})
 }
or

constructor(props){
    super(props);
    this.state = {
       listArr: [],
    }
    this.deleteTodos = this.deleteTodos.bind(this);
  }
Posted by: Guest on March-09-2021
0

preventdefault not working react

//think if you are adding your submit handler function in the submit button
//or into the form itself
const handler = () => {
  e.preventDefault();
}
<form onSubmit={handler}> //good
<button type="submit" onSubmit={handler}> // bad
Posted by: Guest on October-15-2021

Code answers related to "preventdefault not working react"

Code answers related to "Javascript"

Browse Popular Code Answers by Language