Last active
May 6, 2019 15:10
-
-
Save vilindberg/db7495358d4fcd77272f6f212ca562ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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