Skip to content

Commit

Permalink
Noah/fix window reference (#77)
Browse files Browse the repository at this point in the history
* fix: move window into useEffect

* fix: remove news state dependency from useEffect
  • Loading branch information
oahnh authored May 27, 2024
1 parent 8e0ff6d commit 8745949
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/app/news/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function App() {
setNews(fetchedNews);
};
getNews();
}, [news]);
}, []);
return (
<div className="bg-ivory h-full">
<NavBar />
Expand Down
16 changes: 0 additions & 16 deletions src/app/testing/page.tsx

This file was deleted.

10 changes: 6 additions & 4 deletions src/components/userComponents/Footer/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,14 @@ export default function Footer() {
const [inputValueEmail, setEmailValue] = useState('');
const [showError, setShowError] = useState(false);
const [errorMsg, setErrorMsg] = useState('');

const subbed = () =>
Boolean(JSON.parse(window.sessionStorage.getItem('subscribed') || 'false'));
const [subscribed, setSubscribed] = useState(subbed);
const [subscribed, setSubscribed] = useState(false);

useEffect(() => {
const subbed = () =>
Boolean(
JSON.parse(window.sessionStorage.getItem('subscribed') || 'false'),
);
setSubscribed(subbed);
window.sessionStorage.setItem('subscribed', subscribed.toString());
}, [subscribed]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ function HomeNewsFeed() {
setNews(topThreeNews);
};
getNews();
}, [news]);
}, []);

return (
<div
className="w-full flex flex-row px-2.5 py-20 web:px-56 web:py-28 gap-40
justify-center items-center justify-start"
justify-center items-center"
>
{isWebDevice && (
<img
Expand Down
10 changes: 7 additions & 3 deletions src/context/WindowWidthContext/WindowWidthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import React, {
useEffect,
useContext,
useMemo,
useCallback,
} from 'react';

interface WindowWidthContextProps {
Expand All @@ -29,15 +30,18 @@ export function WindowWidthProvider({
}: {
children: React.ReactNode;
}) {
const [isWeb, setIsWeb] = useState(window.innerWidth >= 1024);
const [isWeb, setIsWeb] = useState(false);
const handleResize = useCallback(() => {
setIsWeb(window.innerWidth >= 1024);
}, []);

useEffect(() => {
const handleResize = () => setIsWeb(window.innerWidth >= 1024);
handleResize();
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize);
};
}, []);
}, [handleResize]);

const windowWidthValue = useMemo(() => ({ isWeb }), [isWeb]);

Expand Down

0 comments on commit 8745949

Please sign in to comment.