You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a form that loads data for the current item on page load, and deleting the item redirects to the previous page.
However, invalidating the query on deletion (in the OnSuccess) causes a 404 error before the redirect due to the original query refetching.
I'm currently invalidating the data after the redirect to avoid this. Should I keep it this way or is there a way to invalidate before the redirect without causing the refetch?
Or should I leave the old data in the cache queryKey: ['taskAreas', 'single', data.taskArea.id] and only invalidate queryKey: ['taskAreas', 'all'] which loads on the page the redirect goes to?
constUpdateTaskAreaForm=({ taskAreaId }: UpdateTaskAreaFormProps)=>{constrouter=useRouter();constqueryClient=useQueryClient();// THIS IS THE useQUERY THAT LOADS ON PAGE LOAD AND REFETCHES DURING MUTATION ONSUCCESSconst{data: taskAreaData}=useTaskAreaById(taskAreaId);const{ notify, notifyError }=useToastNotifications();const{
control,
reset,
handleSubmit,formState: { errors, isSubmitting },}=useZodForm({mode: 'onBlur',defaultValues: {area: '',},schema: taskAreaSchema,});useEffect(()=>{if(taskAreaData){reset({area: taskAreaData.area||'',});}},[taskAreaData,reset]);// Deleteconst{mutate: handleDelete,isPending: isDeleteLoading}=useMutation({mutationFn: async()=>{constresponse=awaitapiDelete<ApiResponse>(`/task-areas/${taskAreaId}`);returnresponse;},onSuccess: async(data,variables,_)=>{notify(`${data.taskArea.area} deleted`);reset({area: ''});/* IF I INVALIDATE HERE BEFORE REDIRECT THE useQUERY REFETCHES */// Redirect firstsetTimeout(()=>{router.push('/admin/task-areas');},300);// Invalidate queries after redirectsetTimeout(async()=>{awaitqueryClient.invalidateQueries({queryKey: ['taskAreas','all']});awaitqueryClient.removeQueries({queryKey: ['taskAreas','single',taskAreaId]});},500);},onError: (error)=>{notifyError(error.message);},});constdeleteButtonHandler=async()=>{handleDelete();};return(<Gridcomponent="form"containeritemdirection="column"justifyContent="flex-start"alignItems="flex-start"spacing={3}onSubmit={handleSubmit(handleFormSubmit)}><Forminputs.... /></Grid>
);
};exportdefaultUpdateTaskAreaForm;
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a form that loads data for the current item on page load, and deleting the item redirects to the previous page.
However, invalidating the query on deletion (in the OnSuccess) causes a 404 error before the redirect due to the original query refetching.
I'm currently invalidating the data after the redirect to avoid this. Should I keep it this way or is there a way to invalidate before the redirect without causing the refetch?
Or should I leave the old data in the cache
queryKey: ['taskAreas', 'single', data.taskArea.id]
and only invalidatequeryKey: ['taskAreas', 'all']
which loads on the page the redirect goes to?Beta Was this translation helpful? Give feedback.
All reactions