File tree Expand file tree Collapse file tree 6 files changed +80
-12
lines changed
Expand file tree Collapse file tree 6 files changed +80
-12
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,7 @@ yarn add use-custom-hooks
5151- [ usePrevious] ( https://github.com/aromalanil/useCustomHooks/tree/master/docs#-useprevious )
5252- [ useDebounce] ( https://github.com/aromalanil/useCustomHooks/tree/master/docs#-usedebounce )
5353- [ useDarkMode] ( https://github.com/aromalanil/useCustomHooks/tree/master/docs#-usedarkmode )
54+ - [ useMediaQuery] ( https://github.com/aromalanil/useCustomHooks/tree/master/docs#-usemediaquery )
5455- [ useGeoLocation] ( https://github.com/aromalanil/useCustomHooks/tree/master/docs#-usegeolocation )
5556- [ useLocalStorage] ( https://github.com/aromalanil/useCustomHooks/tree/master/docs#-uselocalstorage )
5657- [ useMousePosition] ( https://github.com/aromalanil/useCustomHooks/tree/master/docs#-usemouseposition )
@@ -129,4 +130,4 @@ SOFTWARE.
129130
130131## ✍🏻 Creator
131132
132- [ Aromal Anil] ( https://aromalanil.me )
133+ [ Aromal Anil] ( https://aromalanil.tech )
Original file line number Diff line number Diff line change 33## 📗 Index
44
55- [ useLocalStorage] ( #-uselocalstorage )
6+ - [ useMediaQuery] ( #-usemediaquery )
67- [ useForm] ( #-useform )
78- [ useStack] ( #-usestack )
89- [ useQueue] ( #-usequeue )
@@ -53,6 +54,38 @@ const LocalValue = () => {
5354
5455</br >
5556
57+ ## 📱 useMediaQuery
58+
59+ Custom hook which listens for a media query and updates the state when the query is active/inactive
60+
61+ ### Usage
62+
63+ ``` jsx
64+ import React from " react" ;
65+ import { useMediaQuery } from " use-custom-hooks" ;
66+
67+ const BottomNav = () => {
68+ const isMobileDevice = useMediaQuery (" (max-width:600px)" );
69+ /*
70+ If isMobileDevice will be true when the screen size is less than
71+ 600px, and false otherwise
72+ */
73+
74+ // Component will only be rendered in mobile devices
75+ return isMobileDevice ? < div className= " bottom-nav" >< / div> : null ;
76+ };
77+ ```
78+
79+ ### Parameters
80+
81+ 1 . ` mediaQuery ` (_ String_ ) : The media query to listen to.
82+
83+ ### Return value
84+
85+ 1 . ` isMediaQueryActive ` (_ any_ ) : A boolean state denoting if the media query is active or not
86+
87+ </br >
88+
5689## 📃 useForm
5790
5891Custom hook to create controlled form component.
Original file line number Diff line number Diff line change 1+ import { useEffect , useState } from 'react' ;
2+
3+ /**
4+ *
5+ * A custom hook which takes a media query as the argument and returns a state
6+ * which will be true when the media query is active and false when not
7+ *
8+ * @example
9+ * const isMobile = useMediaQuery("(max-width:600px)");
10+ *
11+ * @returns {boolean } Is the media query active
12+ */
13+ const useMediaQuery = ( mediaQuery ) => {
14+ const [ doesQueryMatch , setDoesQueryMatch ] = useState ( ( ) => window . matchMedia ( mediaQuery ) . matches ) ;
15+
16+ useEffect ( ( ) => {
17+ const mediaQueryChangeHandler = ( e ) => {
18+ setDoesQueryMatch ( e . matches ) ;
19+ } ;
20+
21+ // Adding event listener for media query change on component mount
22+ window . matchMedia ( mediaQuery ) . addEventListener ( 'change' , mediaQueryChangeHandler ) ;
23+
24+ // Removing event listener component unmount
25+ return ( ) => {
26+ window . matchMedia ( mediaQuery ) . removeEventListener ( 'change' , mediaQueryChangeHandler ) ;
27+ } ;
28+ } , [ ] ) ;
29+
30+ return doesQueryMatch ;
31+ } ;
32+
33+ export default useMediaQuery ;
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ export { default as useToggle } from './hooks/useToggle';
55export { default as useDarkMode } from './hooks/useDarkMode' ;
66export { default as usePrevious } from './hooks/usePrevious' ;
77export { default as useDebounce } from './hooks/useDebounce' ;
8+ export { default as useMediaQuery } from './hooks/useMediaQuery' ;
89export { default as useGeoLocation } from './hooks/useGeoLocation' ;
910export { default as useLocalStorage } from './hooks/useLocalStorage' ;
1011export { default as useMousePosition } from './hooks/useMousePosition' ;
Original file line number Diff line number Diff line change 11{
22 "name" : " use-custom-hooks" ,
3- "version" : " 1.9 .0" ,
3+ "version" : " 1.10 .0" ,
44 "description" : " A collection of Custom Hooks for React." ,
55 "main" : " index.js" ,
66 "scripts" : {
You can’t perform that action at this time.
0 commit comments