Skip to content

Commit 5cb97b1

Browse files
committed
feat(hooks): add useMediaQuery hook
1 parent 485058b commit 5cb97b1

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/hooks/useMediaQuery.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// utils/useMediaQuery.js
2+
import { useState, useEffect } from "react";
3+
4+
export function useMediaQuery(query) {
5+
const mediaQuery = window.matchMedia(query);
6+
const [matches, setMatches] = useState(mediaQuery.matches);
7+
8+
useEffect(() => {
9+
const handleResize = (event) => {
10+
setMatches(event.matches);
11+
};
12+
13+
mediaQuery.addEventListener("change", handleResize);
14+
return () => mediaQuery.removeEventListener("change", handleResize);
15+
}, [mediaQuery]);
16+
17+
return matches;
18+
}

0 commit comments

Comments
 (0)