I would prefer making this more general by taking a configuration object with the desired sizes:
interfaceBreakpoints{[key:string]:number;}constisWindowClient=typeofwindow==="object";constuseWindowSize=(breakpoints:Breakpoints)=>{constgetBreakPoint=useMemo((width:number)=>Object.values(breakpoints).find(([_,size])=>width<=size).map(([id])=>id),[breakpoints]);const[windowSize,setWindowSize]=useState(isWindowClient?getBreakPoint(window.innerWidth):undefined);useEffect(()=>{//a handler which will be called on change of the screen resizefunctionsetSize(){setWindowSize(getBreakPoint(window.innerWidth));}if(isWindowClient){//register the window resize listenerwindow.addEventListener("resize",setSize);//unregister the listerner on destroy of the hookreturn()=>window.removeEventListener("resize",setSize);}},[isWindowClient,setWindowSize]);returnwindowSize;}exportdefaultuseWindowSize;
I am new to React and have never used a custom houk. It seems to me that I understood how your code works, but I don't quite understand how to build the logic on a component in order to return inside the choice of 2 components like the navigation bar. Ex: I have a global topBar container, and I have 2 components to return, either a dropdownMenu or from the large tablet, bring up the other navigation component navMenu.
How can I implement the logic against the code you offer?
This does not work on page reload on mobile devices or when you go from one page to another page in mobile device. It returns infinity value in that case.
We're a place where coders share, stay up-to-date and grow their careers.
I would prefer making this more general by taking a configuration object with the desired sizes:
Cool! Additionally, the way you used
Object.values + find + map, refreshed usage of few concepts which I knew but rarely put in practice. Thanks.My aproach is this way: (Someday I'll publish my hooks xD)
So, you can use like this:
Wow this is an comprehensive one. I think I should sit back and give time to imagine all use cases it covers.
Hi,
I am just wondering why you use matchMedia instead of all these complex calculations? media queries in JavaScript too matchMedia
Thanks
Stefan
First time heard about the matchMedia. Thanks for the input.
Just wondering, is there any way to react if screen resized?
Of course, it has an event receiver for onChange.
developer.mozilla.org/en-US/docs/W...
that's cool!
Hello,
I am new to React and have never used a custom houk. It seems to me that I understood how your code works, but I don't quite understand how to build the logic on a component in order to return inside the choice of 2 components like the navigation bar. Ex: I have a global topBar container, and I have 2 components to return, either a dropdownMenu or from the large tablet, bring up the other navigation component navMenu.
How can I implement the logic against the code you offer?
Thank you.
Thank you very much. I was reading this article and blessing you from Perú, keep going :)
Thanks for your kind words, Favio😊
This does not work on page reload on mobile devices or when you go from one page to another page in mobile device. It returns infinity value in that case.