Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unfocus a TextInput in React Native

I'm building an Android app with React Native.

How can you force a TextInput to "unFocus", meaning the cursor is blinking inside the text field. There are functions for isFocused() and onFocus(), but how do I actually get the text field to give up focus. You would think it does so automatically once I hit enter, but that's not the case.

   import React, {Component} from 'react';    import { AppRegistry, Text, View, StyleSheet, TextInput, TouchableOpacity}     from 'react-native';     var SHA256 = require("crypto-js/sha256");     export default class LoginForm extends Component{   constructor(props){     super(props);     this.state = {         email: '',         password:''     }; }  tryLogin = () => {     if(this.state.email=="email123" && this.state.password == "password"){         console.log("password verified");         this.props.navigator.replace({             title: 'Dashboard'         });     }      console.log(this.state.email);     console.log(this.state.password);     console.log("Hash" + SHA256(this.state.password)); }  render(){     return(         <View style={styles.container}>             <TextInput                  style={styles.input}                  placeholder="Email address"                  placeholderTextColor="white"                 onChangeText={(email) => this.setState({email})}>             </TextInput>             <TextInput style={styles.input}                  placeholder="Password"                  placeholderTextColor="white"                  secureTextEntry                 onChangeText={(password) => this.setState({password})}>             </TextInput>              <TouchableOpacity style={styles.loginButtonContainer} onPress={this.tryLogin}>                 <Text style={styles.loginButtonText}>LOGIN</Text>             </TouchableOpacity>         </View>   ); } }  AppRegistry.registerComponent('LoginForm', () => LoginForm);  const styles =  StyleSheet.create({ container: {     padding: 20 }, input:{     height: 40,     backgroundColor: '#e74c3c',     marginBottom: 20,     color: 'white',     paddingHorizontal: 15,     opacity: .9 }, loginButtonContainer:{     justifyContent: 'center',     backgroundColor: '#bc4c3c',     paddingVertical:15  }, loginButtonText:{     textAlign:'center',     color:'white',     fontWeight: '700',     fontSize: 24  }     }) 

This probably won't matter as much for real users but I'm just emulating and its pesky if I want to reload.

like image 479
Noah Mendoza Avatar asked Apr 15 '17 22:04

Noah Mendoza


People also ask

How do you remove focus from element react?

The blur() method removes focus from an element.

What is TextInput in react native?

TextInput is a Core Component that allows the user to enter text. It has an onChangeText prop that takes a function to be called every time the text changed, and an onSubmitEditing prop that takes a function to be called when the text is submitted.

How do I see focus in react native?

To check if an element is focused in React:Set the ref prop on the element. After the element is rendered, check if the element is the active element in the document. If it is, the element is focused.


1 Answers

A better way is to use ScrollView and Keyboard.dismiss. By using ScrollView when the user taps outside of textInput, keyboard dismissed. It's done because ScrollView default property for keyboardShouldPersistTaps is never. It's the behavior the user expects. For dismiss the keyboard, or it's equivalent blur the textInput, when the user tap on the login button add Keyboard.dismissed() to the tryLogin function.

import React, {Component} from 'react'; import { AppRegistry, Text, View, StyleSheet, TextInput, TouchableOpacity, ScrollView, Keyboard}   from 'react-native'; var SHA256 = require("crypto-js/sha256");  export default class LoginForm extends Component{     constructor(props){     super(props);     this.state = {       email: '',       password:''     };   }    tryLogin = () => {     Keyboard.dismiss();     if(this.state.email=="email123" && this.state.password == "password"){       console.log("password verified");       this.props.navigator.replace({         title: 'Dashboard'       });     }      console.log(this.state.email);     console.log(this.state.password);     console.log("Hash" + SHA256(this.state.password));   }    render(){     return(       <ScrollView style={styles.container}>         <TextInput           style={styles.input}            placeholder="Email address"           placeholderTextColor="white"           onChangeText={(email) => this.setState({email})}>         </TextInput>         <TextInput style={styles.input}                    placeholder="Password"                    placeholderTextColor="white"                    secureTextEntry                    onChangeText={(password) => this.setState({password})}>         </TextInput>          <TouchableOpacity style={styles.loginButtonContainer} onPress={this.tryLogin}>           <Text style={styles.loginButtonText}>LOGIN</Text>         </TouchableOpacity>       </ScrollView>     );   } }  AppRegistry.registerComponent('LoginForm', () => LoginForm);  const styles =  StyleSheet.create({   container: {     padding: 20   },   input:{     height: 40,     backgroundColor: '#e74c3c',     marginBottom: 20,     color: 'white',     paddingHorizontal: 15,     opacity: .9   },   loginButtonContainer:{     justifyContent: 'center',     backgroundColor: '#bc4c3c',     paddingVertical:15    },   loginButtonText:{     textAlign:'center',     color:'white',     fontWeight: '700',     fontSize: 24    }  }) 
like image 97
Meysam Izadmehr Avatar answered Sep 19 '22 08:09

Meysam Izadmehr