Open In App

How to show and hide Password in ReactJS?

Last Updated : 30 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

To show and hide passwords in React JS we can simply use the password input with an input to select the show and hide state. The user might need to see what has he used as the password to verify which is an important concept in login and registration forms. We can create this type of password input using state and also with the help of external libraries.

Prerequisites

Creating React Application:

Step 1: Create a React application using the following command:

npx create-react-app foldername

Step 2: After creating your project folder i.e. foldername, move to it using the following command:

cd foldername 

Project Structure

It will look like the following.

Project Structure

Approach 1: using useState Hook

We will be using useState variable with react funtional component to store and update the show and hide state and store password along with the checkbox linked with this state.

Example: Created a password input that changes to type text if the checkbox is clicked.

Javascript




// Filename - App.js
 
import React, { useState } from "react";
 
import "./App.css";
 
function App() {
    const [password, setPassword] = useState("");
    const [showPassword, setShowPassword] = useState(false);
    return (
        <div className="App">
            <h1 className="geeks">GeeksforGeeks</h1>
            <h3>React Example to Show/Hide password</h3>
            <div>
                <label for="pass">Enter password: </label>
                <input
                    id="pass"
                    type={
                        showPassword ? "text" : "password"
                    }
                    value={password}
                    onChange={(e) =>
                        setPassword(e.target.value)
                    }
                />
                <br />
                <br />
                <label for="check">Show Password</label>
                <input
                    id="check"
                    type="checkbox"
                    value={showPassword}
                    onChange={() =>
                        setShowPassword((prev) => !prev)
                    }
                />
            </div>
            <br />
        </div>
    );
}
 
export default App;


CSS




/* Filename - App.css */
 
.App {
    text-align: center;
    margin: auto;
}
 
.geeks {
    color: green;
}


Step to Run Application: Run the application using the following command from the root directory of the project.

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output.

Approach 2: using MUI Inputs

Password can be shown to the user by adding a feature of the eye icon so that the user can see the password. Material UI for React has this component available for us and it is very easy to integrate. We can use some core material Components in ReactJS using the following approach.

Step to install MUI: Install the material-ui modules using the following command:

npm i @material-ui/core @material-ui/icons

Dependencies:

{
"dependencies": {
"@material-ui/core": "^4.12.4",
"@material-ui/icons": "^4.11.3",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
}

Example: Creating password input to hide and make the password visible as a text using Material UI input components.

Javascript




import React from "react";
import IconButton from "@material-ui/core/IconButton";
import InputLabel from "@material-ui/core/InputLabel";
import Visibility from "@material-ui/icons/Visibility";
import InputAdornment from "@material-ui/core/InputAdornment";
import VisibilityOff from "@material-ui/icons/VisibilityOff";
import Input from "@material-ui/core/Input";
 
const App = () => {
    const [values, setValues] = React.useState({
        password: "",
        showPassword: false,
    });
 
    const handleClickShowPassword = () => {
        setValues({
            ...values,
            showPassword: !values.showPassword,
        });
    };
 
    const handleMouseDownPassword = (event) => {
        event.preventDefault();
    };
 
    const handlePasswordChange = (prop) => (event) => {
        setValues({
            ...values,
            [prop]: event.target.value,
        });
    };
 
    return (
        <div
            style={{
                marginLeft: "30%",
            }}
        >
            <h4>
                How to show and hide password in ReactJS?
            </h4>
            <InputLabel htmlFor="standard-adornment-password">
                Enter your Password
            </InputLabel>
            <Input
                type={
                    values.showPassword
                        ? "text"
                        : "password"
                }
                onChange={handlePasswordChange("password")}
                value={values.password}
                endAdornment={
                    <InputAdornment position="end">
                        <IconButton
                            onClick={
                                handleClickShowPassword
                            }
                            onMouseDown={
                                handleMouseDownPassword
                            }
                        >
                            {values.showPassword ? (
                                <Visibility />
                            ) : (
                                <VisibilityOff />
                            )}
                        </IconButton>
                    </InputAdornment>
                }
            />
        </div>
    );
};
 
export default App;


Step to Run Application: Run the application using the following command from the root directory of the project.

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads