Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

254
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda