How to set Formik InitialValues if field name is coming in props?

Viewed 127

I have to set Formik to something like

initialValues={{ location: initialValue }}>

The name of the field is 'key.location' passed down to as props 'location'. How to set field name as 'key.location' instead of 'location' ?

1 Answers

Yo can just do

initialValues={{ "key.location": initialValue }}

Yo probably tried to do

initialValues={{ key.location: initialValue }}

which will fail. Just making the key a string will solve your problem.

Edit 1:

If the value is passed as a prop with name location, you can do following:

initialValues={{ [location]: initialValue }}
// or
initialValues={{ [props.location]: initialValue }}

Sandbox

Related