ui icon indicating copy to clipboard operation
ui copied to clipboard

Hydration failed for Pagination Components

Open Jahid-Unlock opened this issue 6 months ago • 3 comments

Newly introduced Pagination showing Hydration failed in NextJS 14. 

Component Stack li li ul nav div div div main

it's rendering li elements twice, I think it causing the error.

Jahid-Unlock avatar Dec 27 '23 06:12 Jahid-Unlock

I have the same error. image

sample-tayo avatar Dec 27 '23 07:12 sample-tayo

<Pagination>
        <PaginationContent>
          <PaginationLink href="#" isActive>
            1
          </PaginationLink>
          <PaginationLink href="#">2</PaginationLink>
          <PaginationLink href="#">3</PaginationLink>
        </PaginationContent>
</Pagination>

You have to write like this.

// pagination.tsx
const PaginationLink = ({
  className,
  isActive,
  size = 'icon',
  ...props
}: PaginationLinkProps) => (
  <PaginationItem>
    <a
      aria-current={isActive ? 'page' : undefined}
      className={cn(
        buttonVariants({
          variant: isActive ? 'outline' : 'ghost',
          size
        }),
        className
      )}
      {...props}
    />
  </PaginationItem>
)

Because PaginationLink component was wrapped by PaginationItem Component (this Component is li element).

dayongkr avatar Dec 27 '23 07:12 dayongkr

<Pagination>
        <PaginationContent>
          <PaginationLink href="#" isActive>
            1
          </PaginationLink>
          <PaginationLink href="#">2</PaginationLink>
          <PaginationLink href="#">3</PaginationLink>
        </PaginationContent>
</Pagination>

You have to write like this.

// pagination.tsx
const PaginationLink = ({
  className,
  isActive,
  size = 'icon',
  ...props
}: PaginationLinkProps) => (
  <PaginationItem>
    <a
      aria-current={isActive ? 'page' : undefined}
      className={cn(
        buttonVariants({
          variant: isActive ? 'outline' : 'ghost',
          size
        }),
        className
      )}
      {...props}
    />
  </PaginationItem>
)

Because PaginationLink component was wrapped by PaginationItem Component (this Component is li element).

Thank you, worked.

sample-tayo avatar Dec 27 '23 07:12 sample-tayo

I think the documentation can be updated on this. The code snippet uses </PaginationItem> which leads to this issue

https://ui.shadcn.com/docs/components/pagination

Screenshot 2024-01-09 at 13 35 58

TakuSemba avatar Jan 09 '24 02:01 TakuSemba