"use client";
const Component = ({ className }) => {
const [open, setOpen] = useState(false);
const [showNewOrganizationDialog, setShowNewOrganizationDialog] =
useState(false);
return (
<Dialog
open={showNewOrganizationDialog}
onOpenChange={setShowNewOrganizationDialog}
>
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>...</PopoverTrigger>
<PopoverContent className="w-[200px] p-0">...</PopoverContent>
</Popover>
<DialogContent>...</DialogContent>
</Dialog>
);
};
This version of code works well. But if I set Popover
property open={true}
it starts throwing hydration exception.
"use client";
const Component = ({ className }) => {
const [open, setOpen] = useState(false);
const [showNewOrganizationDialog, setShowNewOrganizationDialog] =
useState(false);
return (
<Dialog
open={showNewOrganizationDialog}
onOpenChange={setShowNewOrganizationDialog}
>
<Popover open={true} onOpenChange={setOpen}>
<PopoverTrigger asChild>...</PopoverTrigger>
<PopoverContent className="w-[200px] p-0">...</PopoverContent>
</Popover>
<DialogContent>...</DialogContent>
</Dialog>
);
};
Expected server HTML to contain a matching <div> in <div>.
The components diplayed are used in Nextjs app using shadcn/ui.
2
Answers
turn off ssr by dynamic import and set ssr to false in options.
For example:
Another way to avoid hydration error: