downshift icon indicating copy to clipboard operation
downshift copied to clipboard

Apollo with debounce example

Open d4lek opened this issue 8 years ago • 8 comments

Hi, thanks for an awesome library! I'm trying to figure out the correct way of throttling / debouncing the input for Apollo, would be awesome if someone could help to figure it out!

Cheers!

d4lek avatar Feb 26 '18 10:02 d4lek

Hi @davidalekna, I extended the Apollo integration example with debounce feature - https://codesandbox.io/s/20x5plo070.

Basically what you need to do is to store the current query value as a separate state slice and apply debounce on the update (i leverage on onStateChange) and pass that value to Apollo instead.

Hope that helps!

donysukardi avatar Feb 26 '18 15:02 donysukardi

Awesome @donysukardi, thanks a lot!

d4lek avatar Feb 26 '18 17:02 d4lek

@donysukardi the sandbox is showing a 404 😞

@davidalekna you hardly still have an example have you?

Pau1fitz avatar Feb 28 '19 14:02 Pau1fitz

would need the same actually 🙏

Manubi avatar Feb 28 '19 16:02 Manubi

So this should work for you @Manubi

This is what I understood from @donysukardi comment 😁

import { debounce } from 'lodash';

const SearchComponent = () => {
   const [value, setValue] = useState('');

   const updateValue = debounce(value => {
	setVal(value);
    }, 1000);

     return (
	<Downshift onInputValueChange={val => updateValue(val)}>
             // rest of code here. 
             // Use `value` from the hook as the value you pass 
             // as the value to Apollo instead of `InputValue`
        </Downshift>
   );
}

Pau1fitz avatar Feb 28 '19 22:02 Pau1fitz

@Pau1fitz, something like that. However, you might want to consider persisting the debounced callback with useRef. Otherwise, whenever your component re-renders, you'd get a new instance of the function, which could possibly cause some unwanted side effects.

const [currValue, setCurrValue] = useState('');
const debounceSetCurrValue = useRef(null);
if (!debounceSetCurrValue.current) {
  debounceSetCurrValue.current = debounce(setCurrValue, 1000);
}

return (
  <Downshift onInputValueChange={debounceSetCurrValue.current}>
  </Downshift>
);

donysukardi avatar Mar 05 '19 04:03 donysukardi

@Pau1fitz, something like that. However, you might want to consider persisting the debounced callback with useRef. Otherwise, whenever your component re-renders, you'd get a new instance of the function, which could possibly cause some unwanted side effects.

const [currValue, setCurrValue] = useState('');
const debounceSetCurrValue = useRef(null);
if (!debounceSetCurrValue.current) {
  debounceSetCurrValue.current = debounce(setCurrValue, 1000);
}

return (
  <Downshift onInputValueChange={debounceSetCurrValue.current}>
  </Downshift>
);

Awesome answer @donysukardi, thanks!

barclayd avatar Jan 21 '21 00:01 barclayd

Here's a nice snippet for anyone using TypeScript based on @donysukardi's answer:

import type { DebouncedFunc } from 'lodash';
import debounce from 'lodash/debounce';

type LodashDebounceFunc = DebouncedFunc<(downshiftInput: { inputValue?: string }) => void>;

const [userInput, setUserInput] = useState('');
const debounceUserInput = useRef<LodashDebounceFunc | null>(null);

const setValidatedUserInput = (downshiftInput: { inputValue: string }) => {
    setUserInput(downshiftInput.inputValue);
};

if (!debounceUserInput.current) {
    debounceUserInput.current = debounce(setValidatedUserInput, 500);
}

const { getInputProps } = useCombobox({ onInputValueChange: debounceUserInput.current })


return <input {...getInputProps()} />

barclayd avatar Mar 20 '21 23:03 barclayd