Skip to content

refactor: use functional state updates in hero carousel callbacks#1021

Open
mvanhorn wants to merge 1 commit intoMODSetter:devfrom
mvanhorn:osc/951-functional-state-carousel
Open

refactor: use functional state updates in hero carousel callbacks#1021
mvanhorn wants to merge 1 commit intoMODSetter:devfrom
mvanhorn:osc/951-functional-state-carousel

Conversation

@mvanhorn
Copy link
Copy Markdown
Contributor

@mvanhorn mvanhorn commented Mar 28, 2026

Summary

Refactored carousel navigation callbacks to use functional state updates, making them stable references that don't change on every index update.

Why this matters

goTo, goToPrev, and goToNext had activeIndex in their dependency arrays. Every slide change recreated these callbacks, causing unnecessary re-renders downstream. Functional updates read the previous state directly, so the callbacks become stable.

Changes

  • surfsense_web/components/ui/hero-carousel.tsx: Converted goTo, goToPrev, goToNext to use setActiveIndex((prev) => ...). Removed activeIndex from the autoplay useEffect dependency array (the setTimeout callback already used a functional update). All three useCallback hooks now have empty dependency arrays.

Testing

Carousel behavior is identical - same navigation, same animation direction logic. The direction ref is set inside the functional updater before returning the new index.

Fixes #951

Retargeted to dev per @MODSetter's request.

High-level PR Summary

This PR refactors the hero carousel navigation callbacks (goTo, goToPrev, goToNext) to use functional state updates instead of depending on activeIndex. This optimization makes the callback references stable, preventing unnecessary re-renders in child components whenever the slide index changes. The autoplay effect dependency array is also cleaned up by removing activeIndex since the timeout callback already uses functional updates.

⏱️ Estimated Review Time: 5-15 minutes

💡 Review Order Suggestion
Order File Path
1 surfsense_web/components/ui/hero-carousel.tsx

Need help? Join our Discord

Analyze latest changes

Copy link
Copy Markdown

@recurseml recurseml bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review by RecurseML

🔍 Review performed on 947def5..490510f

✨ No bugs found, your code is sparkling clean

@MODSetter
Copy link
Copy Markdown
Owner

@mvanhorn Thanks. Can you fix the merge conflicts?

Switched goTo, goToPrev, and goToNext from depending on activeIndex
to using functional setState updates. Removed activeIndex from the
autoplay effect dependency array since the timer callback already
uses a functional update. Callbacks are now stable references.

Fixes MODSetter#951
@mvanhorn mvanhorn force-pushed the osc/951-functional-state-carousel branch from 490510f to 298e658 Compare March 28, 2026 14:12
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 28, 2026

@mvanhorn is attempting to deploy a commit to the Rohan Verma's projects Team on Vercel.

A member of the Team first needs to authorize it.

@mvanhorn
Copy link
Copy Markdown
Contributor Author

Rebased onto main and resolved merge conflicts in 298e658.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants