Skip to content

Commit

Permalink
fix: move window into useEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
oahnh committed May 26, 2024
1 parent 8e0ff6d commit 444da2b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 23 deletions.
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
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 444da2b

Please sign in to comment.