jotaijs / jotai-tanstack-query

Jotai integration library for TanStack Query
MIT License
210 stars 14 forks source link

React-query v5 OnSuccess, OnError ... #51

Closed lveillard closed 2 weeks ago

lveillard commented 11 months ago

Hello!

When updating to tanstack query v5 there is a relevant breaking change to consider: https://tkdodo.eu/blog/breaking-react-querys-api-on-purpose

=> onSuccess and the other callbacks will not be available.

In our case, we are currently using the onSuccess of a atomWithQuery to set another atom, something like this:

 const loadAndCacheUnitAtom = atom(null, async (get, set, { unitId, onSuccess }) => {

       const [loadUnitAtomTwo] = atomsWithQuery((g) => {

        return {
            queryKey: ['units', unitId],
            queryFn: async () => query(spaceId, payload),
            onSuccess: (data: any) => {
                if (onSuccess) {
                    const { units } = data;
                    set(setUnitsMapAtom, units);
                }
            },
        };
    });
    return get(loadUnitAtomTwo);
});

the ugly alternative for this in react query when you really need to sync your store is to use useEffect: image

How can we get the same result here and properly do a set to another atom on query sucess?

Thanks!

dai-shi commented 11 months ago

Related: #42 WIP: #45

You are welcome to join this effort.

kalijonn commented 2 weeks ago

This issue is stale. API has been changed to be closer to React Query.

To answer the question about setting, you should derive the atom rather than setting onSuccess.