Error Driving Me Crazy
Answered
Siamese Crocodile posted this in #help-forum
Siamese CrocodileOP
I'm doing an assignment for school, and I have a hydration error. It only occurs when I add the conditional rendering, without it, it seems to be fine. I've been trying to figure this out for so long, but I don't understand where im going wrong. If someone can help it would be appreciated.
This is the entire component: https://pastebin.com/sq3VPbF6
This is the entire component: https://pastebin.com/sq3VPbF6
34 Replies
Siamese CrocodileOP
Another thing is, it only occurs when i refresh the page after logging in, if I don't refresh, the hydration error doesn't occur.
@Siamese Crocodile I'm doing an assignment for school, and I have a hydration error. It only occurs when I add the conditional rendering, without it, it seems to be fine. I've been trying to figure this out for so long, but I don't understand where im going wrong. If someone can help it would be appreciated.
This is the entire component: https://pastebin.com/sq3VPbF6
make this hook
then in your page
import { useCallback, useEffect, useRef } from 'react'
export function useIsMounted(): () => boolean {
const isMounted = useRef(false)
useEffect(() => {
isMounted.current = true
return () => {
isMounted.current = false
}
}, [])
return useCallback(() => isMounted.current, [])
}then in your page
const MainNav = () => {
let router = useRouter();
let token = readToken();
const [searchField, setSearchField] = useState('');
const [isExpanded, setIsExpanded] = useState(false);
const [searcHistory, setSearchHistory] = useAtom(searchHistoryAtom);
const isMounted = useIsMounted()
const hasUser = token && isMounted
.....
return (
....
<Nav>
{hasUser && (
<NavDropdown
title="User Name"
id="basic-nav-dropdown"
>
....
)Siamese CrocodileOP
idk if I'd be allowed to do it this way because its a school assignment
look like the token is not available when the page is rendering on server so it render different result than on browser and causing the hydration error
Siamese CrocodileOP
ohh ok i see.
@Siamese Crocodile idk if I'd be allowed to do it this way because its a school assignment
what way? you could add a useEffect in MainNav too
just make sure the nav render on browser only
@Ray what way? you could add a useEffect in MainNav too
Siamese CrocodileOP
if it doesn't say to use a hook we usually aren't supposed to use it.
@Siamese Crocodile if it doesn't say to use a hook we usually aren't supposed to use it.
well it is using hook already
useRouter, useState useAtomall are hook
Siamese CrocodileOP
yeah i meant if it doesn't specify a hook, you aren't supposed to use one
so are useEffect and useRef allowed?
Siamese CrocodileOP
no they are not
also, my friend was able to have no hydration errors without using a hook so im not too sure whats wrong with my code.
try
!token && typeof window !== 'undefined'and
token && typeof window !== 'undefined'if it doesn't work, I need to see the code on
readTokenor just do
if (!token) return null
return (
<>
<Navbar
className="fixed-top navbar-light bg-body-tertiary"
expand="lg"
expanded={isExpanded}
>
...
</>
)Siamese CrocodileOP
He sen't his code, we have very similar things, but mine still get a hydration error
@Siamese Crocodile He sen't his code, we have very similar things, but mine still get a hydration error
so what is the different between yours and his code
Siamese CrocodileOP
some styling stuff, the overall structure is kinda the same, we did it the assignment says to do it.
I think something else is wrong
in another component or something
Siamese CrocodileOP
yea
oh then I dont know
Siamese CrocodileOP
yeah its something else, proabably something to do with JWT, ill figure it out, thanks for trying though.
I don't think jwt will cause hydration error
Siamese CrocodileOP
fixed it 🤦â€â™‚ï¸
Siamese CrocodileOP
i was missing a literal word in my route guard
Answer
Siamese CrocodileOP
and it messed everything up