Problem in doing paginations between 2 composables
Hi, so I am making 2 composables
usePaginations.ts
`
So the problem is that the computed variables has logger and it logged with correct variables object but actually the query passed empty object and this is only happens when prevPage is invoked. nextPage works beautifully.
Helps, appreciated. Thank you.
3 Replies
Hi @Mandarin Duck. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on the code you've shared, I can see you're implementing a cursor-based pagination system in Nuxt using composables. The issue you're experiencing where
prevPage()
doesn't work correctly while nextPage()
does is likely related to the timing of state updates.
The Problem
Looking at your code, I notice a key difference between your nextPage()
and prevPage()
implementations:
The issue is likely that you're using nextTick()
in the prevPage()
method, which defers the cursor update to the next DOM update cycle. This means that when your query is being constructed, it's using the old (empty) cursor value.
Solution
Try removing the nextTick()
wrapper in your prevPage()
method to make it consistent with nextPage()
:
If you need to ensure reactivity when working with multiple composables, you might want to look at using toRefs()
when passing props between components and composables, as shown in this Stack Overflow solution: How to pass a reactive object with prop values from a component to a composable in vue3 / nuxt3.
Alternative Approach
If you're building a pagination system, you might also consider using Nuxt UI's pagination component which handles a lot of these edge cases for you: Nuxt UI Pagination Component.
For data fetching with pagination, the useLazyAsyncData
composable can be helpful as it doesn't block navigation while data is being fetched: useLazyAsyncData Documentation.
Let me know if this resolves your issue or if you need further assistance!
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@482905063505920022> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
useProducts.ts