How to load data into dynamically created forms in a page with multiple forms? #2562
-
Hi All, anyone ever tried creating a form page created dynamically? I am having trouble trying to set initial values for when I want to load data into my forms. I am having trouble because I have multiple forms in one page and not sure how to setup initial values for it. I have a code sample here in codesandbox: https://codesandbox.io/s/nice-snowflake-nw79k I basically want to load the data I have in ./mock/data.json into my form but is unsuccessful so far. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey @gidsdangil there can be multiple ways of doing this - loading some data (async) and then rendering the form. These are some I would consider if I fall into such a scenario
// load the data async and set a state to track that the data has been loaded
fetch('data.json').then(() => setHasData(true))
{
data && <RenderForm data={data} />
}
// RenderForm
const form = useFormik({
initialValues: props.data
})
// initialize the form with the default initialValues
const form = useFormik({
initialValues
})
// fetch the async data and update the values in the form
// https://formik.org/docs/api/formik#setvalues-fields-reactsetstateaction-field-string-any--shouldvalidate-boolean--void
fetch('data.json').then(() => setValues(data)) hope this answers your question 😄 |
Beta Was this translation helpful? Give feedback.
Hey @gidsdangil there can be multiple ways of doing this - loading some data (async) and then rendering the form. These are some I would consider if I fall into such a scenario