-
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.
feat(useInfiniteScroll): add
loadMore
method to load more manually …
…& `reset` to reset, deprecated isLoading, prefer `loading`
- Loading branch information
Showing
2 changed files
with
91 additions
and
45 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,45 +1,66 @@ | ||
import { Card, KeyValue, Zone, wait as mockFetch } from '@/components' | ||
import { generateLoremIpsum, useInfiniteScroll, useSafeState } from '@shined/react-use' | ||
import { Button, Card, KeyValue, Zone, cn, wait as mockFetch } from '@/components' | ||
import { generateLoremIpsum, useInfiniteScroll, useSafeState, useVersionedAction } from '@shined/react-use' | ||
import { useRef } from 'react' | ||
|
||
export function App() { | ||
const ref = useRef<HTMLDivElement>(null) | ||
const [list, setList] = useSafeState<{ idx: number; text: string }[]>([]) | ||
const [incVersion, runVersionedAction] = useVersionedAction() | ||
|
||
const fetchData = async () => { | ||
await mockFetch(Math.random() * 600 + 200) | ||
const version = incVersion() | ||
|
||
const newData = Array.from({ length: 20 }, (_, i) => ({ | ||
idx: list.length + (i + 1), | ||
text: generateLoremIpsum(), | ||
})) | ||
await mockFetch(1000) | ||
|
||
setList([...list, ...newData]) | ||
return await runVersionedAction(version, async () => { | ||
const newData = Array.from({ length: 20 }, (_, i) => ({ | ||
idx: list.length + (i + 1), | ||
text: generateLoremIpsum(), | ||
})) | ||
|
||
return newData | ||
setList([...list, ...newData]) | ||
|
||
return newData | ||
}) | ||
} | ||
|
||
const scroll = useInfiniteScroll(ref, fetchData, { | ||
const infiniteScroll = useInfiniteScroll(ref, fetchData, { | ||
canLoadMore: (pre) => { | ||
return pre ? pre[pre.length - 1].idx <= 100 : true | ||
}, | ||
}) | ||
|
||
const reset = () => { | ||
incVersion() | ||
setList([]) | ||
infiniteScroll.reset() | ||
} | ||
|
||
return ( | ||
<Card> | ||
<Zone> | ||
<KeyValue label="isLoading" value={scroll.isLoading} /> | ||
<KeyValue label="isLoadDone" value={scroll.isLoadDone} /> | ||
<KeyValue label="Loading" value={infiniteScroll.loading} /> | ||
<KeyValue label="isLoadDone" value={infiniteScroll.isLoadDone} /> | ||
<KeyValue label="Item Count" value={list.length} /> | ||
</Zone> | ||
<div ref={ref} className="w-full h-80 bg-#666666/20 rounded overflow-scroll p-4"> | ||
<div | ||
ref={ref} | ||
className={cn( | ||
'w-full h-80 bg-#666666/20 rounded overflow-scroll p-4 transition-all', | ||
infiniteScroll.loading ? 'opacity-60' : '', | ||
)} | ||
> | ||
{list.map((item) => ( | ||
<div key={item.idx} className="my-2 p-2 bg-primary/40 dark:text-white rounded"> | ||
{item.text} | ||
</div> | ||
))} | ||
{scroll.isLoading && <div className="text-center my-1 py-1 dark:text-white animate-pulse">Loading...</div>} | ||
{scroll.isLoadDone && <div className="text-center my-1 py-1 dark:text-white">No more data</div>} | ||
{infiniteScroll.loading && ( | ||
<div className="text-center my-1 py-1 dark:text-white animate-pulse">Loading...</div> | ||
)} | ||
{infiniteScroll.isLoadDone && <div className="text-center my-1 py-1 dark:text-white/60">No more data</div>} | ||
</div> | ||
<Button onClick={reset}>Reset List</Button> | ||
</Card> | ||
) | ||
} |
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