Next.js Discord

Discord Forum

Hydration failed due to next/image ??

Answered
Anhinga posted this in #help-forum
Open in Discord
Avatar
AnhingaOP
Im getting that when trying to print
"use client";

import React from "react";
import Image from "next/image";
import { useTheme } from "next-themes";

import { cn } from "@/lib/utils";

import { Skeleton } from "./ui/skeleton";

function Logo({ className }: { className?: string }) {
  const { theme } = useTheme();

  if (!theme) return <Skeleton className={cn("w-[80%] h-16", className)} />;

  return (
    <div className={cn("w-[80%] relative h-16", className)}>
      <Image
        src={`https://assets.kabila.app/logos/main/${theme === "dark" ? "light" : "dark"}.svg`}
        alt="Kabi Logo"
        className="object-contain"
        fill
      />
    </div>
  );
}

export default Logo;
Image
Answered by B33fb0n3
you useTheme is handled clientside. On the server it will be one value and it may be a different value on the client. That's why there is a hydration error.

Make sure you render this component only when it's mounted. To check if something is mounted create a useState and set it to true when it's mounted (set in useEffect). Of course you can also use pre made hooks like this one: https://usehooks-ts.com/react-hook/use-is-mounted
View full answer

4 Replies

Answer
Avatar
AnhingaOP
Yeah it was I was thinking off :3
Avatar
happy to help