-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FE-48 ✨ EmotionSelector 컴포넌트 동적 크기 변경 구현
useMediaQuery 훅 생성: 화면의 크기가 변경될 때마다 리스너 추가 및 제거
- Loading branch information
Showing
2 changed files
with
36 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useState, useEffect } from 'react'; | ||
|
||
function useMediaQuery(query: string): boolean { | ||
const [matches, setMatches] = useState(false); | ||
|
||
useEffect(() => { | ||
const media = window.matchMedia(query); | ||
if (media.matches !== matches) { | ||
setMatches(media.matches); | ||
} | ||
const listener = () => setMatches(media.matches); | ||
media.addEventListener('change', listener); | ||
return () => media.removeEventListener('change', listener); | ||
}, [matches, query]); | ||
|
||
return matches; | ||
} | ||
|
||
export default useMediaQuery; |