react native

React Native Toggle Password Visibility

Professionally built mobile app templates to kickstart your project

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.

Step 1:

Download the eye-close and eye-open icon from Flaticons here.

Step 2:

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});
  };

Step 3:

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>

Step 3:

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,
  },

})

Full Code Here:

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;

Conclusion

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

Recent Posts

Service Workers in JavaScript: An In-Depth Guide

Service Workers are one of the core features of modern web applications, offering powerful capabilities…

1 week ago

What are Database Driven Websites?

A database-driven website is a dynamic site that utilizes a database to store and manage…

2 weeks ago

How to show Toast Messages in React

Toasts are user interface elements commonly used in software applications, especially in mobile app development…

2 weeks ago

Exploring the Relationship Between JavaScript and Node.js

JavaScript has long been synonymous with frontend web development, powering interactive and dynamic user interfaces…

3 weeks ago

Key Differences Between Tailwind CSS and CSS3

Introduction: In the world of web development, CSS (Cascading Style Sheets) plays a crucial role…

3 weeks ago

Why Everybody is Learning JavaScript Right Now

JavaScript has emerged as a ubiquitous programming language, powering the modern web and extending its…

3 weeks ago