[英]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.