Accessing the same cache from multiple components #1313
-
I'm fetching quizzes from the backend, I have information like the title and the contents, configurations ect...I'm loading it in at once when a sidebar opens:
I then want to access the contents of the quiz within another component - it looks like this I was wondering about best practices, I could pass the data down the tree, or create a context but I felt that's the reason I used react-query - to stop managing loading data in context/state. What I did was just fetch the data from the cache by doing -
but I don't get any of the isLoading, status things. Is this an acceptable way to do it? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
The recommended way would be to just call |
Beta Was this translation helpful? Give feedback.
-
What about a case when I need to see if mutation is loading from different components to avoid duplicate requests? |
Beta Was this translation helpful? Give feedback.
The recommended way would be to just call
useQuery
again, or abstract the useQuery away in a customuseQuizzesQuery
hook and call that whenever you need the data. React query will give you the data from the cache and subscribe you to it, and also perform a background refetch when the component mounts.