How to submit a form using Enter key in react.js?

前端 未结 6 433
野的像风
野的像风 2020-12-04 10:34

Here is my form and the onClick method. I would like to execute this method when the Enter button of keyboard is pressed. How ?

N.B: No jquery is appreciated.

6条回答
  •  情话喂你
    2020-12-04 11:12

    this is how you do it if you want to listen for the "Enter" key. There is an onKeydown prop that you can use and you can read about it in react doc

    and here is a codeSandbox

    const App = () => {
        const something=(event)=> {
            if (event.keyCode === 13) {
                console.log('enter')
            }
        }
    return (
        

    Hello CodeSandbox

    Start editing to see some magic happen!

    something(e) }/>
    ); }

提交回复
热议问题