react-forms icon indicating copy to clipboard operation
react-forms copied to clipboard

initialValues not working for dynamic data

Open ag13 opened this issue 1 year ago • 1 comments

Scope: FormRenderer

Description: When initialValues is set to a static object like initialValues={{type: {label: "test", value: "1"}}}, it correctly initialises the value but when the same data comes in from an API call and is passed to initialValues prop it fails to initialise it correctly. The useFormApi is correctly getting the initialValues but it does not pass through to the fields.

Schema: fields: [ { component: 'radio', name: 'type', label: 'Type', actions: { options: ['getTypes'], }, }]

Renderer code

<FormRenderer componentMapper={componentMapper} FormTemplate={FormTemplate} actionMapper={actionMapper} schema={schema} onSubmit={asyncSubmit} onCancel={() => console.log('Cancelling')} onReset={() => console.log('Resetting')} initialValues={dataFromApi} //{type: {label: "test", value: "1"}} subscription={{ values: true, initialValues: true }} />

ag13 avatar Sep 29 '24 16:09 ag13

@ag13 , hi can you provide an example in codesandbox? Changing initial values should work unless some fields were already changed, then the form will not reset the values it has to be done via the form API.

Here is a minimal example that changes the values: https://codesandbox.io/p/sandbox/vwch9x. After 1,5 s the value of the text field is changed.

Hyperkid123 avatar Oct 01 '24 10:10 Hyperkid123