Have a simple search bar (input field) which should trigger an onSubmit action while we press Enter button
Asked
Active
Viewed 295 times
0
-
use onKeyPress: https://stackoverflow.com/questions/27827234/how-to-handle-the-onkeypress-event-in-reactjs – Joey Apr 12 '22 at 14:55
2 Answers
0
You can use onKeyPress.
Check this : https://stackoverflow.com/questions/13987300/how-to-capture-enter-key-press

yanir midler
- 2,153
- 1
- 4
- 16
0
Simple, have a button type submit inside the form. Here's an example:
const onSubmit = () => {}
// ..
<form onSubmit={onSubmit}>
...
<button type="submit">Search</button>
...
</form>
You don't need to use onKeyPress event.

Vijay Dev
- 1,024
- 7
- 14
-
1
-
You can have an id on form ``. Then the input or any element can part of the form with form attribute: ` – Vijay Dev Apr 12 '22 at 15:01