Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add public method reset to force restart pagination #219

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

marlonmleite
Copy link

@marlonmleite marlonmleite commented Jun 28, 2019

The PR is based on issue #190 and #12

@marlonmleite
Copy link
Author

marlonmleite commented Jun 28, 2019

@danbovey

@NurdinDev
Copy link

when this update will be available?

@marlonmleite
Copy link
Author

@danbovey when will be available?

@Tautorn
Copy link

Tautorn commented Jul 25, 2019

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

@marlonmleite
Copy link
Author

marlonmleite commented Jul 26, 2019

@danbovey @YIZHUANG let's release?

@simjoeweb
Copy link

I need this

@marlonmleite
Copy link
Author

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

@remuspoienar
Copy link

Any updates on this ?

@preservance717
Copy link

is it release???

@hendriku
Copy link

hendriku commented Aug 31, 2020

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.

@azhaorz
Copy link

azhaorz commented Dec 1, 2020

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants