【问题标题】:Mapping Elements in a Form (for Formik)在表单中映射元素(用于 Formik)
【发布时间】:2020-06-09 01:01:00
【问题描述】:

在我的表单中,我使用 Formik/Yup 进行验证。目前,这在我的表单中完美运行:

export default function AddUserPage() {
  const [firstName, setFirstName] = useState("");
  const [email, setEmail] = useState("");

  return (
    <div>
      <Formik
        initialValues={{ firstName: "", email: "" }}
        onSubmit={(values, actions) => {
          setTimeout(() => {
            alert(JSON.stringify(values, null, 2));
            actions.setSubmitting(false);
          }, 1000);
        }}
        validationSchema={schema}
      >
        {props => {
          const {
            values: { firstName, lastName, email, password, phone },
            errors,
            touched,
            handleChange,
            isValid,
            setFieldTouched
          } = props;
          const change = (name: string, e: any) => {
            e.persist();
            handleChange(e);
            setFieldTouched(name, true, false);
          };
          return (
            <div className="main-content">
              <form style={{ width: "100%" }}>
                <div>
                  <TextField
                    variant="outlined"
                    margin="normal"
                    id="firstName"
                    name="firstName"
                    helperText={touched.firstName ? errors.firstName : ""}
                    error={touched.firstName && Boolean(errors.firstName)}
                    label="First Name"
                    //onChange={e => setFirstName(e.target.value)}
                    value={firstName}
                    onChange={change.bind(null, "firstName")}
                  />
                  <br></br>
                  <TextField
                    variant="outlined"
                    margin="normal"
                    id="email"
                    name="email"
                    helperText={touched.email ? errors.email : ""}
                    error={touched.email && Boolean(errors.email)}
                    label="Email"
                    value={email}
                    onChange={change.bind(null, "email")}
                  />
                  <br></br>
                </div>
              </form>
            </div>
          );
        }}
      </Formik>
    </div>
  );
}

但是,我不想输入所有内容,而是想映射我的文本字段。我如何映射我的文本字段?

我尝试过这样的事情,但我得到了 helperText 和错误属性的错误:

{[{ label: "First Name", state: firstName , type: "text", function: setFirstName},
{ label: "Email", state: email , type: "text", function: setEmail},
  ]}.map((item, index) => (
  <div>
    <TextField
      id="outlined-basic"
      key={index}
      label={item.label}
      variant="outlined"
      type= {item.type}
      helperText= {touched[item.state] ? errors[item.state] : ""}
      onChange={change.bind(null, state)}        
    />
    <br></br><br></br>
  </div>
)

在这里,我在帮助文本上收到一个错误:

元素隐式具有“any”类型,因为“any”类型的表达式不能用于索引类型“FormikTouched'

我也尝试添加这个,但这也给了我一个错误:

helperText = {touched.item.state}

【问题讨论】:

  • touched.{item.state} 这不是有效的 JS。
  • 我可以使用什么替代方案?对此有点新意。 @evolutionxbox
  • 也许使用括号表示法? touched[item.state]
  • 不,没用
  • “没用”是什么意思?有错误吗?

标签: javascript reactjs typescript mapping material-ui


【解决方案1】:

看起来您在 .map 之前有一个合成错误数字括号

这段代码用地图渲染一些jsx:

[
  { label: "First Name",  type: "text"},
{ label: "Email",  type: "text"},
  ].map((item, i) => (
  <div>
    <TextField
      key={i}
      label={item.label}
      type= {item.type}    
    />
  </div>
)

【讨论】:

  • 哦,这可能是因为我在发布之前从地图中删除了一些项目。但这不是我的问题/问题
  • 我想知道如何映射这些项目:helperText={touched.email ? errors.email : ""} error={touched.email &amp;&amp; Boolean(errors.email)}
猜你喜欢
  • 1970-01-01
  • 2020-03-23
  • 2015-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-21
相关资源
最近更新 更多