Skip to content

Commit

Permalink
chore: release new hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
thijsdaniels committed Aug 14, 2024
1 parent f44a131 commit 2a73c10
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-planes-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codedazur/react-pagination": minor
---

The useLoadMore hook was added.
5 changes: 5 additions & 0 deletions .changeset/little-kids-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codedazur/react-essentials": minor
---

The useDelayedValue hook was added.
12 changes: 4 additions & 8 deletions packages/react-pagination/hooks/useLoadMore.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { useCallback, useEffect, useMemo, useState } from "react";
import { useCallback, 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
Expand All @@ -19,14 +18,11 @@ export interface UseLoadMoreProps<T> {
export function useLoadMore<T>({ items, itemsPerPage }: UseLoadMoreProps<T>) {
const [page, setPage] = useState(1);

const [visibleItems, setVisibleItems] = useState<T[]>(
items.slice(0, itemsPerPage * page),
const visibleItems = useMemo<T[]>(
() => items.slice(0, itemsPerPage * page),
[page, items, itemsPerPage],
);

useEffect(() => {
setVisibleItems(items.slice(0, itemsPerPage * page));
}, [page, items, itemsPerPage]);

const isOverflowing = useMemo(
() => items.length > page * itemsPerPage,
[page, items, itemsPerPage],
Expand Down

0 comments on commit 2a73c10

Please sign in to comment.