首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用单个useRef循环进入多个元素?

如何使用单个useRef循环进入多个元素?
EN

Stack Overflow用户
提问于 2021-05-29 21:42:28
回答 1查看 62关注 0票数 0

我有两个输入字段,使用一个useRef,我想让current.getAttribute从密码类型更改为文本类型。但是根据docs,只会选择最后一个元素。我想使用.map循环到元素中。我不能使用.map来创建元素,因为由于我的身份验证,每个元素都必须是唯一的。

代码

代码语言:javascript
复制
const passwordRef = useRef();
const handleShowPassword = (e) => {
    console.log(passwordRef.map((e) => console.log(e))); <--- this coz error of passwordRef.map is not a function
    setShowEye(!showEye);
    const type =
      passwordRef.current.getAttribute('type') === 'password'
        ? 'text'
        : 'password';
    passwordRef.current.setAttribute('type', type);
  };

<div className='form-group w-full relative'>
            <PasswordStrength
              type='password'
              placeholder='Current password'
              name='currentPassword'
              ref={passwordRef}
              border={error && error.currentPasswordError ? 'failed' : 'transparent'}
              value={userInfo.currentPassword}
              onChange={handleOnChange}
            />
            {!showEye && (
              <FontAwesomeIcon
                icon={faEye}
                className='fill-current text-customRed absolute top-4 right-4'
                onClick={handleShowPassword}
              />
            )}
            <label for='password'>
              {showEye && (
                <FontAwesomeIcon
                  icon={faEyeSlash}
                  className='fill-current text-customRed absolute top-4 right-4'
                  onClick={handleShowPassword}
                />
              )}
              {error && error.passwordError ? (
                <Notifications type='text-failed' field='password'>
                  <span className='text-xxs absolute -left-0 -bottom--7px fadeIn'>
                    {error.msg}
                  </span>
                </Notifications>
              ) : (
                ''
              )}
            </label>
          </div>
          <div className='form-group w-full relative'>
            <PasswordStrength
              type='password'
              placeholder='New password'
              name='password'
              ref={passwordRef}
              border={error && error.passwordError ? 'failed' : 'transparent'}
              value={userInfo.password}
              onChange={handleOnChange}
            />
            <label for='password'>
              {error && error.passwordError ? (
                <Notifications type='text-failed' field='password'>
                  <span className='text-xxs absolute -left-0 -bottom--7px fadeIn'>
                    {error.msg}
                  </span>
                </Notifications>
              ) : (
                ''
              )}
            </label>
          </div>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-29 22:51:27

简短的回答:你不能。即使这是可能的,你的输入有硬编码的type="password",所以密码的可见性将被重置在每一个改进机上。但实际上你甚至不需要裁判。

您的密码字段几乎相同(至少在视觉上如此)。因此,您可以将密码字段提取到单独的组件中,并重用它两次。这种重新组合的好处是您可以创建内部状态,指示输入的密码是显示为文本还是隐藏。

代码语言:javascript
复制
const PasswordField = ({ label, ...props }) => {
  const [visible, setVisible] = React.useState(false)
  
  return (
    <label>
      {label}:{' '}
      <input
        type={visible ? 'text' : 'password'}
        {...props}
      />
      <button onClick={() => setVisible(!visible)}>
        {visible ? 'Hide' : 'Show'}
      </button>
    </label>
  )
}

const App = () => {
  return (
    <div>
      <div>
        <PasswordField label="Old Password" defaultValue="12345" />
      </div>
      <div>
        <PasswordField label="New Password" defaultValue="12345" />
      </div>
    </div>
  )
}

我的代码片段被有意地简化,以演示上面提到的重新组合。

代码语言:javascript
复制
const PasswordField = ({ label, ...props }) => {
  const [visible, setVisible] = React.useState(false)
  
  return (
    <label>
      {label}:{' '}
      <input
        type={visible ? 'text' : 'password'}
        {...props}
      />
      <button onClick={() => setVisible(!visible)}>
        {visible ? 'Hide' : 'Show'}
      </button>
    </label>
  )
}

const App = () => {
  return (
    <div>
      <div>
        <PasswordField label="Old Password" defaultValue="12345" />
      </div>
      <div>
        <PasswordField label="New Password" defaultValue="12345" />
      </div>
    </div>
  )
}

ReactDOM.render(<App />, document.getElementById('root'))
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>

<div id="root"></div>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67755936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档