-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da90b07
commit f031fca
Showing
5 changed files
with
65 additions
and
12 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
export function useDelayedValue<T>(value: T, delay: number) { | ||
const [delayedValue, setDelayedValue] = useState(value); | ||
|
||
useEffect(() => { | ||
const timeout = setTimeout(() => { | ||
setDelayedValue(value); | ||
}, delay); | ||
|
||
return () => clearTimeout(timeout); | ||
}, [value, delay]); | ||
|
||
return delayedValue; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useCallback, useEffect, useMemo, useState } from "react"; | ||
|
||
export interface UseLoadMoreProps<T> { | ||
items: T[]; | ||
itemsPerPage: number; | ||
} | ||
|
||
/** | ||
* @todo Review this code before releasing it. | ||
* @todo Support asynchoronously fetching more items _instead_ of providing all | ||
* items at once and using `itemsPerPage` to limit the results. These are two | ||
* very different use cases, so evaluate if this hook should support both, or if | ||
* we should create a new hook for the async case. | ||
* @todo Create an additional hook that calls this hook's `loadMore` function | ||
* when the bottom of some overflowing container is reached. The new hook should | ||
* be called `useInfiniteScroll` and should be a thin wrapper around this hook. | ||
* @todo Support setting the initial page. | ||
*/ | ||
export function useLoadMore<T>({ items, itemsPerPage }: UseLoadMoreProps<T>) { | ||
const [page, setPage] = useState(1); | ||
|
||
const [visibleItems, setVisibleItems] = useState<T[]>( | ||
items.slice(0, itemsPerPage * page), | ||
); | ||
|
||
useEffect(() => { | ||
setVisibleItems(items.slice(0, itemsPerPage * page)); | ||
}, [page, items, itemsPerPage]); | ||
|
||
const isOverflowing = useMemo( | ||
() => items.length > page * itemsPerPage, | ||
[page, items, itemsPerPage], | ||
); | ||
|
||
const loadMore = useCallback(() => { | ||
setPage((page) => page + 1); | ||
}, []); | ||
|
||
return { | ||
items: visibleItems, | ||
isOverflowing, | ||
loadMore, | ||
}; | ||
} |