admin管理员组

文章数量:1195301

I want to fire a function when the user hit backspace in a textbox. I have searched on stack overflow and React Native docs but there is not a relevant answer. basically, I don't know whats the keyname for backspace

      <TextInput
          onChangeText = {e => {
          e === 'BackSpace' ? alert('delete') : alert(e)
       }}/>

I want to fire a function when the user hit backspace in a textbox. I have searched on stack overflow and React Native docs but there is not a relevant answer. basically, I don't know whats the keyname for backspace

      <TextInput
          onChangeText = {e => {
          e === 'BackSpace' ? alert('delete') : alert(e)
       }}/>
Share Improve this question asked Oct 31, 2018 at 14:30 SyedSyed 5303 gold badges10 silver badges27 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 27

onChangeText only sends the updated text value. It does not send any other information such as keypress. onChange sends much more information, onChangeText is just for convenience.

You could use onKeyPress

<TextInput
  onKeyPress={({ nativeEvent }) => {
    nativeEvent.key === 'Backspace' ? //do action : //other action
  }}
/>

You cannot use alert however, you must use Alert in react-native which has a different API.

However it might be easier to just use onChangeText depending on what you're trying to accomplish. If the text value sent is shorter than the currently controlled text value, you can handle whatever backspace code you're using and manage the text input value in one place.

As suggested here you could try something like this :

<TextInput
     onKeyPress = {e => {
     e.keyCode === 'Backspace' ? alert('delete') : alert(e)
}}/>

本文标签: javascripthow to check for backspace(delete) keyPress in react native (iOS)Stack Overflow