fix(active-index): resets index if activeIndex doesn't exist#9
Conversation
| } = useGallery() | ||
|
|
||
| useEffect(() => { | ||
| if (!galleryItems[activeIndex]) goToIndex(0) |
There was a problem hiding this comment.
I think we can lose the useEffect, and integrate this logic into the actual goToIndex function. That thing should be updated to be a bit more defensive anyway. Inside that function, maybe instead of just accepting that index is defined, let's first check that it's valid, and if not, set to zero.
I think with this useEffect solution, we'll still briefly get a warning, since side-effects run after the render phase.
There was a problem hiding this comment.
Agree with this one, otherwise we'll always have to resort to this useEffect trick.
|
A bit of back and fourth with @andrewrubin on this one.
|
|
Agreed, thanks for entertaining the idea though. Tested on my end and working. Here's a screencap where Screen.Recording.2023-09-21.at.5.44.18.PM.mp4 |
|
Merging this one |
Description
We discovered an edge case where the
activeIndexdid not exist. This often happens if the amount ofGalleryItemschanges based on the viewport width.Solution
Adds a check to see if the activeIndex exists, if it doesn't run the
goToIndex()function and default to 0.Note
Fixes #3