T
TanStack10mo ago
manual-pink

setQueryData and getQueryData

Hi! I'm using react query v3. What I want to do is: - I have a list of checkboxes (which internally is a list of objects with a unique ids - let's call them entity1) - Every time a checkbox is pressed I want to do a mutation to a completely different endpoint with some of the fields from above list to create 'entity2' - If the mutation is successfully I call setQueryData(['entity2', variables.entity1Id], data) // by this I want to ensure that they're linekd together - so using entity1 ID as a key for entity2 values - In another custom hook I check if there is data in getQueryData and if so return it directly, otherwise call the mutate from above function (cache) - in another custom hook for each of entity1 Ids I call getQueryData and return all entity2 objects that matches and I do some calculation based on entity2 values - It DOESN'T work fine first time when I select the checkbox but all sequential tick of checkboxes return entity2 data correctly (only the firs time is undefined) I'm doing this because I have only a mutation in BE for creating entity2 but then I want for them to be linked in FE with entity1 IDS and also do some calculation based on entity2 values where entity1 id is selected
1 Reply
uncomfortable-jade
uncomfortable-jade10mo ago
That is complicated!

Did you find this page helpful?