在 React 组件中将两个函数合并为一个时遇到问题

问题描述 投票:0回答:1

我在尝试将 React 组件中的两个函数合并为一个时遇到问题。具体来说,我正在开发一个登录页面,其中有两个独立的功能:一个用于更改电子邮件输入字段和更新眼睛图像,另一个用于切换密码可见性。这是我的代码:


import { useState } from "react";
import face1 from "../assets/one.png";
import face2 from "../assets/two.png";
import face3 from "../assets/three.png";
import face4 from "../assets/four.png";
import face5 from "../assets/five.png";
import hide from "../assets/six.png";
import show from "../assets/seven.png";

const LoginPage = () => {
  const [email, setEmail] = useState("");
  const [showPassword, setShowPassword] = useState(false);
  const [eyesIndex, setEyesIndex] = useState(0);

  const handleEmailChange = (e) => {
    setEmail(e.target.value);
    if (e.target.value !== "") {
      setEyesIndex((prevIndex) => (prevIndex + 1) % 5);
      console.log(`Image changed to face${((eyesIndex + 1) % 5) + 1}`);
    }
  };

  const togglePasswordVisibility = () => {
    setShowPassword(!showPassword);
  };

  const handleKeyPress = (e) => {
    if (e.key === "Backspace" && email === "") {
      setEyesIndex(0);
      console.log("Image changed to face1");
    }
  };

  const getPasswordImage = () => {
    return showPassword ? show : hide;
  };

  return (
    <div className="min-h-screen bg-gray-100 flex flex-col items-center justify-center py-12 sm:px-6 lg:px-8">
      <div className="sm:mx-auto sm:w-full flex items-center flex-col sm:max-w-md">
        <h2 className="mt-6 text-center text-3xl font-extrabold text-gray-900">
          Login
        </h2>
        <div className="home-page h-[10rem] w-[15rem] flex items-center">
          <img
            className="mx-auto h-16 w-auto"
            src={[face1, face2, face3, face4, face5][eyesIndex]}
            alt="Eyes"
          />
          <img
            className="mx-auto h-16 w-auto"
            src={getPasswordImage()}
            alt="Password"
          />
        </div>
      </div>

      <div className="mt-8 sm:mx-auto sm:w-full sm:max-w-md">
        <div className="bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10">
          <form className="space-y-6">
            <div>
              <label
                htmlFor="email"
                className="block text-sm font-medium text-gray-700"
              >
                Email address
              </label>
              <div className="mt-1 relative">
                <input
                  id="email"
                  name="email"
                  type="email"
                  autoComplete="email"
                  onKeyDown={handleKeyPress}
                  required
                  value={email}
                  onChange={handleEmailChange}
                  placeholder="Email address"
                  className="pl-10 appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm"
                />
                <div className="absolute inset-y-0 right-0 pr-3 flex items-center pointer-events-none">
                  <div className="h-5 w-5 text-gray-400">
                    <i className="fa-regular fa-envelope"></i>
                  </div>
                </div>
              </div>
            </div>

            <div>
              <label
                htmlFor="password"
                className="block text-sm font-medium text-gray-700"
              >
                Email address
              </label>
              <div className="mt-1 relative">
                <input
                  id="password"
                  name="password"
                  type={showPassword ? "text" : "password"}
                  autoComplete="off"
                  placeholder="Password"
                  className="pl-10 appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm"
                />
                <div
                  className="absolute inset-y-0 right-0 pr-3 flex items-center cursor-pointer"
                  onClick={togglePasswordVisibility}
                >
                  <div className="h-5 w-5 text-gray-400">
                    {showPassword ? (
                      <i className="fa-regular fa-eye-slash"></i>
                    ) : (
                      <i className="fa-regular fa-eye"></i>
                    )}
                  </div>
                </div>
              </div>
            </div>
            <div className="flex items-center justify-between">
              <div className="flex items-center">
                <input
                  id="remember-me"
                  name="remember-me"
                  type="checkbox"
                  className="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded"
                />
                <label
                  htmlFor="remember-me"
                  className="ml-2 block text-sm text-gray-900"
                >
                  Remember me
                </label>
              </div>

              <div className="text-sm">
                <a
                  href="#"
                  className="font-medium text-blue-600 hover:text-blue-500"
                >
                  Forgot your password?
                </a>
              </div>
            </div>

            <div>
              <button
                type="submit"
                className="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
              >
                Log in
              </button>
            </div>
          </form>
        </div>
      </div>
    </div>
  );
};

export default LoginPage;

现在,我想将handleEmailChange 和togglePasswordVisibility 函数合并到一个函数中,以处理电子邮件更改和密码可见性切换。但是,我不确定如何有效地组合这些功能。当我尝试合并它们时,它导致了一些意想不到的行为。

有人可以指导我如何正确组合这两个功能吗?任何帮助或建议将不胜感激!

reactjs authentication dynamic jsx
1个回答
0
投票

我不确定组合这些功能是否有正当理由,但以下是如何实现所需结果的方法:

const handleEmailandPassword = (e) => {
 if(e !== null){
    setEmail(e.target.value);
    if (e.target.value !== "") {
      setEyesIndex((prevIndex) => (prevIndex + 1) % 5);
      console.log(`Image changed to face${((eyesIndex + 1) % 5) + 1}`);
    }
 }else{
    setShowPassword(!showPassword);
 }

}

为了

onChange
活动

onChange={(e) => handleEmailandPassword(e)}

为了

OnClick
活动

onClick={() => handleEmailandPassword(null)}
© www.soinside.com 2019 - 2024. All rights reserved.