Files
30-seconds-of-code/snippets/password-revealer.md
Angelos Chalaris f1ce423d01 Kebab file names
2023-04-27 22:04:15 +03:00

32 lines
826 B
Markdown

---
title: Show/hide password toggle
tags: components,input,state
author: chalarangelo
cover: thread
firstSeen: 2018-10-18T20:04:22+03:00
lastUpdated: 2020-11-25T20:46:35+02:00
---
Renders a password input field with a reveal button.
- Use the `useState()` hook to create the `shown` state variable and set its value to `false`.
- When the `<button>` is clicked, execute `setShown`, toggling the `type` of the `<input>` between `'text'` and `'password'`.
```jsx
const PasswordRevealer = ({ value }) => {
const [shown, setShown] = React.useState(false);
return (
<>
<input type={shown ? 'text' : 'password'} value={value} />
<button onClick={() => setShown(!shown)}>Show/Hide</button>
</>
);
};
```
```jsx
ReactDOM.createRoot(document.getElementById('root')).render(
<PasswordRevealer />
);
```