Renders a password input field with a reveal button.
- Use the
React.useState()
hook to create theshown
state variable and set its value tofalse
. - Use a
<div>
to wrap both the<input>
and the<button>
element that toggles the type of the input field between"text"
and"password"
.
代码实现
function PasswordRevealer({ value }) {
const [shown, setShown] = React.useState(false);
return (
<div>
<input type={shown ? 'text' : 'password'} value={value} onChange={() => {}} />
<button onClick={() => setShown(!shown)}>Show/Hide</button>
</div>
);
}
使用样例
ReactDOM.render(<PasswordRevealer />, document.getElementById('root'));