我正在嘗試為我使用 reactjs、graphql 和 nextjs 創建的博客設定分頁。雖然反應鉤子作業得很好,但頁碼不可見,我無法弄清楚錯誤來自哪里,因為實際上也沒有可見的錯誤。請用下面的代碼幫我解決這個問題:
頁面/index.jsx
//import type { NextPage } from 'next'
import React, {useState, useEffect} from 'react'
import Head from 'next/head'
import Image from 'next/image'
import {PostCard, Categories, PostWidget, Pagination} from '../components'
import {getPosts} from '../services'
import {FeaturedPosts} from '../sections'
const Home = ({posts}) => {
const [currentPage, setCurrentPage] = useState(1)
const [postsPerPage, setPostsPerPage] = useState(1)
//get current posts
const indexOfLastPost = currentPage * postsPerPage
const indexofFirstPost = indexOfLastPost - postsPerPage
const currentPosts = posts.slice(indexofFirstPost, indexOfLastPost)
return (
<div className="container mx-auto px-10 mb-8">
<Head>
<title>FTS Blog</title>
<link rel="icon" href="/fts_black.png" />
</Head>
<FeaturedPosts />
<div className="grid grid-cols-1 lg:grid-cols-12 gap-12">
<div className='lg:col-span-8 col-span-1'>
{currentPosts.map((post) => <PostCard post={post.node} key={post.title}/>)}
</div>
<Pagination postsPerPage={postsPerPage} totalPosts={posts.length} />
<div className="lg:col-span-4 col-span-1">
<div className="classname lg:sticky relative top-8">
<PostWidget />
<Categories />
</div>
</div>
</div>
</div>
)
}
export default Home
export async function getStaticProps() {
const posts = (await getPosts()) || []
return {
props:{posts}
}
}
組件/分頁.jsx
import React from 'react'
const Pagination = ({postsPerPage, totalPosts}) => {
const pageNumbers = []
for(let i = 1; i <= Math.ceil(totalPosts / postsPerPage); i ){
pageNumbers.push(i)
}
return (
<div>
<nav className="page navigation">
<ul className="flex list-style-none">
{pageNumbers.map(number => {
<li key={number} className="page-item">
<a href="!#" className="page-link">
{number}
</a>
</li>
})}
</ul>
</nav>
</div>
)
}
export default Pagination
組件/Postcard.jsx
import React from 'react'
import moment from 'moment'
import Link from 'next/link'
const PostCard = ({post}) => {
return (
<div className="bg-white shadow-lg rounded-lg p-0 lg:p-8 pb-12 mb-8">
<div className="relative overflow-hidden shadow-md mb-6">
<img
src={post.featuredImage.url}
alt={post.title}
className="object-top h-full w-full rounded-t-lg"
/>
</div>
<h1 className="transition duration-100 text-center mb-8 cursor-pointer hover:text-pink-600 text-3xl font-semibold">
<Link href={`/post/${post.slug}`}>
{post.title}
</Link>
</h1>
<div className="block lg:flex text-center items-center justify-center mb-8 w-full">
<div className="flex items-center justify-center mb-4 lg:mb-0 w-full lg:auto mr-8">
<img src={post.author.photo.url}
alt={post.author.name}
height="30px"
width="30px"
className="align-middle rounded-full"
/>
<p className="inline align-middle text-grey-700 ml-2 text-lg">{post.author.name}</p>
</div>
<div className=" flex items-center justify-center font-medium text-grey-700">
<svg xmlns="http://www.w3.org/2000/svg" className="h-6 w-6 inline mr-2 text-pink-500" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" />
</svg>
<span>
{moment(post.createdAt).format('MMM DD, YYYY')}
</span>
</div>
</div>
<p className="text-center text-lg text-grey-700 font-normal px-4 lg:px-20 mb-8">
{post.excerpt}
</p>
<div className="text-center">
<Link href={`/post/${post.slug}`}>
<span className="transition duration-500 transform hover:-translate-y-1 inline-block bg-pink-600 text-lg font-meduim rounded-full text-white px-8 py-3 cursor-pointer">
Continue Reading
</span>
</Link>
</div>
</div>
)
}
export default PostCard
上面的代碼是錯誤可能來自的區域,謝謝
uj5u.com熱心網友回復:
你沒有在地圖上回傳任何值(Pagination.jsx):
pageNumbers.map(number => {
<li key={number} className="page-item">
<a href="!#" className="page-link">
{number}
</a>
</li>
}
這樣做要么寫return在 li 之前,要么洗掉大括號
這是第二個選項:
pageNumbers.map(number =>
<li key={number} className="page-item">
<a href="!#" className="page-link">
{number}
</a>
</li>
)
這里的作業示例:https ://jsfiddle.net/afoone/nkj17pmh/26/
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/487439.html
