digidem / comapeo-mobile

The next version of Mapeo mobile
GNU General Public License v3.0
5 stars 1 forks source link

fix: stores previous project #500

Closed ErikSin closed 2 months ago

ErikSin commented 2 months ago

closes #489

achou11 commented 2 months ago

My understanding was that React Query has functionality to handle this kind of need pretty elegantly. I was able to achieve the same thing by solely using their query placeholder data functionality. Curious if you think it makes sense to use this in this situation.

export function useProject(projectId?: string) {
  const api = useApi();

  return useQuery({
    queryKey: [PROJECT_KEY, projectId],
    queryFn: async () => {
      if (!projectId) throw new Error('Active project ID must exist');
      return api.getProject(projectId);
    },
    enabled: !!projectId,
    // This is the part I added
    placeholderData: previousData => {
      return previousData;
    },
  });
}
ErikSin commented 2 months ago

Sweet! Yeah that worked. way too many hours went into this for such a simple fix....