AnswerBun.com

preventDefault not working when form submit react js

Stack Overflow Asked by gihankumara on December 17, 2020

i am trying to alert my form data using form submit function. after submit, I need to prevent reload. please check below code.

this is my render method

    render() {
    return (
        <div>
            <form onSubmit={() => this.formSubmitHandler(this.state.username, this.state.password, this.state.logtype )}>
            <input type="text" placeholder="Username" value={this.state.username} onChange={this.usernameHandler} />
            <input type="password" placeholder="Password" value={this.state.password} onChange={this.passwordHandler} />
            <select value={this.state.logtype} onChange={this.typeHandler}>
                <option value="Primary"> Primary</option>
                <option value="Secondary"> Secondary</option>
            </select>
            <button>Submit</button>
            </form>
        </div>
    );
}

this is my submit function

    formSubmitHandler = (a,b,c) => {
 alert(`${a} + ${b} + ${c}`);  
 event.preventDefault();
}

this function works well and alert also working well. but event.preventDefault() not working.

error said "Unexpected use of ‘event’ no-restricted-globals". how i fix this

2 Answers

In your on submit method pass event as argument

onSubmit={(event) => this.formSubmitHandler(event, this.state.username, this.state.password, this.state.logtype )}

and get from formSubmitHandler get event as a parameter

formSubmitHandler = (event, a,b,c) => {
  event.preventDefault();
 alert(`${a} + ${b} + ${c}`);  
}

Correct answer by Rajitha Udayanga on December 17, 2020

No need to pass the state to this.formSubmitHandler because you can access it inside the function itself.

import React from "react";

export default class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = { username: "", password: "", logtype: "" };

    this.usernameHandler = this.usernameHandler.bind(this);
    this.passwordHandler = this.passwordHandler.bind(this);
    this.typeHandler = this.typeHandler.bind(this);
    this.formSubmitHandler = this.formSubmitHandler.bind(this);
  }

  usernameHandler() {
    // your code
  }

  passwordHandler() {
    // your code
  }

  typeHandler() {
    // your code
  }

  formSubmitHandler(e) {
    e.preventDefault();
    console.log(this.state);
  }

  render() {
    return (
      <div>
        <form onSubmit={this.formSubmitHandler}>
          <input
            type="text"
            placeholder="Username"
            value={this.state.username}
            onChange={this.usernameHandler}
          />
          <input
            type="password"
            placeholder="Password"
            value={this.state.password}
            onChange={this.passwordHandler}
          />
          <select value={this.state.logtype} onChange={this.typeHandler}>
            <option value="Primary"> Primary</option>
            <option value="Secondary"> Secondary</option>
          </select>
          <button>Submit</button>
        </form>
      </div>
    );
  }
}

Answered by ridoansaleh on December 17, 2020

Add your own answers!

Related Questions

Remove web-inspector-hide-shortcut

0  Asked on January 9, 2021 by dumi-padureanu

   

Pandas – Lambda inside apply to return a row

1  Asked on January 9, 2021 by marcio

         

Unclear webhook eventNotification documentation

1  Asked on January 9, 2021 by nolan-aguirre

 

c – Access not within mapped region error

0  Asked on January 9, 2021 by rhcp_lean

     

How to reduce committed memory usage in Java

1  Asked on January 9, 2021 by simon-hu

   

mutool / mulib edit text (with a c programm)

1  Asked on January 9, 2021 by atticus

   

Running browsermob with selenium grid on docker

2  Asked on January 8, 2021 by eduardoreynoso

         

How do I exclude a repeated click event?

2  Asked on January 8, 2021 by jessy

   

handling promises using await and async

2  Asked on January 8, 2021 by jass-preet

   

How to compare data from the same column in a dataframe (Pandas)

4  Asked on January 8, 2021 by abraham-arreola

       

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP