admin管理员组文章数量:1406033
React hook not showing option in loadingOptions in AsyncSelect when I use debounce
from lodash.
Here is the code.
const NoteFormMaster = ({ register, control, errors }) => {
const getAsyncOptions = (inputText) => {
return axios
.get(`/v1/user?username=${inputText}`)
.then((response) => {
return response.data.map((user) => ({
value: user.id,
label: user.username,
}));
})
.catch((error) => {
alert(JSON.stringify(error));
});
};
const loadOptions = (inputText) => getAsyncOptions(inputText);
const debounceLoadOptions = _.debounce(loadOptions, 3000);
return (
<Controller
control={control}
name="shareWith"
id="shareWith"
as={
<AsyncSelect
// cacheOptions
loadOptions={(v) => debounceLoadOptions(v)}
defaultValue={[]}
isMulti
isClearable
defaultOptions={[]}
/>
}
/>
)
);
};
But, when I am not use debounce like loadOptions={(v) => getAsyncOptions(v)}
It works. How to handle this debounce?
React hook not showing option in loadingOptions in AsyncSelect when I use debounce
from lodash.
Here is the code.
const NoteFormMaster = ({ register, control, errors }) => {
const getAsyncOptions = (inputText) => {
return axios
.get(`/v1/user?username=${inputText}`)
.then((response) => {
return response.data.map((user) => ({
value: user.id,
label: user.username,
}));
})
.catch((error) => {
alert(JSON.stringify(error));
});
};
const loadOptions = (inputText) => getAsyncOptions(inputText);
const debounceLoadOptions = _.debounce(loadOptions, 3000);
return (
<Controller
control={control}
name="shareWith"
id="shareWith"
as={
<AsyncSelect
// cacheOptions
loadOptions={(v) => debounceLoadOptions(v)}
defaultValue={[]}
isMulti
isClearable
defaultOptions={[]}
/>
}
/>
)
);
};
But, when I am not use debounce like loadOptions={(v) => getAsyncOptions(v)}
It works. How to handle this debounce?
1 Answer
Reset to default 7loadOptions
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.
const loadOptions = React.useCallback(
debounce((inputText, callback) => {
getAsyncOptions(inputText).then((options) => callback(options));
}, 3000),
[]
);
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:
<AsyncSelect
// cacheOptions
loadOptions={loadOptions}
defaultValue={[]}
isMulti
isClearable
defaultOptions={[]}
/>
Here is the corresponding codesandbox to try it out: https://codesandbox.io/s/react-select-async-debounce-usecallback-oeixm?file=/src/App.js
本文标签:
版权声明:本文标题:javascript - React Hook Form - Controller - React AsyncSelect - Lodash Debounce | Failed showing loadOptions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744954266a2634265.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论