In main layout component there is 2 different component which is MobileHeader and DesktopHeader when resize the browser it rerender these components every resize action. How to stop to render again and again when resize the browser ?
function useWindowSize() {
const [size, setSize] = useState([0, 0]);
useLayoutEffect(() => {
function updateSize() {
setSize([window.innerWidth, window.innerHeight]);
}
window.addEventListener('resize', updateSize);
updateSize();
return () => window.removeEventListener('resize', updateSize);
}, []);
return size;
}
const MainLayout = () => {
const [w,h] = useWindowSize();
return(
<>
{w > 992 ? <DesktopHeader/> : <MobileHeader/>}
</>
)
}