react-infinite-scroller icon indicating copy to clipboard operation
react-infinite-scroller copied to clipboard

Add public method reset to force restart pagination

Open marlonmleite opened this issue 5 years ago • 11 comments

The PR is based on issue #190 and #12

marlonmleite avatar Jun 28 '19 20:06 marlonmleite

@danbovey

marlonmleite avatar Jun 28 '19 21:06 marlonmleite

when this update will be available?

NurdinDev avatar Jul 23 '19 12:07 NurdinDev

@danbovey when will be available?

marlonmleite avatar Jul 24 '19 13:07 marlonmleite

I'm with problem with infinite scroller too. This solution working for me. When will be available?

Tautorn avatar Jul 25 '19 15:07 Tautorn

@danbovey @YIZHUANG let's release?

marlonmleite avatar Jul 26 '19 21:07 marlonmleite

I need this

simjoeweb avatar Jul 30 '19 14:07 simjoeweb

Workaround for this bug:

import React, { PureComponent, createRef } from 'react'
import PropTypes from 'prop-types'
import InfiniteScroller from 'react-infinite-scroller'

class InfiniteScroll extends PureComponent {
  ref = createRef()

  scrolltoTop = () => {
    const scroll = this.ref.current.getParentElement(this.ref.current.scrollComponent)
    scroll.scrollTop = 0
  }

  reset() {
    if (this.ref && this.ref.current) {
      this.ref.current.pageLoaded = this.ref.current.props.pageStart

      this.scrolltoTop()
    }
  }

  render() {
    return <InfiniteScroller ref={this.ref} key="scroller" {...this.props} />
  }
}

InfiniteScroll.defaultProps = {
  pageStart: 1
}

InfiniteScroll.propTypes = {
  pageStart: PropTypes.number
}

export default InfiniteScroll

marlonmleite avatar Aug 23 '19 19:08 marlonmleite

Any updates on this ?

remuspoienar avatar Nov 29 '19 17:11 remuspoienar

is it release???

preservance717 avatar May 18 '20 09:05 preservance717

I also need this. The workaround throws a warning :( most likely because the react-infinite-scroller uses the ref as a prop which is wrong image

My personal workaround was, that I built my own page counter as a wrapper and used the loadMore function to increment the page value. This solution is more flexible in terms of resetting and querying APIs.

hendriku avatar Aug 31 '20 07:08 hendriku

hook + typescript

import React, { forwardRef, ForwardRefRenderFunction, useImperativeHandle, useRef } from "react";
import ReactInfiniteScroll from "react-infinite-scroller";

interface Props {
  element?: string;
  hasMore?: boolean;
  initialLoad?: boolean;
  isReverse?: boolean;
  loadMore(page: number): void;
  pageStart?: number;
  threshold?: number;
  useCapture?: boolean;
  useWindow?: boolean;
  loader?: React.ReactElement;
  getScrollParent?(): HTMLElement | null;
}

const InfiniteScroll: ForwardRefRenderFunction<{ reset: () => void }, Props> = ({ children, ...props }, ref) => {
  const scrollRef = useRef<ReactInfiniteScroll>();

  const scrolltoTop = () => {
    const scroll = scrollRef.current?.getParentElement(scrollRef.current.scrollComponent);
    scroll && (scroll.scrollTop = 0);
  };

  const reset = () => {
    if (scrollRef && scrollRef.current) {
      scrollRef.current.pageLoaded = scrollRef.current.props.pageStart;

      scrolltoTop();
    }
  };

  useImperativeHandle(ref, () => ({
    reset,
  }));

  return (
    <ReactInfiniteScroll ref={scrollRef} {...props}>
      {children}
    </ReactInfiniteScroll>
  );
};

export default forwardRef(InfiniteScroll);

azhaorz avatar Dec 01 '20 03:12 azhaorz