Code Monkey home page Code Monkey logo

vdmrgv / react-easy-infinite-scroll-hook Goto Github PK

View Code? Open in Web Editor NEW
93.0 93.0 5.0 18.6 MB

♾️ A react hook that makes it easy to add infinite scroll in any components. It is very simple to integrate and supports any direction.

Home Page: https://vdmrgv.github.io/react-easy-infinite-scroll-hook/

License: MIT License

JavaScript 2.64% TypeScript 93.09% HTML 0.74% CSS 3.52%
carousel fetch fetching grid hooks infinite infinite-scroll infinite-scrolling list react react-hooks react-virtualized react-window scroll scrolling table

react-easy-infinite-scroll-hook's Introduction

Hi there 👋

GitHub vdmrgv GitHub vdmrgv's stars Linkedin: Vadim Rogov

I'm Vadim! I like code 👨‍💻, games 🕹️ and books 📚, thanks for reading!

react-easy-infinite-scroll-hook's People

Contributors

dependabot[bot] avatar vdmrgv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

react-easy-infinite-scroll-hook's Issues

Infinite loading breaks when scrollbar is dragged

Infinite loading breaks when scrollbar is dragged to the end. It will not load more rows, even after scroll is changed. This bug happens in all examples, but it is most easy to capture in example with prepending items.

Animation

  • OS: Windows 10
  • Browser: Chrome
  • Version: 108.0.5359.95 (64bit)

Reverse mode - scroll position jump after first data load

Describe the bug
Scroll position jumps after first setData execution.

Animation

To Reproduce

minimal reproducible demo:

import { SetStateAction, useCallback, useEffect, useRef, useState } from 'react'
import useInfiniteScroll from 'react-easy-infinite-scroll-hook'
import { FixedSizeList } from 'react-window'

export const loadMore = async (length = 50): Promise<string[]> => new Promise((res) => setTimeout(() => res(createItems(length)), 1000))

export const createItems = (length = 100): string[] => {
    return Array.from({ length }).map((_e, i) => `item ${i}`)
}

export const createNext =
    ({ setLoading, setData, offset }: { setData: (v: SetStateAction<string[]>) => void; setLoading: (v: SetStateAction<boolean>) => void; offset: number }) =>
    async () => {
        try {
            setLoading(true)
            const rows = await loadMore(offset)

            setData((prev) => [...rows, ...prev])
        } finally {
            setLoading(false)
        }
    }
const ReversedVerticalList = () => {
    const [data, setData] = useState<string[]>(() => createItems())
    const listRef = useRef<FixedSizeList>()

    const ref = useInfiniteScroll<HTMLDivElement>({
        next: createNext({ setData, setLoading: () => ({}), offset: 50 }),
        rowCount: data.length,
        hasMore: {
            up: true,
            down: false,
        },
        scrollThreshold: '50px',
    })

    const setRVRef = useCallback((RVnode) => {
        if (RVnode) {
            ref.current = RVnode._outerRef
            listRef.current = RVnode
        }
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [])

    useEffect(() => {
        listRef.current?.scrollTo(Number.MAX_SAFE_INTEGER)
    }, [])

    return (
        <>
            <FixedSizeList ref={setRVRef} width={500} height={500} itemSize={20} itemCount={data.length}>
                {({ index, style }) => {
                    const item = data[index]

                    return <div style={style}>{item}</div>
                }}
            </FixedSizeList>
        </>
    )
}

export default ReversedVerticalList

Possible hackfix
problem can be hackfixed by skipping first setData call:

const ReversedVerticalList = () => {
    const [data, _setData] = useState<string[]>(() => createItems())
    const listRef = useRef<FixedSizeList>()
    const [initialLoad, setInitialLoad] = useState(true)

    const setData = (v) => {
        !initialLoad && _setData(v)
    }

    const ref = useInfiniteScroll<HTMLDivElement>({
        next: createNext({ setData, setLoading: () => ({}), offset: 50 }),
        rowCount: data.length,
        hasMore: {
            up: true,
            down: false,
        },
        scrollThreshold: '50px',
    })

    const setRVRef = useCallback((RVnode) => {
        if (RVnode) {
            ref.current = RVnode._outerRef
            listRef.current = RVnode
        }
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [])

    useEffect(() => {
        listRef.current?.scrollTo(Number.MAX_SAFE_INTEGER)
        setInitialLoad(false)
    }, [])

    return (
        <>
            <FixedSizeList ref={setRVRef} width={500} height={500} itemSize={20} itemCount={data.length}>
                {({ index, style }) => {
                    const item = data[index]

                    return <div style={style}>{item}</div>
                }}
            </FixedSizeList>
        </>
    )
}

@vdmrgv could you think of some nicer solution 😊? Thanks.

Can't drag scrollbar on reversed scroll in Safari

It is not possible to drag the scrollbar when scrolling back in Safari

To Reproduce
Steps to reproduce the behavior:

  1. Go to examples page
  2. Click on reverse scroll example
  3. Drag the scrollbar
  4. See error

Add support for react-window

It would be nice, if you could add support for react-window virtualization library. It's slightly more modern alternative for react-virtualized, from the same author. Currently, I only get console error:
Sorry I can't use this container - try using a different DOM element.

Would it be possible to add support?
Thanks.

If initial viewport is not scrollable, next function wouldnt be triggered

I have an api that I fetch exactly 7 items per request, and if the container is not overflowing (the scrollbar isn't present) the next function would not be called. I tried setting the scrollThreshold to 0px and it does work to some degree, but this is not a solution I would like to use. However, when I reduce the viewport width (ex. I resize the window), I would get a scrollbar and the next function would be called as expected. Am I missing something ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.