Created
March 5, 2021 14:00
-
-
Save ShahrozTanveer/eee7f75879e4e88795f1061a387fc737 to your computer and use it in GitHub Desktop.
Handle responsiveness using javascript in React Functional component (with hooks)
This file contains hidden or 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
import React,{useState,useEffect} from 'react' | |
export default function App(){ | |
const [isMobile, setIsMobile] = useState(false) | |
useEffect(() => { | |
window.addEventListener('resize', handleWindowResize); | |
return () => { | |
window.removeEventListener('resize', handleWindowResize); | |
} | |
}, []) | |
const handleWindowResize = () => { | |
setIsMobile(window.innerWidth < 480) | |
} | |
return(<> | |
{isMobile? | |
<MobileLayoutComponent /> | |
: | |
<DesktopLayoutComponent /> | |
} | |
</>) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment