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

297
Views
Material UI Autocomplete options cache in React JS

So I am having problems caching my autocomplete options for the autocomplete component of mui. This is what I currently have:

My fetching function that gets called on client input: (it's throttled)

const handleAutocompleteInput = (event, value) => {
    fetch(`http://localhost:80/players/autocomplete?term=${encodeURIComponent(value)}`)
      .then((response) => response.json())
      .then((names) => {
        setAutoCompleteOptions(names);
      });
  };

My state for the autocomplete options (which is located in a parent component so I pass the setAutoCompleteOptions to the children):

  const [autoCompleteOptions, setAutoCompleteOptions] = useState([])

And my autocomplete component:

<Autocomplete
      fullWidth
      options={autoCompleteOptions}
      classes={classes}
      getOptionLabel={(autoCompleteOptions) => autoCompleteOptions.name}
      onInputChange={_.debounce(
        (event, value) => handleAutocompleteInput(event, value),
        300
      )}
      isOptionEqualToValue={(autoCompleteOptions, value) =>
        autoCompleteOptions.id === value.id
      }
      //getOptionDisabled={(option) =>
      ///  option === timeSlots[0] || option === timeSlots[2]
      //}
      renderInput={(params) => <TextField {...params} placeholder="Име на играч" />}
    />

The objects I receive from the fetch.

{id: some id, name: some name}

I am replacing the autocomplete options entirely on every response I get from the fetch function. How can I cache them and have only one instance of the objects I receive.

about 4 years ago · Santiago Gelvez
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!