Files
30-seconds-of-code/snippets/PasswordRevealer.md
Isabelle Viktoria Maciohsek 2af2490ca8 Bake dates into snippets
2021-06-13 19:44:42 +03:00

776 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
PasswordRevealer components,input,state,beginner 2018-10-18T20:04:22+03:00 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".
const PasswordRevealer = ({ value }) => {
  const [shown, setShown] = React.useState(false);
  return (
    <>
      <input type={shown ? 'text' : 'password'} value={value} />
      <button onClick={() => setShown(!shown)}>Show/Hide</button>
    </>
  );
};
ReactDOM.render(<PasswordRevealer />, document.getElementById('root'));