Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
Getting warning that a component is changing an uncontrolled input to be controlled while trying to Pass empty array to Autocomplete component-MUI

I am new to react and material UI and struggling to load Autocomplete component options dynamically.

Initially, I am setting empty array as initial option until data is fetched from the database. Once I get my data I update the Autocomplete options with my data and it's working but at the same time I am getting the following warning

Warning: A component is changing an uncontrolled input to be controlled. This is likely caused by the value changing from undefined to a defined value, which should not happen. Decide between using a controlled or uncontrolled input element for the lifetime of the component.

My code

const [listItems, setListItems] = useState([]);

    const {formik, items} = props;

    const handleGetOptionSelected = (option, value) => {
        if (!items) {
            return {};
        }
        return option.id === value.id;
    };

    const handleGetOptionLabel = (option) => {
        if (!items) {
            return 'No Options';
        }
        return option.name;
    };

    useEffect(() => {
        if (items) {
            setListItems(items);
        }
    }, [items]);

return (

        <Autocomplete
            className={classes.autoComplete}
            multiple
            id="tags-standard"
            options={listItems}
            getOptionLabel={(option) => handleGetOptionLabel(option)}
            getOptionSelected={(option, value) => handleGetOptionSelected(option, value)}
            onChange={(event, selectedValues) => {
                formik.setFieldValue(
                    "tag_ids",
                    getCollectionColumn(selectedValues, 'id'),
                    false
                );
            }}
            renderInput={(params) => (
                <TextField
                    {...params}
                    variant="standard"
                    placeholder="select tags"
                    name="tag_ids"
                    error={formik.touched.tag_ids && Boolean(formik.errors.tag_ids)}
                    helperText={formik.touched.tag_ids && formik.errors.tag_ids}
                />
            )}
        />

    );
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

For an input to be controlled, its value must correspond to that of a state variable.

That condition is not initially met in your example because your state is not initially set before onChange. Therefore, the input is initially uncontrolled. Once the onChange handler is triggered for the first time, your formik data state gets set. At that point, the above condition is satisfied and the input is considered to be controlled. This transition from uncontrolled to controlled produces the error seen above.

By initializing formik data structure in the constructor to an empty string the input will be controlled from the start, fixing the issue.

e.g If you want to set name inside formik then initialize it as empty string then change it 
on onChange according to need. Like this:  
  
   constructor(props) {
        super(props);
        this.state = { name: '' }
    }

I don't know your data structure so you can try this on your own by mimicking this. See React Controlled Components for more examples. Also try this solution

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!