Hey everyone! I'm a web and mobile app developer working primarily with Node, React, React Native, and PHP. Despite having a couple of years of experience, I'm struggling to implement infinite scrolling correctly on both the frontend and backend. Any suggestions or useful repositories would be greatly appreciated! Thanks!
3 Answers
You could use htmx with a `revealed` trigger to load new content as it scrolls into view. It’s also worth considering pagination. Cursor-based pagination is a great fit for infinite scroll because it handles live updates to your content better than limit-offset pagination, which can trip you up if new data is added between requests.
For handling infinite scroll in React and React Native, I recommend using the Intersection Observer API instead of scroll event listeners. You can attach a ref to your last item in the list and trigger the next page fetch when that item comes into view. On the backend, consider using cursor-based pagination instead of traditional offset/limit setup for better performance. Send back a `nextCursor` and a `hasMore` boolean with your responses. Also, React Native's FlatList has a built-in `onEndReached` feature that can simplify things! Let me know if you need any code snippets for this!
Infinite scroll typically relies on cursor-based pagination rather than offsets. For the backend, set up cursor-based pagination using fields like `createdAt` or `id`. Always return your items along with `nextCursor`. On the frontend, use the Intersection Observer API to manage loading, a `hasMore` state, and an `error` state. Trigger your fetch only when the sentinel is visible. As for resources, check out TanStack Query's `useInfiniteQuery`, and search for ‘cursor pagination node postgres’ on GitHub. Once you shift your thinking to cursors, infinite scroll becomes a lot easier!

Awesome tips! I won’t be doing a PWA this time, but I'm planning to make this project cross-platform using Expo with React Native.