React - detect 'enter' key press in change event

后端 未结 1 395
无人共我
无人共我 2021-01-07 22:18

I have a React component that is, more or less, a simple textarea where a user can type into. Here is what the code looks like so far:

import React from \'r         


        
相关标签:
1条回答
  • 2021-01-07 22:48

    You can use both of handlers same time.

    In onKeyDown you can check key. If it is ENTER pressed call event.preventDefault() to prevent onChange call, or if it is not - do nothing

    Javascript event queue doesn't contain change event until default handler for keydown event is called. If you call event.preventDefault() in onKeyDown handler no change event will be fired.

    0 讨论(0)
提交回复
热议问题