Replies: 3 comments 3 replies
-
looking at their promises example, I think one way to do it is to provide queryClient.fetchQuery, which returns the promise. With a given but given that react-select async has its own cache via cacheOptions, maybe there is no need to combine the two? |
Beta Was this translation helpful? Give feedback.
-
I had a problem with making this work. All seemed fine when I wanted to use reactQuery and React Select (not async select) but then my options were not updating accordingly and I had no idea why. This repo example helped me fix the problem after I added |
Beta Was this translation helpful? Give feedback.
-
After reading @TkDodo answer here is my solution const fetchOptions = async (inputValue) => {
const response = await fetch(`https://dummyjson.com/users/search?q=${inputValue}`);
if (!response.ok) throw new Error("Network error");
const data = await response.json();
const options = data.users.map((user) => ({
value: user.id,
label: `${user.firstName} ${user.lastName}`,
}));
return options;
};
function MyComponent(){
const [inputValue, setInputValue] = React.useState("");
const queryClient = useQueryClient();
const { data: profileOptions } = useQuery({
queryKey: ["options", inputValue],
queryFn: () => fetchOptions(inputValue),
});
const loadOptions = debounce(async(inputValue, callback) => {
setInputValue(inputValue);
const newData = await queryClient.fetchQuery(
["options", inputValue],
() => fetchOptions(inputValue)
);
callback(newData)
}, 500);
return (
<AsyncSelect
defaultOptions={profileOptions}
cacheOptions={true}
name={"role"}
placeholder={"Select role"}
loadOptions={loadOptions}
value={null}
onChange={(selected) => {
console.log(
"🚀 ~ onChange ~ selected:",
selected
);
}}
onInputChange={(e) => {
console.log("🚀 ~ onInputChange ~ e:", e);
}}
noOptionsMessage={({ inputValue }) =>
!inputValue
? "Type to search"
: "No options found"
}
loadingMessage={() => "Loading..."}
isDisabled={false}
isSearchable={true}
/>
)
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Please, suggest how to use both libraries in the AsyncSelect component.
Beta Was this translation helpful? Give feedback.
All reactions