Next.js Discord

Discord Forum

HYDRATION FILED

Unanswered
Challangerson posted this in #help-forum
Open in Discord
  return (
    <>
      <div className='header'>
        <div className={`${Navbar ? "act" : ""}`}>
          <div className="logo">Salk<span className="white">Mc</span></div>
          <p className="scroll"> </p>
          <nav className={`nav ${Navbar ? "active" : ""}`}>
            <ul className='ul'>
              <li className='li'><Link href={"/"} className="menu active">MENU GŁÓWNE</Link></li>
              <li className='li'><Link href={"/shop"}>SKLEP</Link></li>
              <li className='li'><Link href={"/rules"}>REGULAMIN</Link></li>
              <li className="li"><Link href={"/gameinfo"}>ROZGRYWKA</Link></li>
            </ul>
          </nav>
          <div className={`menu-toggle ${Navbar ? "active" : ""}`} > <FaAlignRight onClick={() => {toggleNavbar(!Navbar)}}/> </div>
        </div>
      </div>
    </>
  )


Error: Hydration failed because the initial UI does not match what was rendered on the server.

Warning: Expected server HTML to contain a matching <div> in <div>.

227 Replies

Btw fixed this code isn't a problem
Next dont like function without name like empty fc so i used arrowed function
u can add to the html on layout suppressHydrationWarning
<html lang="en" suppressHydrationWarning>
Northeast Congo Lion
I would advise fixing the issue.
Usually this happens because you use illegal html
so a block element inside an inline element div inside p for example
  useEffect(() => {
    window.addEventListener('scroll', () => {
      const header = document.querySelector('.header');

          if(header == null) {
            console.log('error with header');
            return;
          }
      
          header.classList.toggle("sticky", window.scrollY > 250);
    })
  });
this returs me that error
Northeast Congo Lion
i would use refs
for this
instead of just using document.querySelector
return; no need.
just simple if header != null, then run the toggle logic
its not
Northeast Congo Lion
hydration error is usually illegal html.
please show full html where component is causing issue.
  return (
    <>
      <div className="header">
        <div className={`${Navbar ? "act" : ""}`}>
          <p className="logo">Salk<span className="white">Mc</span></p>
          <p className="scroll"> </p>
          <nav className={`nav ${Navbar ? "active" : ""}`}>
            <ul className='ul'>
              <li className='li'><Link href={"/"} className="menu active">MENU GŁÓWNE</Link></li>
              <li className='li'><Link href={"/shop"}>SKLEP</Link></li>
              <li className='li'><Link href={"/rules"}>REGULAMIN</Link></li>
              <li className="li"><Link href={"/gameinfo"}>ROZGRYWKA</Link></li>
            </ul>
          </nav>
          <div className={`menu-toggle ${Navbar ? "active" : ""}`} > 
            <FaAlignRight onClick={() => {toggleNavbar(!Navbar)}}/> </div>
        </div>
      </div>
    </>
  )
}
Northeast Congo Lion
I usually like to work back with hydration error.

E.g remove components till the error is gone, then you know where the issue lies
export default function RootLayout({ children }) {



  return (
    <html lang="en" suppressHydrationWarning>
      <Head />
      <body className={inter.className}>
        <ShowHeader />
            {children}
        <Footer/>
      </body>
    </html>
  )
}
Northeast Congo Lion
hmm i dont know if <Head /> is valid anymore in next 14
I would first remove that.
also remove suppressHydrationWarning from body.
try and find the actual error instead of ignoring it
function Head() {
  return (
    <>
      <title>xxx</title>
      <meta content="width=device-width, initial-scale=1" name="viewport" />
      <link rel="icon" href="/favicon.ico" type="image/gif" />
    </>
  )
}

export default Head
this is my head
Northeast Congo Lion
yeah u dont do this in next14
so how can i do it
Northeast Congo Lion
you have to use metadata
to set title, desc, etc
per page basis.
still that same errro
Northeast Congo Lion
you can also set it on layout.js

export const metadata = {
  title: 'Site Title'
};
yeah it may not be the error, but it is the proper way
disabled it
and still that same
yea
its something in header
Northeast Congo Lion
okay
@Challangerson yea
Northeast Congo Lion
so remove the header code
bit by bit
@Northeast Congo Lion so remove the header code
found it but dont know why
Northeast Congo Lion
show me issue
"use client";

import React from 'react'
import { useState, useEffect } from 'react'
import { FaAlignRight } from 'react-icons/fa';
import Link from 'next/link'
function ShowHeader() {

  const [navbar, toggleNavbar] = useState(false);


  useEffect(() => {
    window.addEventListener('scroll', () => {
      const header = document.getElementsByClassName('header')[0];

      if(header == null) {
        console.log('error with header');
        return;
      }
  
      header.classList.toggle("sticky", window.scrollY > 250);
    })
  });

  return (
    <>
      <div className="header">
        <div className={`${navbar ? "act" : ""}`}>
          {/* <p className="logo">Salk<span className="white">Mc</span></p>
          <p className="scroll"> </p>
          <nav className={`nav ${Navbar ? "active" : ""}`}>
            <ul className='ul'>
              <li className='li'><Link href={"/"} className="menu active">MENU GŁÓWNE</Link></li>
              <li className='li'><Link href={"/shop"}>SKLEP</Link></li>
              <li className='li'><Link href={"/rules"}>REGULAMIN</Link></li>
              <li className="li"><Link href={"/gameinfo"}>ROZGRYWKA</Link></li>
            </ul>
          </nav>
          <div className={`menu-toggle ${Navbar ? "active" : ""}`} > 
            <FaAlignRight onClick={() => {toggleNavbar(!Navbar)}}/>
          </div> */}
        </div>
      </div>
    </>
  )
}

