Created
October 10, 2019 19:34
-
-
Save whoisryosuke/24ae29a059c96460371d70b97cee3c2d to your computer and use it in GitHub Desktop.
React / JS - Mobile sidebar - detects if window size is mobile (600px) and tracks sidebar visibility independently
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
import React, { useState, useEffect } from 'react' | |
import Header from './Header' | |
import debounce from '../../helpers/debounce' | |
const MobileHeader = () => { | |
const [isMobile, setMobile] = useState(false) | |
const [isVisible, setVisibility] = useState(false) | |
const resize = () => { | |
let currentHideNav = window.innerWidth <= 600 | |
// Are we mobile? | |
setMobile(currentHideNav) | |
} | |
const toggleVisibility = () => { | |
setVisibility(!isVisible) | |
} | |
useEffect(() => { | |
window.addEventListener("resize", debounce(resize, 250)) | |
resize() | |
}) | |
return ( | |
<Header mobile={isMobile} visible={isVisible} toggleVisibility={toggleVisibility} /> | |
) | |
} | |
export default MobileHeader |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment