fix(pages): add missing useEffect dependency arrays for Escape key handlers#725
fix(pages): add missing useEffect dependency arrays for Escape key handlers#725saksham-1304 wants to merge 2 commits intosugarlabs:mainfrom
Conversation
…ndlers Add [modalImage] dependency array to useEffect hooks handling Escape key for modal closing in NewsDetailPage.tsx and More.tsx. This prevents event listeners from being re-registered on every render, improving performance. - NewsDetailPage.tsx: Move closeImageModal callback before useEffect, add [modalImage, closeImageModal] deps - More.tsx: Add [modalImage] dependency array
🎉 All Checks Passed!
✅ Completed Workflows
🚀 This PR is ready for review and can be safely merged to Great work! Your code meets all quality standards. 👏 |
There was a problem hiding this comment.
Pull request overview
This PR addresses a performance issue by adding missing dependency arrays to useEffect hooks that handle Escape key events for closing image modals. The fix prevents unnecessary re-registration of event listeners on every render.
Key Changes:
- Added dependency arrays to Escape key handler useEffect hooks in both files
- Refactored
NewsDetailPage.tsxto movecloseImageModaldeclaration before its usage in useEffect - Applied
useCallbackwrapper tocloseImageModalinNewsDetailPage.tsx
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/pages/News/NewsDetailPage.tsx |
Moved closeImageModal callback before the useEffect and added [modalImage, closeImageModal] dependency array to the Escape key handler |
src/pages/More.tsx |
Added [modalImage] dependency array to the Escape key handler useEffect |
Comments suppressed due to low confidence (1)
src/pages/More.tsx:114
- Inconsistent implementation pattern between files. In NewsDetailPage.tsx,
closeImageModalis wrapped withuseCallbackand included in the dependency array, but here it's a regular function and omitted from dependencies.
For consistency and to follow React best practices, consider applying the same pattern as NewsDetailPage.tsx: wrap closeImageModal with useCallback and include it in the dependency array.
const closeImageModal = () => {
setModalImage(null);
document.body.classList.remove('overflow-hidden');
};
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…y array Convert closeImageModal to useCallback and move it before the useEffect to satisfy React's exhaustive-deps rule. This ensures the effect properly tracks all dependencies.
🎉 All Checks Passed!
✅ Completed Workflows
🚀 This PR is ready for review and can be safely merged to Great work! Your code meets all quality standards. 👏 |
Fix #726
Description
This PR fixes a performance bug where
useEffecthooks for Escape key handlers were missing dependency arrays, causing event listeners to be re-registered on every render.The Problem
In both
NewsDetailPage.tsxandMore.tsx, the Escape key handleruseEffecthooks lacked dependency arrays:This causes:
The Fix
Added proper dependency arrays to both
useEffecthooks:NewsDetailPage.tsx:closeImageModalcallback declaration before theuseEffectthat uses it[modalImage, closeImageModal]dependency arrayMore.tsx:[modalImage]dependency arrayTesting
npm run lintpassesChecklist