Hydration failed due to next/image ??
Answered
Anhinga posted this in #help-forum
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;
Answered by B33fb0n3
you
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
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
4 Replies
@Anhinga Im getting that when trying to print
ts
"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;
you
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
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
Answer
AnhingaOP
Yeah it was I was thinking off :3
happy to help