Skip to content

Instantly share code, notes, and snippets.

@vilindberg
Last active May 6, 2019 15:10
Show Gist options
  • Save vilindberg/db7495358d4fcd77272f6f212ca562ae to your computer and use it in GitHub Desktop.
Save vilindberg/db7495358d4fcd77272f6f212ca562ae to your computer and use it in GitHub Desktop.
import { useMemo, useState } from 'react'
type Pagination = {
currentPage: number
setPage: (page: number) => void
}
export function usePagination<T>(items: T[] = [], pageSize = 10): [T[], Pagination] {
const [currentPage, setPage] = useState(0)
const currentItems = useMemo(() => {
return items.slice(currentPage * pageSize, (currentPage + 1) * pageSize)
}, [items, currentPage, pageSize])
return [
currentItems,
{
currentPage,
setPage,
},
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment