在这里,我try 创建一个定制组件,它可以通过props 接收数据,并充当基本的表单输入.

但是,我遇到了一个没有显示错误的问题.有人能解释一下我在实现过程中可能犯了什么错误吗?

如果有人知道这一点,我将非常感谢您在解决这个问题上的帮助.

trying to build an reuseable component but now working why??

/* eslint-disable react/prop-types */



const Input = (props) => {
  const {formik, icon, title,type,otherState,btn1,btn2, name} = props
  Input.defaultProps = {
    type: "text",
    otherState: null,
    btn1: null,
    btn2: null,
    name: title.toLowerCase()
  }

   if (otherState) {
     var {state, setState} = otherState
   }
    if (btn1) {
      var {icon1, text1, action1} = btn1
    }
    if (btn2) {
      var {icon2, text2, action2} = btn2
    }

    


  return (
   <div className="mb-2">
   <span className="flex">
     <img className="w-6 h-6 mr-2" src={icon} alt={title} />
     <label
       htmlFor={name}
       className="block text-sm font-semibold text-textPrimary dark:text-dark_textPrimary"
     >
       {title}
     </label>
   </span>
   <span className="flex w-full">
{console.log(formik.touched.name)}
   <input
     
     type={type}
     name={name}
     value={formik.values.name}
     title={`${formik.touched.name && formik.values.name == "" ? `${title} is required` : ""}`}
     onChange={formik.handleChange}
     onBlur={formik.handleBlur}
     placeholder={
       formik.touched.name && formik.errors.name
         ? `${title} is required`
         : `${title}`
     }
     className={`block w-full px-4 py-2 mt-2 text-textPrimary dark:text-dark_textPrimary bg-background dark:bg-dark_background border-2 rounded-md focus:ring-secondary ${
       formik.touched.name && formik.errors.name 
         ? " border-error_red placeholder-error_red"
         : "border-cardBorder dark:border-dark_cardBorder"
     }`}
   />

     {
        btn1 || btn2 ? (
          <img
       className="w-6 h-7 cursor-pointer mt-3 transition duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-110"
       onClick={() => action1()}
       src={state ? icon1 : icon2}
       title={!state ? text1 : text2}
     />) : null
     }
   </span>
{
     formik.touched.name && formik.errors.name && formik.values.name != "" ? (
       <h3 className="text-xs text-error_red">{formik.errors.name}</h3>
     ) : null
   }
 </div>
  )
}

export default Input


这是一个输入组件, 第一个组件用于邮箱,第二个组件用于密码

<Input
          formik={formik}
          icon={emailIcon}
          title="Email"
          name="email"
          type="email"
          />

          <Input 
          formik={formik}
          icon={passwordIcon}
          name="password"
          title="Password"
          type={showPassword ? "password" : "text"}
          otherState={{'state':showPassword, 'setState':setShowPassword}}
          btn1={{'icon1':eyeOpenIcon, 'text1':"Hide Password", 'action1':() => setShowPassword(!showPassword)}}
          btn2={{'icon2':eyeCloseIcon, 'text2':"Show Password", 'action2':() => setShowPassword(!showPassword)}}
       

          />

推荐答案

要访问与给定输入相关的值或错误,应使用其名称.由于在Input组件中,您将获得名称作为nameprops 的一部分,因此您将执行以下操作:

formik.values[name]  // To access its value
formik.touched[name] // To check if it's checked
formik.errors[name]  // To gets its related error

Javascript相关问答推荐

使用Apps Script缩短谷歌表中的URL?

在JavaScript中检索一些文本

如何最好地从TypScript中的enum获取值

格式值未保存在redux持久切片中

Google图表时间轴—更改hAxis文本 colored颜色

使用复选框在d3.js多折线图中添加或删除线条

如何为我的astro页面中的相同组件自动创建不同的内容?

编辑文本无响应.onClick(扩展脚本)

类构造函数不能在没有用With Router包装的情况下调用

检索相加到点的子项

Reaction Redux&Quot;在派单错误中检测到状态Mutations

如何在JAVASCRIPT中临时删除eventListener?

如果一个字符串前面有点、空格或无字符串,后面有空格、连字符或无字符串,则匹配正则表达式

如何为仅有数据可用的点显示X轴标签?

用于测试其方法和构造函数的导出/导入类

react 路由如何使用从加载器返回的数据

JQuery使用选项填充HTMLSELECT并设置默认结果,默认结果显示为空

在Press Reaction本机和EXPO av上播放单个文件

如何检测当前是否没有按下键盘上的键?

限制数组中每个元素的长度,