Apollo with debounce example
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!
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!
Awesome @donysukardi, thanks a lot!
@donysukardi the sandbox is showing a 404 😞
@davidalekna you hardly still have an example have you?
would need the same actually 🙏
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, 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>
);
@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!
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()} />