-
|
Please, suggest how to use both libraries in the AsyncSelect component. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 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.
-
|
Edit: Solved, see replies I previously used Switching to the non-async select works but when the search updates the results flicker on update which didn't happen with By logging the data from the I guess it's just not intended on the react-select side to externally update If someone knows a solution, please let me know. Here's my usage: const [search, setSearch] = useState("")
const debouncedSearch = useDebounce(search, 250)
const searchQuery = trpc.search.useQuery({ search: debouncedSearch })
return <Select
inputValue={search}
onInputChange={setSearch}
filterOption={null}
options={searchQuery.data}
isLoading={searchQuery.isFetching}
onChange={() => {/* placeholder */}}
/> |
Beta Was this translation helpful? Give feedback.
use their
loadOptionsprop and execute the query imperatively withqueryClient.fetchQuery. You still get caching, deduplication and everything.