Next.js Discord

Discord Forum

referenceError: document is not defined

Unanswered
Green Heron posted this in #help-forum
Open in Discord
Green HeronOP
Hi ! i'm getting this error in this code ( i'm not sure it's in this code but but it's more likely )
i'm still a beginner to next ts so feel free to tell me if i'm doing something wrong
import React from 'react'
import type { NextPage } from 'next'
import { BrowserRouter } from 'react-router-dom';
import { MainRoutes } from '@/src/routes';

const Home: NextPage = () => {
    console.log("Home zozo 1")
    return (
        <BrowserRouter>
            <MainRoutes />
        </BrowserRouter>
    )
}

export default Home

here's my mainroutes component
import React from "react"
import { useEffect } from "react"
import { useState } from "react"
import { getLocalStorageItem } from "../utils/localStorage"
import { authorizedRoutes } from "./authorized"
import { unauthorizedRoutes } from "./unauthorized"
import { sharedRoutes } from "./shared"
import { useRoutes } from "react-router-dom"

export const MainRoutes = () => {
    const [authorized, setAuthorized] = useState<boolean>(false)
    const [routes, setRoutes] = useState<any>([])

    useEffect(() => {
        if(getLocalStorageItem("loggedIn") === "true") {
            setAuthorized(true)
        }
    }, [])
    
    useEffect(() => {
        if (authorized) {
            setRoutes(authorizedRoutes)
        } else {
            setRoutes(unauthorizedRoutes)
        }
    }, [authorized])

    const element = useRoutes([...routes, ...sharedRoutes])
    return <>{element}</>
}

15 Replies

is this a client component?
page dir or app dir?
@Green Heron
Don’t use react-router-dom, it conflicts with the nextjs router
Green HeronOP
it's in a src/ dir
Nextjs already has their own client-side routing system
Green HeronOP
i see ok so it might be the problem ?
I never used react-router-dom but theres a chance that react-router-dom uses the document object, so yes it might be
Green HeronOP
damn ok what should i use then ? the basic next js routing system ?
yes
why not?
Green HeronOP
well my compagny is using react router dom so i thought it would've been a good idea to implement the same logic
thanks anyways i'll be changing my routing system
well bad news, you can't use react-router-dom so sorry i can't help ya there :/