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

356
Views
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 answers
Answer question

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 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!