npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-g-infinite-scroll

v0.0.1

Published

This package is an infinite scroll: component, hook or HOC. You choose!

Downloads

4

Readme

react-g-infinite-scroll

This package is an infinite scroll: component, hook or HOC. You choose!

Installation:

yarn add react-g-infinite-scroll

or

npm install --save react-g-infinite-scroll

Live demo:

http://gri.fo/react-g-infinite-scroll/storybook-static

How to:

Options:

| Option | Default | Description | | - | - | - | | expectRef[1] | false | If this is false, the script will use the window scroll, but if you want to use the infinite scroll in a custom element, you should pass true, then you will receive a ref to add to the element | | fetchMore[2] | - | The function that will be called when need to load the next page | | ignoreScroll[3] | false | You should use this prop when the data is fetching or wether there is no more data to fetch. When ignoreScroll is true, the infinite scroll stops to watch the scroll | | offset | 0 | The offset of the scroll to call the fetchMore |

  1. Keep in mind that if you want to use scroll in a custom element, instead the window, you need to add classes to activate the scroll (overflow: "auto | hidden" and a custom height)

  2. Ensure that fetchMore changes only when needed to prevent unnecessary renders https://overreacted.io/a-complete-guide-to-useeffect/#but-i-cant-put-this-function-inside-an-effect

  3. When the ignoreScroll is updated from true to false (normally after a fetch), it is a trigger to recalculate if needs a new fetch - This is to solve cases when the content do not fill whole space. Eg: If your content has 200px, but your container has 300px, then we need to fetch one more page

You have three ways to use the infinite scroll package: Hook, Component or HOC.

Hook

Code sample: https://github.com/grifo/react-g-infinite-scroll/tree/develop/stories/hook

How to use the hook with scroll in the window:

import React, { useCallback } from 'react'
import { useInfiniteScroll } from 'react-g-infinite-scroll'

const MyComponent = ({ isFetching }) => {
  const fetchMore = useCallback(() => {
    /* YOUR_FETCH_CODE_HERE */
  }, [/* YOUR_FETCH_DEPS_HERE */])

  useInfiniteScroll({
    fetchMore, // Shorthand to fetchMore: fetchMore
    ignoreScroll: isFetching,
    offset: 20
  })

  return /* CONTENT_HERE */
}

If you want to use the scroll in a custom element, you should pass the expectRef: true to the hook function and it will return a ref:

  const ref = useInfiniteScroll({
    expectRef: true,
    fetchMore,
    ignoreScroll: isFetching,
    offset: 20
  })

  return (
    <div ref={ref} className={/* SCROLL_STYLES */}>
      {/* CONTENT_HERE */}
    </div>
  )

Component

Code sample: https://github.com/grifo/react-g-infinite-scroll/tree/develop/stories/component

Notice: In the backstage it uses the hook

How to use the hook with scroll in the window:

import React, { useCallback } from 'react'
import { InfiniteScroll } from 'react-g-infinite-scroll'

const MyComponent = ({ isFetching }) => {
  const fetchMore = useCallback(() => {
    /* YOUR_FETCH_CODE_HERE */
  }, [/* YOUR_FETCH_DEPS_HERE */])

  return (
    <InfiniteScroll
      fetchMore={fetchMore}
      ignoreScroll={isFetching}
      offset={20}
    >
      {/* CONTENT_HERE */}
    </InfiniteScroll>
  )
}

If you want to use the scroll in a custom element, you should pass the prop expectRef as true to the component, then the children will be a render prop with the ref to add to your element:

import React, { useCallback } from 'react'
import { InfiniteScroll } from 'react-g-infinite-scroll'

const MyComponent = ({ isFetching }) => {
  const fetchMore = useCallback(() => {
    /* YOUR_FETCH_CODE_HERE */
  }, [/* YOUR_FETCH_DEPS_HERE */])

  return (
    <InfiniteScroll
      expectRef
      fetchMore={fetchMore}
      ignoreScroll={isFetching}
      offset={20}
    >
      {ref => (
        <div ref={ref} className={/* SCROLL_STYLES */}>
          {/* CONTENT_HERE */}
        </div>
      )}
    </InfiniteScroll>
  )
}

HOC

Code sample: https://github.com/grifo/react-g-infinite-scroll/tree/develop/stories/hoc

Notice: In the backstage it uses the hook

How to use the hook with scroll in the window:

import React from 'react'
import { withInfiniteScroll } from 'react-g-infinite-scroll'

const MyComponent = ({ myProps }) => (
  /* CONTENT_HERE */
)

export default withInfiniteScroll(MyComponent)
import React, { useCallback } from 'react'
import MyComponent from 'MyComponent'

const MyComponentWrapper = ({ isFetching }) => {
  const fetchMore = useCallback(() => {
    /* YOUR_FETCH_CODE_HERE */
  }, [/* YOUR_FETCH_DEPS_HERE */])

  return (
    <MyComponent
      infiniteScrollProps={{
        fetchMore,
        ignoreScroll: isFetching,
        offset: 20
      }}
      /* MY_PROPS_TO_MY_COMPONENT_HERE */
    />
  )
}

If you want to use the scroll in a custom element, you should pass the prop expectRef as true to the enhanced component. And your component will receive the ref with the forwardRef:

import React, { forwardRef } from 'react'
import { withInfiniteScroll } from 'react-g-infinite-scroll'

const MyComponent = forwardRef(({ myProps }, ref) => (
  <div ref={ref} className={/* SCROLL_STYLES */}>
    {/* CONTENT_HERE */}
  </div>
))

export default withInfiniteScroll(MyComponent)
import React, { useCallback } from 'react'
import MyComponent from 'MyComponent'

const MyComponentWrapper = ({ isFetching }) => {
  const fetchMore = useCallback(() => {
    /* YOUR_FETCH_CODE_HERE */
  }, [/* YOUR_FETCH_DEPS_HERE */])

  return (
    <MyComponent
      infiniteScrollProps={{
        expectRef: true,
        fetchMore,
        ignoreScroll: isFetching,
        offset: 20
      }}
      /* MY_PROPS_TO_MY_COMPONENT_HERE */
    />
  )
}