1.0.1 • Published 3 years ago
solid-infinite-scroll v1.0.1
solid-infinite-scroll
Infinite scrolling / Dynamic list loading library for SolidJS
Installation
First, install the library from the npm registry:
yarn add -D solid-infinite-scrollSecond, import it in the tsx component you're working on:
import InfiniteScroll from 'solid-infinite-scroll';Usage
To use the infinite scroll module, you would need a signal/resource for data storage, and implement two functions:
eachshould return the currently stored data array, this is similar to the SolidJS<For>componenthasMoreshould return true if more data is availablenextshould load more data into the array stored byeachand return nothing
For example, if you want to load all data at once but only display e.g. 50 at a time, you can do:
const fetchApi = async () => await (await fetch("https://api.example.com/list").json() as string[]
export default function App() {
const [api] = createResource(fetchApi)
const [scrollIndex, setScrollIndex] = createSignal(50)
const scrollNext = () => setScrollIndex(Math.min(scrollIndex() + 50, api().length))
return (
<InfiniteScroll each={api()?.slice(0, scrollIndex())}
hasMore={scrollIndex() < api()?.length}
next={scrollNext}>
{(item, index) =>
<div>{item}</div>
}
</InfiniteScroll>
)
}