Skip to content

Edvins's Blog

Show and hide password in React

React1 min read

In this article, you’ll learn how to show and hide password input field in React.js using basic state manipulations. Moreover, you will learn how to make this component more re-usable to suit your needs. Initially, let's start by creating a PasswordShowHide component which has hidden state parameter set to true by default. As well as generic input and button elements.

1class PasswordShowHide extends Component {
2 constructor(props) {
3 super(props);
4
5 this.state = {
6 hidden: true
7 };
8 }
9
10 render() {
11 return (
12 <div>
13 <input type="text" />
14 <button>Show / Hide</button>
15 </div>
16 );
17 }
18}

What are we doing next

  • Add an onClick={this.toggleShow} event to the button element.
  • Create a method where we toggle hidden parameter of the state.
  • Bind this.toggleShow in the class constructor.
  • Update input type to be dependable on the hidden state parameter, so if the hidden is true - we use password type, and text for false.
1import React, { Component } from 'react';
2
3class PasswordShowHide extends Component {
4 constructor(props) {
5 super(props);
6
7 this.state = {
8 hidden: true
9 };
10 this.toggleShow = this.toggleShow.bind(this);
11 }
12
13 toggleShow() {
14 this.setState({ hidden: !this.state.hidden });
15 }
16
17 render() {
18 return (
19 <div>
20 <input type={this.state.hidden ? 'password' : 'text'} />
21 <button onClick={this.toggleShow}>Show / Hide</button>
22 </div>
23 );
24 }
25}
26
27export default PasswordShowHide;

You've made it so far. Now we toggle state upon the button click. But what next?




Making a better component

We are nearly finished as we have only last final step to make. We need to implement the onChange logic for the input component as well as slightly update the component so we could pass password as a prop (e.g. PasswordShowHide password="qwerty1337").

1import React, { Component } from 'react';
2
3class PasswordShowHide extends Component {
4 constructor(props) {
5 super(props);
6
7 this.state = {
8 hidden: true,
9 password: ''
10 };
11
12 this.handlePasswordChange = this.handlePasswordChange.bind(this);
13 this.toggleShow = this.toggleShow.bind(this);
14 }
15
16 handlePasswordChange(e) {
17 this.setState({ password: e.target.value });
18 }
19
20 toggleShow() {
21 this.setState({ hidden: !this.state.hidden });
22 }
23
24 componentDidMount() {
25 if (this.props.password) {
26 this.setState({ password: this.props.password });
27 }
28 }
29
30 render() {
31 return (
32 <div>
33 <input
34 type={this.state.hidden ? 'password' : 'text'}
35 value={this.state.password}
36 onChange={this.handlePasswordChange}
37 />
38 <button onClick={this.toggleShow}>Show / Hide</button>
39 </div>
40 );
41 }
42}
43
44export default PasswordShowHide;



Conclusion

Now when the implementation is finished, we can finally start using this component. We  can either call <PasswordShowHide /> component as it is or <PasswordShowHide password="qwerty1337" /> (e.g.<PasswordShowHide password={bla.bla.bla.password} />) and pass password as a prop in order to pre-populate the input field.




Demo

  • GitHub
  • CodeSandbox
© 2020 by Edvins Antonovs. All rights reserved.