Uh oh!
There was an error while loading. Please reload this page.
Hi, i want achieve scenario: Component, which sets initial data ...
exportconstAppQueryClient=newQueryClient()...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})...constgetSummaryData=async(currency)=>{// my fetcher function which us displayCurrencyconstresponse=awaitfetch(`MYAPI¤cy=${currency}`);constrecord=awaitresponse.json();returnrecord};
...
// Based on data from query, Chart is drawnreturn(<Chart.../>
)Component, which should trigger updating the previous component ...
<SelectonChange={(event)=>{constselectedCurrency=event.target.valueAppQueryClient.setQueryData(['displayCurrency'],selectedCurrency);// here i update displayCurrencyAppQueryClient.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 |
Answered by
skorphil
Dec 7, 2023
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 queryKeyqueryFn: ({queryKey})=>getSummaryData(queryKey[1])// getting value from queryKey}) |
2 replies
Answer selected byskorphil
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Well, I fixed this with passing queryFn arguments from queryKey. IDK why exactly this works, but somehow this fixed an issue: