-
Notifications
You must be signed in to change notification settings - Fork 0
β‘ Bolt: Throttle scroll event listeners #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -79,14 +79,22 @@ | |||||||||||
|
|
||||||||||||
| useEffect(() => { | ||||||||||||
| AOS.init(); | ||||||||||||
| const state = { isTicking: false }; | ||||||||||||
|
|
||||||||||||
| const handleScroll = (): void => { | ||||||||||||
| const scrollCheck: boolean = window.scrollY > 100; | ||||||||||||
| if (scrollCheck !== scroll) { | ||||||||||||
| setScroll(scrollCheck); | ||||||||||||
| if (!state.isTicking) { | ||||||||||||
| window.requestAnimationFrame(() => { | ||||||||||||
| const scrollCheck: boolean = window.scrollY > 100; | ||||||||||||
| if (scrollCheck !== scroll) { | ||||||||||||
| setScroll(scrollCheck); | ||||||||||||
| } | ||||||||||||
|
Comment on lines
+87
to
+90
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to To optimize this, you can remove the
Suggested change
|
||||||||||||
| state.isTicking = false; | ||||||||||||
| }); | ||||||||||||
| state.isTicking = true; | ||||||||||||
| } | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| document.addEventListener("scroll", handleScroll); | ||||||||||||
| document.addEventListener("scroll", handleScroll, { passive: true }); | ||||||||||||
|
|
||||||||||||
| return () => { | ||||||||||||
| document.removeEventListener("scroll", handleScroll); | ||||||||||||
|
|
@@ -101,9 +109,9 @@ | |||||||||||
|
|
||||||||||||
| const resolvedHeaderStyle = headerStyle || 1; | ||||||||||||
| const resolvedFooterStyle = footerStyle || 1; | ||||||||||||
| const headerRenderer = headerRenderers[resolvedHeaderStyle] ?? headerRenderers[1]; | ||||||||||||
| const headerElement = headerRenderer({ scroll, isSearch, handleSearch }, defaultNavigation); | ||||||||||||
| const footerElement = footerComponents[resolvedFooterStyle] ?? footerComponents[1]; | ||||||||||||
|
|
||||||||||||
| return ( | ||||||||||||
| <> | ||||||||||||
|
|
||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
useEffecthook for this component hasscrollin its dependency array (on line 37), causing the event listener to be re-attached every time the scroll state changes. This is inefficient and is caused by the dependency onscrollwithin thisifcondition.To improve this, you can remove the conditional check and call
setScrolldirectly. React is already optimized to avoid re-renders if the state value is the same. This change will allow you to removescrollfrom theuseEffectdependency array (changing it to[]), ensuring the listener is attached only once.The implementation in
components/elements/BackToTop.tsxis a good example of this pattern.