Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

281
Vistas
Why does my submit button not work on first submit?

I want to authenticate the user and redirect them to the home page after. When I submit this form redux state is being set, but the user is not redirected to the home page on first submit; I have to submit twice or sometimes three times for it to properly redirect me to the home page.

Any ideas as to what I may be doing wrong?

async function login(e) {
  e.preventDefault()
  const response = await axios.post("http://localhost:3001/login", {
    email,
    password
  }, {
    withCredentials: true
  })
  if (response.data.auth) {
    history.push("/")
    store.dispatch({
      type: "setUser",
      payload: response.data
    })
  }
}

Here is my code for the login form.

<form className="login_form" onSubmit={(e)=> login(e)}>
  <label>Email</label>
  <input type="text" onChange={e=> setEmail(e.target.value)} required placeholder="enter your email"/>
  <label>Password</label>
  <input type="password" onChange={e=> setPassword(e.target.value)} required placeholder="enter your password"/>
  <div className="login_btn_div">
    <button className="route_to_register_btn" onClick={()=> history.push("/register")} type="button">Create account</button>
    <button className="login_submit_btn" type="submit">Sign in</button>
  </div>
</form>

Here I initialize history in its own file and pass it to the root component as a prop.

const createHistory = require("history").createBrowserHistory 
export default createHistory();
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda