Last active
December 6, 2021 16:04
-
-
Save RenatoLopes771/83860fc4c4cdf050f7ea844e73a23cf3 to your computer and use it in GitHub Desktop.
React JS: do something on window scroll and conditional render on window size
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
export default function Footer() { | |
const [width, setWidth] = useState(window.innerWidth); | |
useEffect(() => { | |
window.onresize = () => { | |
setWidth(parseInt(window.innerWidth)); | |
} | |
}, []); | |
// return ( | |
// <> | |
// { | |
// // window.innerWidth doesn't rerender | |
// width >= 1024 | |
// ? <DesktopFooter /> | |
// : <MobileFooter /> | |
// } | |
// </> | |
// ); | |
} |
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
function Component () { | |
const [navChange, setNavChange] = useState(false); | |
// https://stackoverflow.com/a/61018017 alt: https://www.youtube.com/watch?v=JMsNslI8KoY | |
useEffect(() => { | |
window.onscroll = () => { | |
setNavChange(window.scrollY >= 80) | |
} | |
}, []); | |
// return ( | |
// <> | |
// <div className={`header-section ${navChange ? "active" : ""}` }> | |
// <p> navbar </p> | |
// </div> | |
// </> | |
// ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment