Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

263
Visualizações
How to delay the API request to only work after finishing typing

i'm trying to use react-debounce-input ,which works great as a stand-alone piece of code but i just can't seem to integrate it with material UI Autocomplete component no matter what.

is there a way of debouncing my search input without it? can i use react-debounce with my current code?

the debounceinput component

          <DebounceInput
            minLength={2}
            debounceTimeout={300}
            type='text'
            placeholder='enter city'
            onChange={(e) => onSearch(e.target.value)}
          />

the entire code :

import React, { useEffect, useState } from 'react';
import { weatherService } from '../service/weather.service.js';
import { DayList } from '../cmps/DayList';
import Autocomplete from '@mui/material/Autocomplete';
import TextField from '@mui/material/TextField';

export const WeatherPage = () => {
  const [searchValue, setSearchValue] = useState(null);
  const [weather, setWeather] = useState([]);
  const [results, setResults] = useState([]);

  const onSearch = async (ev) => {
    const result = await weatherService.getLocationKey(ev);
    console.log('result:', result);
    setWeather(result);
  };

  weather.forEach((city) => {
    city.label = city.LocalizedName;
  });

  const handleChange = (key) => {
    console.log('key:', key);
    setSearchValue(key);
  };

  const onSubmitCity = async (searchValue) => {
    const city = await weatherService.getWeatherResults(searchValue);
    setResults(city);
  };

  return (
    <div>
      <div className='main-layout'>
        <div className='weather-page'>
          <div>WeatherPage</div>

          <Autocomplete
            disablePortal
            id='combo-box-demo'
            options={weather}
            clearOnBlur={false}
            onChange={(event, value) => handleChange(value.Key)} // prints the selected value
            sx={{ width: 300 }}
            renderInput={(params) => (
              <TextField
                {...params}
                label='City'
                type='text'
                placeholder='enter city'
                onChange={(e) => onSearch(e.target.value)}
              />
            )}
          />
          <button onClick={() => onSubmitCity(searchValue)}>Submit</button>
          <section>Lorem ipsum, dolor sit amet consectetur adipisicing</section>
          <button>Toggle Degrees</button>
          <DayList results={results} />
        </div>
      </div>
      <section className='footer'>Footer</section>
    </div>
  );
};

Any help appreciated!

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda