Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

adding debounce in autocomplete material ui

import React, { useCallback, useEffect, useState } from "react";
import Autocomplete from '@mui/lab/Autocomplete';
import TextField from '@mui/material/TextField';
import debounce from "lodash/debounce";
import { getOptionsAsync } from "./options";

function App() {
  const [options, setOptions] = useState([]);
  const [inputValue, setInputValue] = React.useState("");
  const getOptionsDelayed = useCallback(
    debounce((text, callback) => {
      setOptions([]);
      getOptionsAsync(text).then(callback);
    }, 200),
    []
  );

  useEffect(() => {
    getOptionsDelayed(inputValue, (filteredOptions) => {
      setOptions(filteredOptions);
    });
  }, [inputValue, getOptionsDelayed]);

  return (
    <Autocomplete
      options={options}
      getOptionLabel={(option) => option.title}
      filterOptions={(x) => x} // disable filtering on client
      loading={options.length === 0}
      onInputChange={(e, newInputValue) => setInputValue(newInputValue)}
      renderInput={(params) => <TextField {...params} label="Combo box" />}
    />
  );
}
Comment

adding debounce in autocomplete material ui

const handleSearch = () => { // ... }
const handleSearchDelayed = debounce(handleSearch, 50);

handleSearchDelayed();
handleSearchDelayed();
handleSearchDelayed();

// handleSearchDelayed is called 3 times but handleSearch will only be called 1 time
// 50ms later
Comment

adding debounce in autocomplete material ui

import debounce from 'lodash/debounce
Comment

adding debounce in autocomplete material ui

import _ from 'lodash';

...

doSearch(text) {
   // Your normal handler here
}

...

// Delay autocomplete until 500 ms after use stops typing
<AutoComplete
  onUpdateInput={_.debounce((value) => doSearch(value), 500)}
  ...
/>
Comment

PREVIOUS NEXT
Code Example
Javascript :: HashRouter 
Javascript :: remove 0 after decimal point in javascript 
Javascript :: Adding User And Hashed Password In ExpressJS 
Javascript :: javascript loop 
Javascript :: Create JavaScript Strings 
Javascript :: append javascript example 
Javascript :: react-moralis 
Javascript :: concat multiple arrays in javascript 
Javascript :: Put Variable Inside JavaScript String 
Javascript :: js array.some 
Javascript :: modulus js 
Javascript :: react 17 
Javascript :: reset value object js 
Javascript :: nested json schema mongoose 
Javascript :: node js add new object to object array json 
Javascript :: netlify page not found on refresh vuejs vue-router 
Javascript :: ? operator in js 
Javascript :: vue cli debugger 
Javascript :: react-query dependent query 
Javascript :: how to change size of button in react native 
Javascript :: javascript initialize two array in one line 
Javascript :: UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON 
Javascript :: where is select value in javascript event object 
Javascript :: reactjs change fill color .svg 
Javascript :: change events 
Javascript :: how to do jest unit test in react 
Javascript :: date compare in js 
Javascript :: @angular-devkit/build-angular <error 
Javascript :: what is morgan in nodejs 
Javascript :: how to change version of npm 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =