i'm using local global state with zustand to minimise the amount of queries i make . the issue im having is that i have a custom hook which calls a convex query and sets app state. the app local state is set within a useffect and re-runs every time the payload (result) from the backend changes, but it looks like the useeffect if re-ran on set time interval regardless of the payload changes, so it acts like there is an infinite loop.
export function useMagicRatioData({ }: { } = {}) {
const isUserSignedIn = isAuthenticated && isSignedIn ;
//? queries
const { data: result,isError: isErrorMagicRatioData } = useQueryWithStatus(api.insightsMagicRatio.getCurrentMagicRatioQuery,
isUserSignedIn
? {
}
: "skip"
);
//? mutations
const mutationCurrentMagicRatioNew = useMutation(api.insightsMagicRatio.getCurrentMagicRatioMutation);
//? state management
const setMagicRatioData = useUserConfigStore(state => state.setMagicRatioData);
useEffect(() => {
console.log("🔄 useMagicRatioData",);
if (useMutate) return;
if (!isAuthenticated || !isSignedIn || isConvexLoading || !isClerkLoaded) return;
//? uses data from backend when a new event has been created or deleted
if (result?.success && result?.payload?.personalAllData.combined
&& result?.payload?.partnerAllData.combined
&& !testMode) {
const newData = {
some data...
};
if (newData.personalAllData && newData.partnerAllData && newData.zones) {
setMagicRatioData({
zones: newData.zones,
personalAllData: newData.personalAllData,
partnerAllData: newData.partnerAllData,
lastUpdated: newData.lastUpdated,
isErrorMagicRatio: false,
isPendingMagicRatio: false
});
}
},[result?.payload?.partnerAllData,result?.payload?.personalAllData]);
@fluid raft