亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為所有輸入類型創建通用文本框

為所有輸入類型創建通用文本框

繁花不似錦 2021-10-21 17:15:44
由于我是 React JS 的新手,我需要支持為所有輸入類型創建一個通用文本框。此外,我已經開發了文本框,但是當類型為密碼時,我需要顯示密碼功能。但是,目前,當我第一次單擊它時,我的顯示密碼元素消失了。import React, { useState, useEffect } from 'react';const TextBox=(props)=> {const [type, setType] = useState('text');const [placeholder, setPlaceholder] = useState('Text');const [passValue, setPassValue] = useState();useEffect(() => {    setType(props.type);    setPlaceholder(props.placeholder);}, []);const handleChange=(e)=>{    setPassValue(e.target.value);}const showHide=(e)=>{    e.preventDefault();    e.stopPropagation();    setType(type === 'input' ? 'password' : 'input') }return (    <div>    <h1>{type}:{placeholder}</h1>    <input type={type} placeholder={placeholder} onChange={handleChange}></input>    {(type==='password', type=== 'input') &&         <div>            <p>This is Password</p>                <span onClick={showHide}>{type === 'input' ? 'Hide' : 'Show'}</span>        </div>    }    {(type==='text')&&<p>This is Text</p>}    {(type==='email')&&<p>This is an Email</p>}    </div>);}export default TextBox;我的道具正在通過以下代碼獲?。?lt;TextBox type="password" placeholder="Enter your Password"/>如何在左側的輸入文本中顯示顯示/隱藏,并保留上述代碼?<input class='container textContainer' type={type} placeholder={placeholder} onChange={handleChange} /> {(type==='password' || type=== 'input') && <span onClick={showHide}>     {type === 'input' ? 'Hide' : 'Show'} </span> }
查看完整描述

3 回答

?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

將您的密碼條件更改為以下代碼


  {(type==='password' || type=== 'input') && 

  <div>

    <p>This is Password</p>

      <span onClick={showHide}>

        {type === 'input' ? 'Hide' : 'Show'}

      </span>

  </div>}


查看完整回答
反對 回復 2021-10-21
?
嚕嚕噠

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>

  );

}


查看完整回答
反對 回復 2021-10-21
  • 3 回答
  • 0 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號