【发布时间】:2021-07-18 17:54:41
【问题描述】:
我正在像这样在 Gatsby 中设置博客分页:
gatsby-node.js文件:
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const result = await graphql(`
query {
allContentfulPost(sort: { order: ASC, fields: [createdAt] }) {
edges {
node {
postTitle
slug
}
}
}
}
`)
const postsPerPage = 2
const numPages = Math.ceil(posts.length / postsPerPage)
Array.from({ length: numPages }).forEach((_, i) => {
createPage({
path: i === 0 ? `/blog` : `/blog/${i + 1}`,
component: path.resolve("./src/templates/blog.js"),
context: {
limit: postsPerPage,
skip: i * postsPerPage,
numPages,
currentPage: i + 1,
},
})
})
}
然后在blog.js 文件中添加按钮和页码,如下所示:
function Blog({ pageContext, data }) {
const { currentPage, numPages } = pageContext
const isFirst = currentPage === 1
const isLast = currentPage === numPages
const prevPage = currentPage - 1 === 1 ? "" : (currentPage - 1).toString()
const nextPage = (currentPage + 1).toString()
return (
<PaginationContainer>
<FlatButton
item={
!isFirst && {
title: "Previous Page",
icon: "/images/icons/left-arrow.svg",
link: `/blog/${prevPage}`,
}
}
/>
<PageNumbersContainer>
{Array.from({ length: numPages }, (_, i) => (
<PageNumber
item={{
title: `${i + 1}`,
icon: "",
link: `/blog/${i === 0 ? "" : i + 1}`,
key: `pagination-number${i + 1}`,
}}
/>
))}
</PageNumbersContainer>
<FlatButton
item={
!isLast && {
title: "Next Page",
icon: "/images/icons/right-arrow.svg",
link: `/blog/${nextPage}`,
}
}
/>
</PaginationContainer>
)}
现在当没有上一页或下一页时,按钮中的title 消失,只有容器在那里。
我想保持标题和按钮形状保持原样,但使其禁用(不可点击)
感谢您的帮助。
【问题讨论】:
标签: reactjs navigation gatsby contentful