【问题标题】:React Native TypeError: undefined is not an object ('evaluating useFormikContext.errors')React Native TypeError: undefined is not an object ('evalating useFormikContext.errors')
【发布时间】:2021-10-21 08:40:58
【问题描述】:

我正在开发一个带有本机反应的应用程序。对表单使用 formik,我收到此错误 'TypeError: undefined is not an object (evalating '_useFormikContext.errors') '

import { useFormikContext } from "formik";

import { ErrorMessage } from "./index";
import ImageInputList from "../ImageInputList";

function FormImagePicker({ name }) {
  const { errors, setFieldValue, touched, values } = useFormikContext();
  const imageUris = values[name];

  const handleAdd = (uri) => {
    setFieldValue(name, [...imageUris, uri]);
  };
  const handleRemove = (uri) => {
    setFieldValue(
      name,
      imageUris.filter((imageUri) => imageUri !== uri)
    );
  };

  return (
    <>
      <ImageInputList
        imageUris={imageUris}
        onAddImage={handleAdd}
        onRemoveImage={handleRemove}
      />
      <ErrorMessage error={errors[name]} visible={touched[name]} />
    </>
  );
}

export default FormImagePicker;

【问题讨论】:

    标签: javascript reactjs react-native formik


    【解决方案1】:

    您正在尝试获取尚未定义的对象属性,请尝试这样做:

    {errors && (<ErrorMessage error={errors[name]} visible={touched[name]} />)}
    

    如果这不是您想要的,请尝试寻找不同的解决方案,但请始终记住,您不能像这样获得errors[name],在这样做之前您必须确保errors 存在。

    希望这能解决您的问题,干杯!

    【讨论】:

    • 我试过了,还是出现同样的错误,请帮助我
    猜你喜欢
    • 2016-07-30
    • 2020-08-31
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多