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

351
Vistas
onSubmit not working on enter in render props (React)

I need a button inside a form which triggers the onSubmit event when user presses the Enter button.

This is the example of a working solution:

<form onSubmit={() => console.log('ok')}>
  <button type="submit" autoFocus>Submit</button>
</form>

When the user presses Enter, onSubmit is properly triggered.

My problem is when I have to render this button in render props of a component:

<Component render={()=> (
  <button type="submit" autoFocus /> // here the button will not be focused
)}/>

So when I press Enter, nothing happens.

Is there any way to get a working solution using only html attributes?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It is not working because in your example the button onClick event is not connected to the GoogleLogin or the wrapping Component.

Step by step:

Because of the autoFocus, when your button is rendered it is automatically focused. As soon as you click enter while it is still focused the click event of that button is fired.

In the pure html example the onSubmit of the form is triggered because the button has a type="submit".

<form onSubmit={() => console.log('ok')}>
  <button type="submit" autoFocus>Submit</button>
</form>

So if you want your example to trigger the GoogleLogin on click of enter, you have to trigger it manually trough props on click of your button like that:

  <GoogleLogin
render={(renderProps) => (!renderProps.disabled ?
  <button  autoFocus onClick={renderProps.onClick}>
    This is my custom Google button
  </button>: null
)}
  />

Keep in mind, if the user clicks Tab, the autofocus will be lost and the enter input will not trigger the onClick event anymore.

Instead, you could set up an event listener which will listen for the enter input and trigger GoogleLogin as long as it is rendered, independently of the autoFocus. Or you let your user click that button instead of enter.

about 4 years ago · Juan Pablo Isaza Denunciar
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