onSuccess not trigeered when useQuery has staleTime #3019
-
I have a Form component whose initial data I am setting like so: const EditProfileForm: React.FC = ({ section }) => {
const [state, dispatch] = useReducer(
updateProfileReducer,
PROFILE_INITIAL_STATE,
);
const setInitalState = (getApiData: GetUserDetailsQueryResp) => {
dispatch(
updateProfileAction({
data: getApiData.getUserDetails,
reset: getApiData.getUserDetails,
}),
);
};
const { data, isLoading } = useGetUserDetailsQuery(setInitalState);
const onChange = (formData) => {
// update the state by dispatching the data
}
return // My component;
} The The problem is the onSuccess callback doesn't trigger when query has staleTime, thus not setting my initialState. Any way to avoid this problem (without using useEffect) ? the query function for reference: export const useGetUserDetailsQuery = (
onSuccess?: (stateData: GetUserDetailsQueryResp) => void,
): UseQueryResult<GetUserDetailsQueryResp, ErrorResponse> => {
return useGraphqlQuery('get-user-details', GET_USER_DETAILS, {
staleTime: 600000,
onSuccess: onSuccess,
});
}; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
my suggestion would be to keep server and client state separate, thus not syncing the initial state to the local state. But its usually a bigger refactoring if you do it that way already.
we've discussed this to some extent: |
Beta Was this translation helpful? Give feedback.
my suggestion would be to keep server and client state separate, thus not syncing the initial state to the local state. But its usually a bigger refactoring if you do it that way already.
useEffect
is indeed the preferred solution for the problem. onSuccess is tied to a network request, just likeonError
.data
returned from useQuery is referentially stable, so all you need is:we've discussed this to some extent: