How to fix: 1 of 1 unhandled error Server Error TypeError: Cannot read properties of…
- User
How to fix: 1 of 1 unhandled error Server Error
TypeError: Cannot read properties of null (reading 'useState') This error happened while generating the page. Any console logs will be displayed in the terminal window. Source
components\Transition.jsx (6:50) @ eval
4 | import ParticlesContainer from "./ParticlesContainer"; 5 | import { useGlobe } from '../components/globe/globeContext';
6 | const [isMobileView, setIsMobileView] = useState(false); | ^ 7 | 8 | useEffect(() => { 9 | function handleResize() in the following code: import { motion } from "framer-motion"; import React, { useState, useEffect } from "react"; import { useRouter } from "next/router"; import ParticlesContainer from "./ParticlesContainer"; import { useGlobe } from '../components/globe/globeContext'; const [isMobileView, setIsMobileView] = useState(false);
useEffect(() => { function handleResize() { // Define the width breakpoint for mobile, for example 768px if (window.innerWidth < 768) { setIsMobileView(true); } else { setIsMobileView(false); } }
// Call the handler right away so state gets updated with initial window size handleResize(); // Set up event listener window.addEventListener('resize', handleResize); // Remove event listener on cleanup return () => window.removeEventListener('resize', handleResize);}, []);
const Transition = () => { const [isLoaded, setIsLoaded] = useState(false); const router = useRouter(); const { globeReady } = useGlobe(); let transitionDelay;
const baseDelay = router.pathname === '/' ? 0.2 : 0.2;
const transitionVariants = { initial: { x: "100%", width: "100%", }, animate: { x: "0%", width: "0%", }, exit: { x: ["0%", "100%"], width: ["0%", "100%"], }, };
useEffect(() => { // If we're on the homepage and globe is ready, set loaded to true if (router.pathname === '/' && globeReady) { setIsLoaded(true); } else if(router.pathname!== '/') { setIsLoaded(true); } }, [globeReady, router.pathname]);
if (router.pathname === '/' && !globeReady) { transitionDelay = globeReady ? baseDelay : Infinity; } else if (router.pathname !== '/' && !globeReady) { transitionDelay = baseDelay; }
return ( <> {!isLoaded && ( <motion.div role="status" className="fixed top-0 bottom-0 right-full w-screen h-screen z-30 bg-gradient-to-tl from-violet-900 to-black-900" variants={transitionVariants} initial="initial" animate="animate" exit="exit" transition={{ delay: transitionDelay, duration: 0.6, ease: "easeInOut" }} aria-hidden="true" > <motion.img src="/logo.svg" alt="Descriptive text" style={{ position: 'absolute', top: '50%', left: '50%', transform: 'translate(-50%, -50%)', width: '500px', height: '500px' }} /> <motion.img src="/hand.gif" alt="Descriptive text" style={{ position: 'absolute', top: '70%', left: '50%', transform: 'translate(-50%, -50%)', width: '500px', height: '500px' }} />
- Assistant
The error message you are encountering indicates that you are trying to use
useStateoutside of a component or function body. This is a common mistake when using React hooks. TheuseStatehook must be called within the body of a function component.The issue in your code is that the
useStatecall forisMobileViewis being invoked at the module level, not inside your function component: - User
Server Error
TypeError: Cannot read properties of null (reading 'useEffect') This error happened while generating the page. Any console logs will be displayed in the terminal window. Source
components\Transition.jsx (8:12) @ eval
6 |
7 |8 | useEffect(() => { | ^ 9 | function handleResize() { 10 | // Define the width breakpoint for mobile, for example 768px 11 | if (window.innerWidth < 768) {
- Assistant
- User
- Assistant
It appears that the error you are encountering is occurring on the server side while trying to execute the
useEffecthook within a server-rendered environment (Next.js in this case). The issue here is likely due to attempting to access thewindowobject insideuseEffectbefore checking if the code is being executed on the client side. In a server-rendered environment like Next.js, thewindowobject is not available, as there is no browser context during server-side rendering.To fix this, you need to ensure that any code within that relies on the object or other browser-specific APIs is only run on the client side. This can be done by checking if the object is defined before executing such code.
- User
Server Error
TypeError: Cannot read properties of null (reading 'useEffect') This error happened while generating the page. Any console logs will be displayed in the terminal window. Source
components\Transition.jsx (8:12) @ eval
6 |
7 |8 | useEffect(() => { | ^ 9 | function handleResize() { 10 | // Define the width breakpoint for mobile, for example 768px 11 | if (window.innerWidth < 768) {
- Assistant