question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

<Field /> and undefined initial value fires error

See original GitHub issue

if value in <Formic initialVales> is undefined React throws error

Warning: A component is changing an uncontrolled input of type text to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component. More info: https://fb.me/react-controlled-components in input (created by Field)

const InnerForm2=()=>(
    <Form className={styles.Form}>
            <Field type={"text"} name={"clusterName"}/>
    </Form>
)
...
             <Formik
                    validate={this.validate as any}
                    validateOnBlur
                    onSubmit={values => console.log(values)}
                    initialValues={{clusterName: undefined,type:1}}
                    component={InnerForm2 as any}
                />
...

with undefined value <input /> rendered without value attribute and first update causes value rendering and this throw error

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

32reactions
mellis481commented, Oct 3, 2018

@jaredpalmer This is fine for fields of type string (I’m using Typescript), you can initialize as an empty string. But for inputs of type number, you have to use an initial value of null or undefined if you don’t want a value to appear in the input by default. And this continues to show the “uncontrolled input” warning.

Here is my code:

export interface InvoiceLevelTotal {
  amount?: number;
  currency?: string;
}

export enum FormKeys {
  Amount = 'amount',
  Currency = 'currency'
}

const formSchema: Yup.ObjectSchema<InvoiceLevelTotal> = Yup.object().shape({
  amount: Yup.number().required('Amount is required'),
  currency: Yup.string().required('Currency is required')
});

const initialFormValues: InvoiceLevelTotal = {
  amount: undefined,
  currency: ''
};

<Formik
  initialValues={initialFormValues}
  onSubmit={(values, { resetForm }) => {
    this.addInvoiceLevelTotal(values);
    resetForm();
  }}
  validationSchema={formSchema}>
    {(formProps: FormikProps<InvoiceLevelTotal>) => (
      <Form>
        <Field
          name={FormKeys.Amount}
          render={({ field, form }: FieldProps<InvoiceLevelTotal>) => (
            <input
              className="input-block-level"
              maxLength="20"
              {...field}
            />
          )}
        />
        <Field
          name={FormKeys.Currency}
          render={({ field, form }: FieldProps<InvoiceLevelTotal>) => (
            <input
              className="input-block-level"
              autoComplete="off"
              {...field}
            />
          )}
        />
      </Form>
    )}
</Formik>
18reactions
kishaningithubcommented, Jun 29, 2019

I fixed it using render approach in javascript

<Field name={name}
  render={({field}) => (
    <Input
        {...field} 
        value={field.value || ''}  
     />
    )}
/>
Read more comments on GitHub >

github_iconTop Results From Across the Web

MUI - Select without initial value throws warnings & errors
A component is changing the uncontrolled value state of Select to be controlled. Elements should not switch from uncontrolled to controlled (or ...
Read more >
using @wire and field value is returning undefined
There's a chance that your callout is happening before the wire method is returning. This component shows a trick I used to help...
Read more >
e.force:createRecord - Populating defaultFieldValues ...
The reason this occurs is down to the null or undefined value being passed to the lookup field. ... Looks like there's a...
Read more >
Angular Debugging "Expression has changed"
We will first start by quickly debugging the error (a video is available for this), and then we will explain the cause and...
Read more >
Documentation
The Signal's value starts out equal to the passed first argument initialValue (or undefined if there are no arguments). The createSignal function returns...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found