All Projects → onderonur → React Infinite Scroll Hook

onderonur / React Infinite Scroll Hook

Licence: mit
A simple hook to create infinite scroll list components

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to React Infinite Scroll Hook

mey
A react package that exports hooks for handling the request lifecycle.
Stars: ✭ 18 (-88.08%)
Mutual labels:  fetch, hooks
Tipple
A lightweight dependency-free library for fetching data over REST with React.
Stars: ✭ 133 (-11.92%)
Mutual labels:  hooks, fetch
Masonic
🧱 High-performance masonry layouts for React
Stars: ✭ 209 (+38.41%)
Mutual labels:  hooks, infinite-scroll
React Fetch Hook
React hook for conveniently use Fetch API
Stars: ✭ 285 (+88.74%)
Mutual labels:  hooks, fetch
Swr
React Hooks for data fetching
Stars: ✭ 20,348 (+13375.5%)
Mutual labels:  fetch, hooks
React Fetching Library
Simple and powerful API client for react 👍 Use hooks or FACCs to fetch data in easy way. No dependencies! Just react under the hood.
Stars: ✭ 561 (+271.52%)
Mutual labels:  hooks, fetch
React Query
⚛️ Hooks for fetching, caching and updating asynchronous data in React
Stars: ✭ 24,427 (+16076.82%)
Mutual labels:  hooks, fetch
React Ufo
🛸 react-ufo - A simple React hook to help you with data fetching 🛸
Stars: ✭ 85 (-43.71%)
Mutual labels:  hooks, fetch
Pure Store
A tiny immutable store with type safety.
Stars: ✭ 133 (-11.92%)
Mutual labels:  hooks
Vue Scroller
Scroller Component for Vue.js
Stars: ✭ 1,775 (+1075.5%)
Mutual labels:  infinite-scroll
React Use Scrollspy
Flexible React Hook to automatically update navigation based on scroll position
Stars: ✭ 133 (-11.92%)
Mutual labels:  hooks
Od Virtualscroll
🚀 Observable-based virtual scroll implementation in Angular
Stars: ✭ 133 (-11.92%)
Mutual labels:  infinite-scroll
React Storage Hooks
React hooks for persistent state
Stars: ✭ 146 (-3.31%)
Mutual labels:  hooks
React Atom
A simple way manage state in React, inspired by Clojure(Script) and reagent.cljs
Stars: ✭ 133 (-11.92%)
Mutual labels:  hooks
Distormx
The ultimate hooking library
Stars: ✭ 146 (-3.31%)
Mutual labels:  hooks
React Simple Infinite Scroll
A brutally simple react infinite scroll component
Stars: ✭ 132 (-12.58%)
Mutual labels:  infinite-scroll
Use Http
🐶 React hook for making isomorphic http requests
Stars: ✭ 2,066 (+1268.21%)
Mutual labels:  fetch
React Universal Hooks
🎉 React Universal Hooks : just use****** everywhere (Functional or Class Component). Support React DevTools!
Stars: ✭ 148 (-1.99%)
Mutual labels:  hooks
Vertical Collection
Infinite Scroll and Occlusion at > 60FPS
Stars: ✭ 144 (-4.64%)
Mutual labels:  infinite-scroll
Precommit
pre-commit hooks for R projects
Stars: ✭ 141 (-6.62%)
Mutual labels:  hooks

react-infinite-scroll-hook

All Contributors

This is a hook to create infinite scroll components!
It has a really basic logic that solved a lot of problems for me. So, I just wanted to publish it as a package.

Live demo is here.
Also, you can find a more complete usage with react-apollo here.

Basically; useInfiniteScroll hook checks the DOM with an interval and looks at the distance between the bottom of your "infinite" component and the bottom of the window.
You can set scrollContainer prop to parent if you want to use the scrollable parent of that infinite container and not the window. With this setting, when the parent component is in view, the hook will check the bottom offset and trigger the onLoadMore callback if offset is less than threshold.

While setting the interval, we use another custom hook named useInterval and it makes the setInterval declarative. It has been explained by Dan Abramov here.

Installation

npm install react-infinite-scroll-hook

Basic Usage

import useInfiniteScroll from 'react-infinite-scroll-hook';

function InfiniteList({}) {
  const [items, setItems] = useState([]);
  const [hasNextPage, setHasNextPage] = useState();

  /// ...

  function handleLoadMore() {
    setLoading(true);
    // Some API call to fetch the next page
    loadNextPage(endCursor, pageSize).then((newPage) => {
      setLoading(false);
      setHasNextPage(newPage.hasNextPage);
      setItems([...items, newPage.items]);
    });
  }

  const infiniteRef = useInfiniteScroll({
    loading,
    hasNextPage,
    onLoadMore: handleLoadMore,
    scrollContainer,
  });

  // ...

  return (
    <List ref={infiniteRef}>
      {items.map((item) => (
        <ListItem key={item.key}>{item.value}</ListItem>
      ))}
      {loading && <ListItem>Loading...</ListItem>}
    </List>
  );
}

Props

  • loading: Some sort of "fetching" info of the request.
  • hasNextPage: If the list has more items to load.
  • onLoadMore: The callback function to execute when the threshold is exceeded.
  • threshold: Maximum distance to bottom of the window/parent to trigger the callback. Default is 150.
  • checkInterval: Frequency to check the dom. Default is 200.
  • scrollContainer: May be "window" or "parent". Default is "window". If you want to use a scrollable parent for the infinite list, use "parent".

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Eugene Fidelin

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].