Skip to content

Commit

Permalink
Solve the gatsby window error in navbar
Browse files Browse the repository at this point in the history
  • Loading branch information
Gagansankhla committed Aug 22, 2023
1 parent 8021a8d commit c5ca2dc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
17 changes: 11 additions & 6 deletions uli-website/src/components/molecules/Footer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import { navigate } from "gatsby";

export default function Footer() {

const [windowWidth, setWindowWidth] = useState(window.innerWidth);
const [windowWidth, setWindowWidth] = useState(
typeof window !== "undefined" ? window.innerWidth : 0
);

useEffect(() => {
const handleResize = () => {
setWindowWidth(window.innerWidth);
};
window.addEventListener("resize", handleResize);
return () => window.removeEventListener("resize", handleResize);
if (typeof window !== "undefined") {
const handleResize = () => {
setWindowWidth(window.innerWidth);
};

window.addEventListener("resize", handleResize);
return () => window.removeEventListener("resize", handleResize);
}
}, []);

const isSmallScreen = windowWidth <= 768;
Expand Down
16 changes: 10 additions & 6 deletions uli-website/src/components/molecules/NavBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ const NavBarByLang = {
export default function NavBar() {
const [langOption, setLangOption] = useState(undefined);
const location = useLocation();
const [windowWidth, setWindowWidth] = useState(window.innerWidth);
const [windowWidth, setWindowWidth] = useState(
typeof window !== "undefined" ? window.innerWidth : 0
);

useEffect(() => {
console.log(`pageLoad `);
Expand All @@ -50,12 +52,14 @@ export default function NavBar() {
lang = "en";
}
setLangOption(lang);
const handleResize = () => {
setWindowWidth(window.innerWidth);
};
if (typeof window !== "undefined") {
const handleResize = () => {
setWindowWidth(window.innerWidth);
};

window.addEventListener("resize", handleResize);
return () => window.removeEventListener("resize", handleResize);
window.addEventListener("resize", handleResize);
return () => window.removeEventListener("resize", handleResize);
}
}, [location.pathname]);

const isSmallScreen = windowWidth <= 768;
Expand Down

0 comments on commit c5ca2dc

Please sign in to comment.