4

I'm using react-infinite-scroll-component with Mantine's Select component, and have come across an issue. Basically whenever new data loads, the scroll bar resets all the way to the top, which as you can imagine is quite annoying in terms of UX.

Does anyone have a clue of why this is happening? While inspecting the DOM, it seems that the infinite scroll component completely re-renders for some reason.

You can see what I mean in this gif:

enter image description here

1
  • Hi @Lorik, I'm recently looking for a way to combine Mantine's select with infinite scroll, may I know how you do that, thx. Commented Apr 25, 2023 at 3:14

1 Answer 1

2

I'm almost sure that you are using some state for checking. I will give you a real bad example & solution for it.

Bad example which isn't works:

const hasData = !isLoading && postsResp?.content?.length;

{!hasData && (
        <InfiniteScroll
          dataLength={postsResp?.content?.length}
          next={() => getContentFeed(true)}
          hasMore={!postsResp?.last}
          loader={(
            <Skeleton
              variant='text'
              width={120}
              height={24}
              sx={{ margin: 'auto' }}
            />
          )}
        >
          {postsResp?.content?.map((post: Content) => (
            <Post key={`post_${post.uuid}+${Math.random()}`} post={post} />
          ))}
        </InfiniteScroll>
      ) : (
        'No posts'
      )}

And this is a one which works good: I know that you noticed i've moved the check if hasData on other place. I did that because each time this state is updating with new value and react re-render my component. That's why the scroll goes on top each time.

 {(!hasData && !isLoading) && (
        'No posts'

      )}
      <InfiniteScroll
        dataLength={postsResp?.content?.length ?? 0}
        next={() => getContentFeed(true)}
        hasMore={!postsResp?.last}
        loader={(
          <Skeleton
            variant='text'
            width={120}
            height={24}
            sx={{ margin: 'auto' }}
          />
        )}
      >
        {postsResp?.content?.map((post: Content) => (
          <Post key={`post_${post.uuid}+${Math.random()}`} post={post} />
        ))}
      </InfiniteScroll>

The main idea here is to remember to do not update any state according to your infinity scroll which can re-render your component.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.