有没有办法在Next.js中与MUI的分页组件一起使用Link?

huangapple go评论58阅读模式
英文:

Is there a way to use a Next.js Link together with MUI's Pagination component?

问题

I use Next.js and the MUI design system in my project. In one of the components I use a table with multiple pages and use the Pagination component from the MUI to view different pages in the table. At the moment I use the a tag but I want to be able to use Next.JS' Link component instead.

This is what my working code looks like:

 <PaginationItem
   component={'a'}
   href={`/tunes${item.page === 1 ? '' : `?page=${item.page}`}`}
   {...item}
  />

This is what I tried to write:

 <PaginationItem
   component={Link}
   href={`/tunes${item.page === 1 ? '' : `?page=${item.page}`}`}
   {...item}
  />

I get this error message: Error: Multiple children were passed to <Link> with 'href' of '/tunes?page=0' but only one child is supported. Open your browser's console to view the Component stack trace.

So the problem seems to be that MUI Pagination component fits two children inside Link, although the Link tag only tolerates one. It looks like this in the console (when using the 'a' tag):

<a
  class='MuiButtonBase-root MuiPaginationItem-root MuiPaginationItem-sizeSmall MuiPaginationItem-text MuiPaginationItem-circular MuiPaginationItem-textPrimary MuiPaginationItem-page css-1vj5it5-MuiButtonBase-root-MuiPaginationItem-root'
  tabindex='0'
  href='/tunes?page=3'
  aria-label='Go to page 3'
>
  3
  <span class='MuiTouchRipple-root css-8je8zh-MuiTouchRipple-root'></span>
</a>

Is there a way to combine Next.js' Link component with MUI's Pagination component? Maybe wrap the items inside somehow, or make Link accept more than one child inside it?

英文:

I use Next.js and the MUI design system in my project. In one of the components I use a table with multiple pages and use the Pagination component from the MUI to view different pages in the table. At the moment I use the a tag but I want to be able to use Next.JS' Link component instead.

This is what my working code looks like:

 &lt;PaginationItem
   component={&#39;a&#39;}
   href={`/tunes${item.page === 1 ? &#39;&#39; : `?page=${item.page}`}`}
   {...item}
  /&gt;

This is what I tried to write:

 &lt;PaginationItem
   component={Link}
   href={`/tunes${item.page === 1 ? &#39;&#39; : `?page=${item.page}`}`}
   {...item}
  /&gt;

I get this error message: Error: Multiple children were passed to &lt;Link&gt; with &#39;href&#39; of &#39;/tunes?page=0&#39; but only one child is supported https://nextjs.org/docs/messages/link-multiple-children Open your browser&#39;s console to view the Component stack trace.

So the problem seems to be that MUI Pagination component fits two children inside Link, although the Link tag only tolerates one. It looks like this in the console (when using the a tag):

&lt;a
  class=&#39;MuiButtonBase-root MuiPaginationItem-root MuiPaginationItem-sizeSmall MuiPaginationItem-text MuiPaginationItem-circular MuiPaginationItem-textPrimary MuiPaginationItem-page css-1vj5it5-MuiButtonBase-root-MuiPaginationItem-root&#39;
  tabindex=&#39;0&#39;
  href=&#39;/tunes?page=3&#39;
  aria-label=&#39;Go to page 3&#39;
&gt;
  3
  &lt;span class=&#39;MuiTouchRipple-root css-8je8zh-MuiTouchRipple-root&#39;&gt;&lt;/span&gt;
&lt;/a&gt;

Is there a way to combine Next.js' Link component with MUI's Pagination component? Maybe wrap the items inside <PaginationItem> somehow, or make Link accept more than one child inside it?

答案1

得分: 1

你应该将PaginationItem包装在Link组件内,而不是相反的方式。

这是如何做的:

<Pagination
  count={count}
  page={Number(page)}
  renderItem={(item) => (
    <Link href={`/tunes${item.page === 1 ? '' : `?page=${item.page}`}`}>
      <PaginationItem {...item} />
    </Link>
  )}
/>
英文:

You should wrap the PaginationItem inside the Link component, rather than the other way around.

Here's how you can do it:

&lt;Pagination
  count={count}
  page={Number(page)}
  renderItem={(item) =&gt; (
    &lt;Link href={`/tunes${item.page === 1 ? &#39;&#39; : `?page=${item.page}`}`}&gt;
      &lt;PaginationItem {...item} /&gt;
    &lt;/Link&gt;
  )}
/&gt;

huangapple
  • 本文由 发表于 2023年2月9日 03:28:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定