Next.js Discord

Discord Forum

Hydration error

Unanswered
Giant Chinchilla posted this in #help-forum
Open in Discord
Giant ChinchillaOP
Im getting 2 errors:
- Error: Hydration failed because the initial UI does not match what was rendered on the server.
- Warning: Expected server HTML to contain <div> in <div>

Im sure my HTML structure is correct, im using shadcn/ui to create a modal, if i comment out the modal, the errors dissapear.

modal.tsx

"use client";

import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog";

interface Props {
  title: string;
  description: string;
  isOpen: boolean;
  onClose: () => void;
  children?: React.ReactNode;
}

export default function Modal({
  title,
  description,
  isOpen,
  onClose,
  children,
}: Props) {
  const onChange = (open: boolean) => {
    if (!open) {
      onClose();
    }
  };

  return (
    <Dialog open={isOpen} onOpenChange={onChange}>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>{title}</DialogTitle>
          <DialogDescription>{description}</DialogDescription>
        </DialogHeader>
        <div>{children}</div>
      </DialogContent>
    </Dialog>
  );
}


page.tsx
"use client";

import Modal from "@/components/ui/modal";

export default function SetupPage() {
  return (
    <div className="p-4">
      <Modal title="test" description="test" isOpen onClose={() => {}}>
        Test
      </Modal>
    </div>
  );
}

4 Replies

It is because of an internal bug in Next.js, you can read [this thread](https://nextjs-forum.com/post/1136552476217180310).
Installing the latest version of Next.js should fix the problem
@fuma It is because of an internal bug in Next.js, you can read [this thread](https://discord.com/channels/752553802359505017/1136552476217180310). Installing the latest version of Next.js should fix the problem
Giant ChinchillaOP
i created the project today with
npx create-next-app@latest

so i should be on the latest version, but still see the error
@Giant Chinchilla Im getting 2 errors: - Error: Hydration failed because the initial UI does not match what was rendered on the server. - Warning: Expected server HTML to contain <div> in <div> Im sure my HTML structure is correct, im using shadcn/ui to create a modal, if i comment out the modal, the errors dissapear. ts modal.tsx "use client"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; interface Props { title: string; description: string; isOpen: boolean; onClose: () => void; children?: React.ReactNode; } export default function Modal({ title, description, isOpen, onClose, children, }: Props) { const onChange = (open: boolean) => { if (!open) { onClose(); } }; return ( <Dialog open={isOpen} onOpenChange={onChange}> <DialogContent> <DialogHeader> <DialogTitle>{title}</DialogTitle> <DialogDescription>{description}</DialogDescription> </DialogHeader> <div>{children}</div> </DialogContent> </Dialog> ); } ts page.tsx "use client"; import Modal from "@/components/ui/modal"; export default function SetupPage() { return ( <div className="p-4"> <Modal title="test" description="test" isOpen onClose={() => {}}> Test </Modal> </div> ); }
You are force-opening the modal on mount, which is [an existing bug of radix](https://github.com/radix-ui/primitives/issues/1386) that shadcn ui uses. Refactor your code so that the modal is initialised to not be open; if you want to open the modal on mount then open it in an useEffect