-
Hi, i want achieve scenario: Component, which sets initial data ...
export const AppQueryClient = new QueryClient()
...
AppQueryClient.setQueryData(['displayCurrency'], 'usd');
... Component, which needs to update it's state, based on fetched data ...
const { data: displayCurrency } = useQuery({ queryKey: ['displayCurrency'] }); // displayCurrency value used to fetch data
...
const { isLoading, error, data, isFetching, refetch } = useQuery({
queryKey: ['latestRecord'],
queryFn: () => getSummaryData(displayCurrency) // here i'm passing displayCurrency to fetcher
})
...
const getSummaryData = async (currency) => { // my fetcher function which us displayCurrency
const response = await fetch(
`MYAPI¤cy=${currency}`);
const record = await response.json();
return record
};
...
// Based on data from query, Chart is drawn
return (
<Chart
...
/ >
) Component, which should trigger updating the previous component ...
<Select
onChange={(event) => {
const selectedCurrency = event.target.value
AppQueryClient.setQueryData(['displayCurrency'], selectedCurrency); // here i update displayCurrency
AppQueryClient.refetchQueries(['latestRecord']) // here i trigger refetching and expect it will use new displayCurrency
}}
... However this is not working as expected.
Please, help me figure this out, how to make refetch, whith new |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Well, I fixed this with passing queryFn arguments from queryKey. IDK why exactly this works, but somehow this fixed an issue: const { isLoading, error, data, isFetching, refetch } = useQuery({
queryKey: ['latestRecord', displayCurrency], // here I added displayCurrency in queryKey
queryFn: ({queryKey}) => getSummaryData(queryKey[1]) // getting value from queryKey
}) |
Beta Was this translation helpful? Give feedback.
Well, I fixed this with passing queryFn arguments from queryKey. IDK why exactly this works, but somehow this fixed an issue: