简体   繁体   English

错误:使用 Formik 和传递值时,对象作为 React 子项无效

[英]Error: Objects are not valid as a React child when using Formik and passing values

I'm building a form using Formik.我正在使用 Formik 构建一个表单。 Following the warning saying that the render props would soon be depreciated, I tried to get rid of it and move render={({values}) => {.....}} to {({values}) => {.....}} (the latter being placed as a child rather than a prop but I get the following error:在警告说渲染道具很快就会贬值之后,我试图摆脱它并将render={({values}) => {.....}}移动到{({values}) => {.....}} (后者被放置为孩子而不是道具,但我收到以下错误:

Error: Objects are not valid as a React child (found: object with keys {firstName, lastName, items}). If you meant to render a collection of children, use an array instead.

My understanding is that I'm destructuring props when I'm passing ({ values }) so I assumed values was going to be my initialValues object and then I could just map my patients array to create as many components as needed.我的理解是,当我传递({ values })时我正在解构props ,所以我假设值将是我的initialValues object 然后我可以只 map 我的patients数组来创建所需的组件。

I suppose React considers values to be an object so he's not happy as he expect a Children element but what should I be passing then?我想 React 认为values是 object 所以他不高兴,因为他期望一个 Children 元素,但我应该传递什么? I tried (...props) but it's also giving me an error.我试过(...props)但它也给了我一个错误。

I sense I'm doing a rookie mistake handling my props but I can't pinpoint it:/我觉得我在处理我的道具时犯了一个新手错误,但我无法确定它:/

Any thoughts?有什么想法吗?

My Formik component is the following:我的 Formik 组件如下:

<Formik
  initialValues={{
    patients: [
      {
        id: "1",
        firstName: "",
        lastName: "",
        items: "",
      },
    ],
    phoneNumber: "",
    peopleOrdering: 1,
    monthOrder: "1",
    collectionOrDelivery: "",
    waitingOrLater: "",
    address: "",
    staffMember: "",
    claimReceipt: "",
    whereIsPrescription: "",
    keepSubscription: "",
    }}
    validationSchema={OrderSchema}
    onSubmit={() => {}}
>
  {({ values }) => (
    <Form>
      <FormItem name="patients">
        {values.patients.map((patient, i) => (
          <div key={patient}>
            <Input name={`patients[${i}].firstName`}></Input>
            <Input name={`patients[${i}].lastName`}></Input>
            <Input name={`patients[${i}].items`}></Input>
          </div>
        ))}
      </FormItem>
     // ... REST OF THE FORM
    </Form>
  )}
</Formik

For me it looks like you are doing:对我来说,看起来你正在做:

<Formik {...props}>
  {fn}
</Formik>

instead of doing:而不是这样做:

<Formik {...props}>
  {fn()}
</Formik>

I switched to using the FieldArray component and that does the trick我切换到使用 FieldArray 组件,这可以解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对象无效为React子错误 - Objects are not valid as a React child error “对象作为 React 子项无效”错误 - "Objects are not valid as a React child" error 未捕获的错误:对象作为 React 子项无效。 使用反应流编辑器时 - Uncaught Error: Objects are not valid as a React child. when using react-flow-editor 使用 React.createElement 时对象不是有效的反应子对象? - Objects are not valid react child when using React.createElement? 单击元素时出错:对象作为 React 子项无效 - Error when clicking on an element: Objects are not valid as a React child 添加 div 时,对象作为 React 子对象无效? - Objects are not valid as a React child getting error when adding div? 在对象上循环 // 错误:对象作为 React 子对象无效 - Looping on objects // Error: Objects are not valid as a React child 故事书:不使用反引号 after.attrs 时,对象作为 React 子项无效 - Storybook: Objects are not valid as a React child when not using backticks after .attrs 使用 Gatsby wrapRootElement TypeScript 时获取“对象作为 React 子项无效” - Getting "Objects are not valid as a React child" when using Gatsby wrapRootElement TypeScript ReduxForm - React 错误 - 对象作为 React 子对象无效 - ReduxForm - React Error - Objects are not valid as a React child
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM