T
TanStack2y ago
exotic-emerald

Trigger useQueries fnc in onSuccess of other func, but queryOptions fnc has diff queryKey

export const useFetchDataScreen = (id:number):any => { const assignDataFn = (id: number) => { return queryOptions({ queryKey: ['assignKey',id], queryFn: () => getAssign(id), enabled:!!id }) } const getRoles = () => { return queryOptions({ queryKey: ['getRoleUser], queryFn: () => getRolesUser(), enabled:!!roleId }) } return useQueries({ queries: [assignDataFn(id), getRoles() ], combine:([{data:permissionOfRoles}, roles, allPermissions])=> { //// logic and return data }, }) }; this is first fn to render data and this is the func i need to help: export const useAssignRoleForPermission = (roleId:number) => return useMutation({ mutationFn: (payload:any) => postFn(payload), mutationKey:['post_fn_key'], onSuccess:() => { // i need call useFetchDataScreen in here } }) }
2 Replies
fascinating-indigo
fascinating-indigo2y ago
That is not allowed as per rules of react The question is what you really want to solve! To simply answer your question: queryClient.fetchQuery is what you say you want, but probably not what you need
fair-rose
fair-rose2y ago
You could define the hook like this, maybe?
export const useAssignRoleForPermission = (roleId:number) =>
const queryResult = useFetchDataScreen();

const onSuccess = useCallback(
() => {
// do something with queryResult
},
[queryResult],
)

return useMutation({
mutationFn: (payload:any) => postFn(payload),
mutationKey:['post_fn_key'],
onSuccess,
})
}
export const useAssignRoleForPermission = (roleId:number) =>
const queryResult = useFetchDataScreen();

const onSuccess = useCallback(
() => {
// do something with queryResult
},
[queryResult],
)

return useMutation({
mutationFn: (payload:any) => postFn(payload),
mutationKey:['post_fn_key'],
onSuccess,
})
}
Pretty standard pattern I believe.

Did you find this page helpful?