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

126
Views
Submit button reloads the page

I am working on a login form, and I am getting a strange behavior whereby whenever I click on the 'Login' button, the page simply reloads, without logging in my email and password through the 'submitHandler' function. Below is a code of the form (I removed a lot of extra CSS formatting and div's). Here is a screenshot of the form:

loginForm

import React from 'react'
import { useState, useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { login } from "../../../actions/userActions";
import { Form, Button, Row, Col } from "react-bootstrap";

function LoginForm ({history}) {

    const [email, setEmail] = useState("");
    const [password, setPassword] = useState("");

    const submitHandler = (e) => {
      e.preventDefault();
      console.log(email, password)
      // dispatch(login(email, password));
    };

   
    return (
       <form>              
              <button onSubmit={submitHandler} type="submit">
            Login
          </button>              
              </form>
 
    )
}

export default LoginForm

Does anyone have idea why the page reloads after clicking on the login button?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Issue

The button is of type="submit" but the form has no onSubmit handler and is thus taking the default form action, i.e. submitting the form and reloading the page.

Solution

Move the onSubmit to the form element so the callback can prevent the default form action from occurring.

<form onSubmit={submitHandler}>              
  <button type="submit">
    Login
  </button>              
</form>
about 4 years ago · Juan Pablo Isaza Report

0

You can also convert button type="" method provider submit to button, in some cases you don't really want to submit internal form so just keep in mind.

Here is the example,

<form>    
  <button onSubmit={submitHandler} type="button">
</form>

Other answer is also OK, but I would like to approach from a different angle.

about 4 years ago · Juan Pablo Isaza Report

0

Try calling the submitHandler with onSubmit on the form instead of on the button.

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!