我的代码工作正常,当我在密码字段中写入时,文本被隐藏了。有什么方法可以添加功能,让用户可以根据需要选择查看密码?
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
return (
<div>
<div className='main-content'>
<form className="form" noValidate autoComplete="off">
{[{ label: "Email", state: email , type: "text", function: setEmail},
{ label: "Password", state: password , type: "password", function: setPassword},
].map((item, index) => (
<div>
<TextField
id="outlined-basic"
key={index}
label={item.label}
variant="outlined"
type= {item.type}
onChange= {e => item.function(e.target.value)}
/>
<br></br><br></br>
</div>
)
)}
</form>
</div>
</div>
);
}
原文由 x89 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以根据某些真/假状态更改值的类型。
在您的示例中,您使用循环遍历您的字段。用我的示例替换您的文本字段会将切换添加到所有字段。这(可能)不是您想要的,因此您必须找到一种不同的方式来呈现这些字段。