-
Hello, I have a simple situation here that I was hoping someone could chime in on. PROBLEM I have two React components that use the same custom hook which wraps When a button is clicked, a new network request is made that returns the filtered posts, and this then is stored in RQ's cache (visible in the devtools). However, the QUESTIONS
DEMO |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
with some adjustments started to work perfectly, you could avoid passing params in the query from the tab (since it creates different subscriptions , since post has another key), you can simple react to the selectedUser here my example: queries.ts import { useSnapshot } from 'valtio'
import { selectedUserIdStore, setSelectedUserId } from './store'
...
const fetchPosts: any = async (queryCtx) => {
const {
queryKey: [_key, { selectedUserId } = { selectedUserId: null }],
} = queryCtx
// All posts, otherwise get posts filtered by a user id
const url =
selectedUserId === null ? POSTS_BASE_URL : `${POSTS_BASE_URL}?userId=${selectedUserId}`
const res = await (await fetch(url)).json()
return res
}
export const usePostsQuery = (opts?: any) => {
const selectedUserId = useSnapshot(selectedUserIdStore)
return useQuery(
[...QUERY_KEYS.posts, selectedUserId ],
fetchPosts,
opts?.useQueryOptions,
)
}
tabs.ts export const Tabs = () => {
const selectedUserId = useSnapshot(selectedUserIdStore)
const { status, data } = usePostsQuery()
console.log('Tab', {
selectedUserId: selectedUserId.selectedUserId,
status,
data,
})
return (
<>
{USER_IDS.map(({ id, label }) => (
<button key={id} onClick={() => setSelectedUserId(id)}>
{label}
</button>
))}
</>
)
} |
Beta Was this translation helpful? Give feedback.
with some adjustments started to work perfectly, you could avoid passing params in the query from the tab (since it creates different subscriptions , since post has another key),
you can simple react to the selectedUser
here my example:
queries.ts