Skip to content

Instantly share code, notes, and snippets.

@kapscudi
Forked from viswanathsaj/LatestPost.js
Created January 7, 2021 10:16
Show Gist options
  • Save kapscudi/8b67e4daccb4e8d15cc6faffd3e5981b to your computer and use it in GitHub Desktop.
Save kapscudi/8b67e4daccb4e8d15cc6faffd3e5981b to your computer and use it in GitHub Desktop.
Implementation for a load more button in React/NextJS using Ghost CMS
// Make sure you are pulling ALL your posts from the ghost API [No limit]
function LatestPost (props) {
const [ postNum, setPostNum] = useState(3); // Default number of posts dislplayed
function handleClick() {
setPostNum(prevPostNum => prevPostNum + 3) // 3 is the number of posts you want to load per click
}
return (
<div>
{props.posts.slice(0, postNum).map(post => (
<div key={post.id}>
//...Post Data
</div>
))}
<button onClick={handleClick}>Load More</button>
</div>
)
}
export default LatestPost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment