3 回答

TA貢獻1815條經驗 獲得超13個贊
將您的密碼條件更改為以下代碼
{(type==='password' || type=== 'input') &&
<div>
<p>This is Password</p>
<span onClick={showHide}>
{type === 'input' ? 'Hide' : 'Show'}
</span>
</div>}

TA貢獻1784條經驗 獲得超7個贊
您處理有關何時顯示元素的條件的方式使應用程序崩潰。
此外,您可能需要重新考慮使用它useEffect并允許對組件進行更多控制。您不需要將type和placeholder道具保存到狀態,刪除它會稍微簡化您的代碼。
function TextBox({ type, placeholder }) {
const [ value, setValue ] = useState('');
const [ visible, setVisible ] = useState(false);
const handleChange = (e) => setValue(e.target.value);
const showhide = () => setVisible(visible => !visible);
return (
<div>
<h1>{type}:{placeholder}</h1>
<input
value={value}
type={type}
placeholder={placeholder}
onChange={handleChange}
/>
{type === 'password' && (
<div>
<p>This is Password <button onClick={showhide}>Show/Hide</button></p>
{visible && <span>{value}</span>}
</div>
)}
{type==='text' && <p>This is Text</p>}
{type==='email' && <p>This is an Email</p>}
</div>
);
}
添加回答
舉報