Comments (7)
Infinite Scroll seems like a better option rather than pagination.
React Infinite Scroll - I have used this npm package for some of my projects
from project_graphql_blog.
Hi everyone,
I'm still trying to do the pagination. Could someone help me with this:
This is my main index.js : https://controlc.com/32243536
And this is my index.js (services : https://controlc.com/511f7e29
Should I use useEffect or something like useSWR?
I'm stuck for a long time now. I hope someone will see this and kindly guide me ...
from project_graphql_blog.
Am I the only one right here who want to implement pagination on this blog? :/
from project_graphql_blog.
Hi, I want to implement pageanation too but yet to figure it out
from project_graphql_blog.
I used this : https://www.npmjs.com/package/react-paginate
The code i used:
function Posts({currentPosts}) {
return (
<>
{currentPosts &&
currentPosts.map((post, index) => (
<PostCard key={index} post={post.node} />
))}
</>
);
}
function PaginatedPosts({itemsPerPage}) {
// We start with an empty list of items.
const [currentPosts, setCurrentPosts] = useState(null);
const [pageCount, setPageCount] = useState(0);
// Here we use item offsets; we could also use page offsets
// following the API or data you're working with.
const [itemOffset, setItemOffset] = useState(0);
useEffect(() => {
// Fetch items from another resources.
const endOffset = itemOffset + itemsPerPage;
console.log(`Loading items from ${itemOffset} to ${endOffset}`);
setCurrentPosts(posts.slice(itemOffset, endOffset));
setPageCount(Math.ceil(posts.length / itemsPerPage));
}, [itemOffset, itemsPerPage]);
// Invoke when user click to request another page.
const handlePageClick = e => {
const newOffset = (e.selected * itemsPerPage) % posts.length;
console.log(
`User requested page number ${e.selected}, which is offset ${newOffset}`
);
setItemOffset(newOffset);
};
return (
<>
<Posts currentPosts={currentPosts} />
<ReactPaginate
nextLabel="next >"
previousLabel="< previous"
onPageChange={handlePageClick}
pageRangeDisplayed={3}
marginPagesDisplayed={2}
pageCount={pageCount}
pageClassName="page-item"
pageLinkClassName="page-link"
previousClassName="page-item"
previousLinkClassName="page-link"
nextClassName="page-item"
nextLinkClassName="page-link"
breakLabel="..."
breakClassName="page-item"
breakLinkClassName="page-link"
containerClassName="pagination"
activeClassName="active"
renderOnZeroPageCount={null}
/>
</>
);
}`
Then insert the component:
<PaginatedPosts itemsPerPage={5} />
from project_graphql_blog.
You will have to do some css to customize it (use the classnames of the ReactPaginate component)
from project_graphql_blog.
Create a component and reuse it for the categorie page aswell
from project_graphql_blog.
Related Issues (20)
- Slug must be defined HOT 3
- New posts show 404 on deployment HOT 1
- how to build schema using TS?
- Why is create-next-app command creating .tsx files instead of .js files? HOT 3
- Module not found: Can't resolve './util' HOT 1
- Unhandled runtime error. Help!
- Fetch Code snippets and Image in a Post
- Unhandled Runtime Error when trying to pass PostWidget and Caregories component into index.js
- Unhandled Promise Rejection: SyntaxError: The string did not match the expected pattern. HOT 1
- ClientError: Mutation failed due to permission errors HOT 1
- Can't Post comments on a deployed live site HOT 1
- Not displaying Related posts HOT 1
- TypeError: Cannot read properties of undefined (reading 'document') HOT 3
- Error token verification failed: token contains an invalid number of segments HOT 2
- [{message: "Mutation failed due to permission errors",…}] HOT 2
- Posts fail when change to getPosts HOT 2
- how to fix - Error: error:0308010C:digital envelope routines::unsupported - command(npm run dev) HOT 4
- Email Required Problem & Post Page Pagination & Author Image Can Not Center Issues HOT 1
- Getting 'Minified React error' after deploying app to vercel
- Posts showing up in Recent Posts but not the Post list on home page
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from project_graphql_blog.