Skip to content
Advertisement

React Hook Form – Controller – React AsyncSelect – Lodash Debounce | Failed showing loadOptions

React hook not showing option in loadingOptions in AsyncSelect when I use debounce from lodash.

Here is the code.

JavaScript

But, when I am not use debounce like loadOptions={(v) => getAsyncOptions(v)} It works. How to handle this debounce?

Advertisement

Answer

loadOptions expects that a callback parameter is called with the new options or that a promise is returned. Your debounceLoadOptions returns a function wrapped in a debounce function so it does not fulfill either of these requirements.

Given your implementation, I would replace the loadOptions functions declaration like this.

JavaScript

Also there is no need to declare the loadOptions prop as is. It should be noted that the loadOptions prop actually passes two parameters back and the second one is needed in this case to set the new options so the select can be rendered like this:

JavaScript

Here is the corresponding codesandbox to try it out: https://codesandbox.io/s/react-select-async-debounce-usecallback-oeixm?file=/src/App.js

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement