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

275
Views
Material-ui Autocomplete defaultValue not working

I tried setting the defaultValue to be "Chairs" and it is not working.

These are the codes:

import React, { useState } from "react";
import TextField from "@mui/material/TextField";
import Autocomplete from "@mui/material/Autocomplete";
import { Items2 } from "./Items2";

export default function ComboBox() {
  const [selected, setSelected] = useState("");

  console.log(selected);

  return (
    <>
      you selected: {selected}
      <br />
      <br />
      <Autocomplete
        disablePortal
        isOptionEqualToValue={(option, value) => option?.label === value?.label}
        id="combo-box-demo"
        options={Items2}
        defaultValue="Chairs"
        fullwidth
        value={selected}
        onChange={(event, value) => setSelected(value)}
        renderInput={(params) => <TextField {...params} label="Items" />}
      />
    </>
  );
}

Also in codesandbox: https://codesandbox.io/s/combobox-material-demo-forked-g88fi?file=/demo.js:0-904

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

0

You just need to set default value for selected state and remove defaultValue from Autocomplete component:

import React, { useState } from "react";
import TextField from "@mui/material/TextField";
import Autocomplete from "@mui/material/Autocomplete";
import { Items2 } from "./Items2";

export default function ComboBox() {
  const [selected, setSelected] = useState("Chairs");

  console.log(selected);

  return (
    <>
      you selected: {selected}
      <br />
      <br />
      <Autocomplete
        disablePortal
        isOptionEqualToValue={(option, value) => option?.label === value?.label}
        id="combo-box-demo"
        options={Items2}
        fullwidth
        value={selected}
        onChange={(event, value) => setSelected(value)}
        renderInput={(params) => <TextField {...params} label="Items" />}
      />
    </>
  );
}

Edit ComboBox Material Demo (forked)

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!