5

I am on the project with react.js

For the project, I am supposed to include board with infinite scroll like Facebook

In that, I have a question.

When I scroll the board and recall more post like Facebook, is it working

whether it draws only additional posts under the bottom of post or

it recalls the whole list and redraws it

ex) case 1 :  {1,2,3} > scroll > {1,2,3} + {4,5} (additional posts)
    case 2 :  {1,2,3} > scroll > {1,2,3,4,5} (whole posts)

if I do case 1, I wanna know-how.

Thanks!

1
  • If there are a million things in the list and the user scrolls down then eventually there will be a million things on the page and the browser will crash (probably much earlier). Instead of making your life more difficult why not use virtualized. I think this is wat facebook changed to as well so you don't get too many items rendered at some point. Commented Mar 21, 2020 at 15:58

1 Answer 1

4

You should absolutely prefer 1st method. It will enable you to have smaller payload size when you fetch data from api and provide smoother user experience in general.

Let's assume that your api already provides you a way to fetch posts with in certain index-range, example https://myapi.com/posts?start=0&end=10 would return you a list of 10 first posts.

In your React.js -app you should then keep track of latest index you have fetched and when you reach end of page you add to index and fetch for more posts. I pasted here minimalistic codebase for blocks to achieve this, working demo on CodeSandbox

// Store current index of posts
const [startIndex, setStartIndex] = React.useState(0);

const LoadMorePosts = () => {
  // Load 10 more
  setStartIndex(prev => prev + 10);
};

React.useEffect(() => {
  (async () => {
    // Add search range to url
    const url = `?start=${startIndex}&end=${startIndex + 10}`;
    const posts = await mockApi(url);
    // Append new posts to end of old ones
    setPosts(prev => [...prev, ...posts]);
  })();
}, [startIndex]); // Run this effect when startIndex changes
Sign up to request clarification or add additional context in comments.

4 Comments

if you keep loading more post then eventually you are rendering too many elements and crash the browser. I think facebook already changed their "infinite list" to a virtualized list
i do not think the question was about how to render long list of items, but how to actually achieve and proceed with 1st case mentioned with the post. and yes there are various libraries that get you to work around the issue of rendering large data sets in browser, example github.com/bvaughn/react-window & github.com/bvaughn/react-virtualized , and both of them has solution to implement infinite loader of their own.
The reason i say this is because the load more is bad design and you'll end up with too many elements rendered. So even if OP asked for it it doesn't mean that's the best solution. If the OP would ask what's the best way to jump off a high building you won't answer "High as balls and shouting YOLOOOOO", you'll probably advice the elevator or stairs.
it is by no any means "bad design", it all depends on what you are rendering. and in the end it comes to nothing else that you just replace your list component with react-virtualized or react-window if you come to have bottleneck. why should you consider all the different issue before they have even arise? just for incase over engineer every possible step?

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.