how to clear TextInput focus in React Native? (Android)
I have look for an API here:
https://facebook.github.io/react-native/docs/textinput.html#content
But no result...
Anyone can help?
Wrap the View component that is the parent of the TextInput in a Pressable component and then pass Keyboard. dismiss to the onPress prop. So, if the user taps anywhere outside the TextInput field, it will trigger Keyboard. dismiss , which will result in the TextInput field losing focus and the keyboard being hidden.
Use caretHidden={true} if you want to disable all operation like Cut Paste Copy. It will also hide your cursor as well.
Two methods exposed via the native element are .focus() and .blur() that will focus or blur the TextInput programmatically.
I think this should work. Add the onFocus as a prop to your TextInput
. This is assuming you have a state variable called text, and that the TextInput
takes its value from that state variable.
<TextInput
...
onFocus= {() => this.setState({text : ''})}
value={this.state.text}/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With