The toggle password visibility helps for a good user experience and lets the user see and confirm what they are typing.
And in most cases, it can even completely eliminate the need for a confirm password field.
Why The Confirm Password Field is Useless
So let’s get into it.
Download the eye-close and eye-open icon from Flaticons here.
Set the toggle state for the password visibility in the constructor and write the functions
constructor(props){
super(props);
this.state = {
hidePassword: true
}
}
//Toggle password visibility
managePasswordVisibility = () => {
this.setState({hidePassword: !this.state.hidePassword});
}; Next, import these downloaded icons to your project. Also import TextInput from React Native
<View style={[styles.container, {marginBottom: 18}]}>
<TextInput
style={styles.inputFlex}
secureTextEntry={this.state.hidePassword}
autoCompleteType="password"
autoCapitalize="none"
autoCorrect={false}
returnKeyType="send"
onChangeText={(UserPassword) => this.setState({UserPassword})}
/>
<TouchableOpacity
activeOpacity={0.8}
style={styles.visibilityBtn}
onPress={this.managePasswordVisibility}>
<Image
source={
this.state.hidePassword
? require('../assets/images/show.png')
: require('../assets/images/hide.png')
}
style={styles.btnImage}
/>
</TouchableOpacity>
</View>
</View>
Finally, we’ll add some styling
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
flexWrap: 'nowrap',
marginTop: 10,
},
inputFlex: {
alignSelf: 'stretch',
width: '100%',
padding: 0,
backgroundColor: '#ddd'
},
visibilityBtn: {
position: 'absolute',
right: 9,
height: 25,
width: 25,
padding: 0,
marginTop: 21,
},
}) import { Component }, React from 'react';
import { View, Text, TextInput, TouchableOpacity, Stylesheet } from 'react-native';
class App extends Component {
constructor(props){
super(props);
this.state = {
hidePassword: true
}
}
//Toggle password visibility
managePasswordVisibility = () => {
this.setState({hidePassword: !this.state.hidePassword});
};
render(){
return(
<View style={[styles.container, {marginBottom: 18}]}>
<TextInput
style={styles.inputFlex}
secureTextEntry={this.state.hidePassword}
autoCompleteType="password"
autoCapitalize="none"
autoCorrect={false}
returnKeyType="send"
onChangeText={(UserPassword) => this.setState({UserPassword})}
/>
<TouchableOpacity
activeOpacity={0.8}
style={styles.visibilityBtn}
onPress={this.managePasswordVisibility}>
<Image
source={
this.state.hidePassword
? require('../assets/images/show.png')
: require('../assets/images/hide.png')
}
style={styles.btnImage}
/>
</TouchableOpacity>
</View>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
flexWrap: 'nowrap',
marginTop: 10,
},
inputFlex: {
alignSelf: 'stretch',
width: '100%',
padding: 0,
backgroundColor: '#ddd'
},
visibilityBtn: {
position: 'absolute',
right: 9,
height: 25,
width: 25,
padding: 0,
marginTop: 21,
},
})
export default App; There we have it, our React Native toggle password implementation in our software application. Feel free to modify the code to suit your own project.
Let me know what you think in the comments!
Toggle Password Visibility With Android Studio
Latest tech news and coding tips.
1. What Is the Golden Ratio? The Golden Ratio, represented by the Greek letter φ (phi), is…
In CSS, combinators define relationships between selectors. Instead of selecting elements individually, combinators allow you to target elements based…
Below is a comprehensive, beginner-friendly, yet deeply detailed guide to Boolean Algebra, complete with definitions, laws,…
Debugging your own code is hard enough — debugging someone else’s code is a whole…
Git is a free, open-source distributed version control system created by Linus Torvalds.It helps developers: Learn how to…
Bubble Sort is one of the simplest sorting algorithms in computer science. Although it’s not…