Skip to content

Instantly share code, notes, and snippets.

@RenatoLopes771
Last active December 6, 2021 16:04
Show Gist options
  • Save RenatoLopes771/83860fc4c4cdf050f7ea844e73a23cf3 to your computer and use it in GitHub Desktop.
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
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 />
// }
// </>
// );
}
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