Skip to content

Commit

Permalink
fix: upgrade to addEventListener (code100x#1532)
Browse files Browse the repository at this point in the history
* event listener

* Update src/components/Appbar.tsx

---------

Co-authored-by: Sargam <[email protected]>
  • Loading branch information
Kuzuri247 and devsargam authored Dec 6, 2024
1 parent fc0c66b commit f2596bb
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/components/Appbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,31 @@ export const menuOptions = [
{ id: 5, name: 'Watch History', Component: History, href: '/watch-history' },
];

// Custom hook for media query
const useMediaQuery = (query: string) => {
const [matches, setMatches] = useState(false);
//Added Eventlistener
const useMediaQuery = (query: string): boolean => {
const [matches, setMatches] = useState<boolean>(false);

useEffect(() => {
const media = window.matchMedia(query);
if (media.matches !== matches) {
setMatches(media.matches);
}
const listener = () => setMatches(media.matches);
media.addListener(listener);
return () => media.removeListener(listener);
}, [matches, query]);
const listener = (e: MediaQueryListEvent) => setMatches(e.matches);

// Use addEventListener instead of addListener
media.addEventListener("change", listener);

// Set the initial match state
setMatches(media.matches);

// Cleanup function to remove the event listener
return () => {
media.removeEventListener("change", listener);
};
}, [query]);

return matches;
};

export default useMediaQuery;

export const Appbar = () => {
const [isCollapsed, setIsCollapsed] = useState(true);
const [isMounted, setIsMounted] = useState(false);
Expand Down

0 comments on commit f2596bb

Please sign in to comment.