Skip to content

Commit 0426568

Browse files
authored
Remove fetchPreviousPage/fetchNextPage from dependency array of useEf… (#13949)
## Summary **What** — What changes are introduced in this PR? Fix to prevent InfiniteList component from falling into an infinite loop of requests. Before: Uploading Grabación de pantalla 2025-11-03 a la(s) 11.34.55 p. m..mov… **Why** — Why are these changes relevant or necessary? The current dependency array of the useEffect in charge of issuing the next/previous fetch calls for pagination, include the functions from `useInfiniteQuery` in its dependency array as well as its body. This causes the component to render infinitely, as the functions reference change on every render. **How** — How have these changes been implemented? - Updated the problematic dependency array to not include these functions. - Included a useEffect and refs for both functions to always have the latest value. **Testing** — How have these changes been tested, or how can the reviewer test the feature? Tested locally that the bug that was happening before the fix was mitigated and i was able to paginate correctly. After fix: https://github.com/user-attachments/assets/17d41bf9-6b3f-437b-b2ce-10e4dcbe248a --- ## Examples Provide examples or code snippets that demonstrate how this feature works, or how it can be used in practice. This helps with documentation and ensures maintainers can quickly understand and verify the change. ```ts // Example usage ``` --- ## Checklist Please ensure the following before requesting a review: - [x] I have added a **changeset** for this PR - Every non-breaking change should be marked as a **patch** - To add a changeset, run `yarn changeset` and follow the prompts - [ ] The changes are covered by relevant **tests** - [x] I have verified the code works as intended locally - [x] I have linked the related issue(s) if applicable --- ## Additional Context Add any additional context, related issues, or references that might help the reviewer understand this PR. closes SUP-2620 --- > [!NOTE] > Prevents InfiniteList from entering an infinite fetch loop by storing pagination functions in refs and removing them from the effect dependency array. > > - **Dashboard**: > - **`packages/admin/dashboard/src/components/common/infinite-list/infinite-list.tsx`**: > - Store pagination callbacks in refs: `fetchNextPageRef`, `fetchPreviousPageRef`, synced via `useEffect`. > - Update `IntersectionObserver` callbacks to call `ref.current()` instead of the functions directly. > - Remove `fetchNextPage`/`fetchPreviousPage` from the `useEffect` dependency array to stabilize observer setup. > - **Changeset**: > - Patch release for `@medusajs/dashboard` describing the fix. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit e21d999. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup>
1 parent 48ab55a commit 0426568

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

.changeset/six-bats-look.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/dashboard": patch
3+
---
4+
5+
Update problematic dependency array of InfiniteList useEffect to avoid infinite loop.

packages/admin/dashboard/src/components/common/infinite-list/infinite-list.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ export const InfiniteList = <
6565
const parentRef = useRef<HTMLDivElement>(null)
6666
const startObserver = useRef<IntersectionObserver>()
6767
const endObserver = useRef<IntersectionObserver>()
68+
const fetchNextPageRef = useRef(fetchNextPage)
69+
const fetchPreviousPageRef = useRef(fetchPreviousPage)
70+
71+
useEffect(() => {
72+
fetchNextPageRef.current = fetchNextPage
73+
fetchPreviousPageRef.current = fetchPreviousPage
74+
}, [fetchNextPage, fetchPreviousPage])
6875

6976
useEffect(() => {
7077
if (isPending) {
@@ -78,7 +85,7 @@ export const InfiniteList = <
7885
(entries) => {
7986
if (entries[0].isIntersecting && hasPreviousPage) {
8087
startObserver.current?.disconnect()
81-
fetchPreviousPage()
88+
fetchPreviousPageRef.current()
8289
}
8390
},
8491
{
@@ -90,7 +97,7 @@ export const InfiniteList = <
9097
(entries) => {
9198
if (entries[0].isIntersecting && hasNextPage) {
9299
endObserver.current?.disconnect()
93-
fetchNextPage()
100+
fetchNextPageRef.current()
94101
}
95102
},
96103
{
@@ -113,8 +120,6 @@ export const InfiniteList = <
113120
endObserver.current?.disconnect()
114121
}
115122
}, [
116-
fetchNextPage,
117-
fetchPreviousPage,
118123
hasNextPage,
119124
hasPreviousPage,
120125
isFetching,

0 commit comments

Comments
 (0)