export default ShowHeader
<div className={`${navbar ? "act" : ""}`}>
this cline returns it
but dont know why
Northeast Congo Lion
toggleNavbar(!navbar)
case sensistive.
know it
Northeast Congo Lion
ur state uses navbar but ur using Navbar
just changed it 10 sec ago
its doesnt matter
Northeast Congo Lion
also u can remove
as u can see that code is commented
Northeast Congo Lion
onClick={() => toggleNavbar(!navbar)}
the extra {}
Northeast Congo Lion
<FaAlignRight what is this component
oh
icons from FA
  return (
    <>
      <div className="header">
        <div className={`${navbar ? "act" : ""}`}>
        </div>
      </div>
    </>
  )
this code returns me that error
BUT why?
Northeast Congo Lion
can u remove the Fragment?
<></>
wrapper
still that same error
Northeast Congo Lion
what next version u using
14.1
Northeast Congo Lion
hmm
and if u do this:

return (
    <>
      1
    </>
still same error?
if so, the issue doesn't exist in the header.
not error
this code
<div className="header">
        <div className={`${navbar ? "act" : ""}`}>
        </div>
      </div>
returns error
this code
<div className="header">
      </div>
didnt
cant be div in div
or what
Northeast Congo Lion
okay
<div className={navbar && "act"}>
</div>
what about this?
are u able to put this repo onto git so I can clone it myself?
still thta same error
Northeast Congo Lion
wtf
for the first time vsc returns
error about ? and :
<div className={navbar ? "act" : ""}>
need to change for that
and that still dont work
Northeast Congo Lion
if u do
navbar && act
that is if left hand side true, return right hand
If you used to conditionally omit it with className={condition && value}, pass className={condition ? value : undefined} instead.
Northeast Congo Lion
both work, but idk why the hydration issue flags that
i use both without issues
trying to
my network is really slow
@Northeast Congo Lion
can u give me ur gh?
found it
added
Northeast Congo Lion
Is it private?
yes
but added ur acc
@Northeast Congo Lion did u find something?
fixed if i changed div to section
but why
Northeast Congo Lion
sorry i am eating lunch right now 🙂
iwill check soon
i would review this
@Challangerson i am not getting issue
refresh the page
Northeast Congo Lion
i am
0 error
and scroll down
a little
the other question how can i set a favicon using metadata?
Northeast Congo Lion
yes
u can
use icons
Northeast Congo Lion
1 sec
icons: {
  icon: path_to_favicon 
}
  icons: {
    icon: '/favicon.ico',
    shortcut: ['/favicon.ico']
  }
?
still dont work
Northeast Congo Lion
if inside public, try adding /public/favicon/ico
@Challangerson fixed if i changed div to section
Northeast Congo Lion
if its a header element, instead of using section use header
@Northeast Congo Lion if inside public, try adding `/public/favicon/ico`
public have /public/favicon.ico, app folder have /favicon,ico
everywhere i can i put an favicon
and still
on head in website it didnt chnaged
Northeast Congo Lion
is there a 404
try ctrl shift r
hard-refresh,

check console if any 404
did u read the docs i sent?
ye
Northeast Congo Lion
sorry
try
in proposition for link
its shows a icon
but not on the tab
Northeast Congo Lion
clear ur cahce and check
still that same
Northeast Congo Lion
show full code pls
import './globals.scss'
import { Inter } from 'next/font/google'
import Head from 'next/head';
import ShowHeader from '../../components/header'
import Footer from '../../components/footer'
import favicon from './favicon.ico';

const inter = Inter({ subsets: ['latin'] })

export const metadata = {
  title: 'x',
  description: 'xy',
  icons: {
    icon: './favicon.ico',
    shortcut: './favicon.ico'
  }
}

export default function RootLayout({ children }) {

  return (
    <html lang="en" suppressHydrationWarning>
      <body className={inter.className}>
        <ShowHeader />
            {children}
        <Footer/>
      </body>
    </html>
  )
}
Northeast Congo Lion
are u using ./ or /
doesnt matter
with this and this still dont work
Northeast Congo Lion
uhm
where is favicon.ico?
located
it should be in root /app actually, not public
e.g

/app/favicon.ico
everywhere i can put it
i put it
Northeast Congo Lion
weirdd
can u show me console
log in browser
should see a 404
Northeast Congo Lion
no errors?
then ur ico is loading
but in head option its says fav.ico
Northeast Congo Lion
u dont use <head>
when i change my file name to something like example.ico
it still dont work
means about that
Northeast Congo Lion
if u click
the /favicon.ico
does it go to the actual file?
inside inspect element
if it does, the issue lies with ur browser not loading the icon
because I see no 404 in console, it's loading it into <head>
Northeast Congo Lion
show folders
Northeast Congo Lion
remove favicon.ico
from pulbi cpls
public
its loaded
but not showed
why
Northeast Congo Lion
what doesnt show?
@Challangerson Click to see attachment
from this
/favicon
return me an favicon
but still dont show in tab
Northeast Congo Lion
pls can u push updates to github
i will pulldown and check whats going wrong
something is wrong with ur browser @Challangerson
ohh
on google work
on opera dont
Northeast Congo Lion
opera...
theres ur problem
opera gx >>
@Challangerson opera gx >>
Northeast Congo Lion
hmm
if u go to a caniuse
opera always be red for 1/2 the stuff I search 😂
saying that, ive never actually downloaded it, maybe I will try it out