Chrysolites Tech Academy

Short-Video App with React Native and Supabase · Course book

Chapter 10: The Auto-Playing Feed

Pairs with Lesson 10: A Home Feed with Video Playback

The home feed is the heart of a short-video app: a vertical stack of full-screen videos, one at a time, where swiping up brings the next clip and each one starts playing the moment it settles on screen. Getting it right needs three things working together: fetching the list of videos and secure addresses for their files, a list component that snaps one full screen at a time, and logic that plays exactly the visible video and pauses all the others. This chapter explains each, from signed URLs and the query that loads the feed, through FlatList's paging and viewability callbacks, to controlling many players at once, with the visibility arithmetic worked out by hand.

Fetching the feed and signed URLs

The feed starts with a database query: the newest videos, each with its creator's username and photo. With Supabase, a single call can fetch videos together with related rows, for example supabase.from("Video").select("*, User(username, profile_photo)").order("created_at", { ascending: false }).limit(20). Fetching related user data in the same request avoids a separate query per video, the "one plus N" pattern that makes feeds slow.

Each video row holds the path of its file in storage, not a playable address. If the bucket is private, which is the safer default, the app asks Supabase for signed URLs: addresses that include a cryptographic signature and an expiry time, so that anyone holding the address can fetch the file until it expires, and nobody can guess addresses for other files. Supabase can sign many paths in one call with createSignedUrls, and the app merges each signed address back into its video object.

ChoicePublic bucketPrivate bucket with signed URLs
AddressPermanent, based on the pathTemporary, includes a signature
Who can fetchAnyone with the addressAnyone with an unexpired signed address
Extra requestNoneOne signing call per batch of videos
Caching by phones and networksEasy, addresses never changeHarder, addresses change when re-signed
Good forProfile photos, public contentContent that should stay private to the app's users

Expiry should comfortably exceed how long someone might keep the app open on one batch of videos; an hour, or 3,600 seconds, is a common choice. If a user returns after the addresses expire, refetching the feed produces fresh ones. Using TanStack Query for the feed gives loading and error states for free, caches the result, and lets a pull-to-refresh gesture call refetch.

A list that pages one screen at a time

React Native's FlatList renders long lists efficiently by drawing only the items near the visible area and recycling memory as the user scrolls. For a video feed, each item is made exactly as tall as the visible area, and pagingEnabled makes the list snap so that each swipe moves exactly one item. Because video is expensive to render, a few further props keep memory under control: windowSize limits how many screens of items are kept mounted, and removeClippedSubviews detaches items far off screen.

The item height must match the space the feed occupies: the window height minus the tab bar and any safe-area insets, or simply the measured height of the list's container. If the height is even a few points off, snapping drifts and each video appears slightly shifted. Measuring the container with an onLayout handler and using that value for every item is the most reliable approach. Supplying getItemLayout, which tells the list each item's height and position in advance, lets it jump to any item instantly and avoids measuring as it scrolls.

Each item's component should also be memoised, wrapped so that it re-renders only when its own props change. Without this, updating the active video id, which happens on every swipe, would re-render every mounted item, and with heavy video components that causes visible stutter. Passing each item a simple boolean, "is this the active one?", rather than the whole active id, means only the two items whose flag changed, the one that stopped and the one that started, need to update. Keys matter too: giving FlatList a keyExtractor that returns each video's id lets it track items correctly when new videos are added at the top or loaded at the bottom.

Knowing which video is on screen

FlatList reports which items are visible through the onViewableItemsChanged callback, configured by viewabilityConfig. The setting itemVisiblePercentThreshold decides how much of an item must be on screen for it to count as viewable. With a threshold of 50, an item becomes viewable when at least half of it is showing.

Worked example. Each item is 760 points tall, and the user is dragging from video 0 towards video 1. After scrolling s points, video 0 shows its lower 760 − s points and video 1 shows its top s points. Their visible shares are (760 − s) / 760 and s / 760. At s = 190, video 0 is 75 percent visible and video 1 is 25 percent. At s = 380, both are exactly 50 percent, the switching point with a threshold of 50. At s = 570, video 1 is 75 percent visible and becomes the one to play. Paging then completes the movement to s = 760, where video 1 fills the screen.
video 0 visible video 1 visible switch at 380 pt 0%50%100% 0190380570760 scroll offset (points)
Figure 10.1 — Visible shares of two neighbouring videos during one swipe. With a 50 percent threshold, playback switches exactly halfway.

The callback receives the list of viewable items, and the feed stores the id of the first one as the active video. Two details trip people up. FlatList does not allow the callback or the configuration to change after the first render, so both must be stable, defined once with useRef or outside the component. And the callback runs often during scrolling, so it should only update state, not start network requests.

Playing one video and pausing the rest

Each feed item contains a video player whose source is the signed address. Rather than letting every player decide for itself, the feed passes each item a flag saying whether it is the active one, and the item plays or pauses its player accordingly, using a reference to call the player's play and pause methods in an effect that runs when the flag changes. Players are set to loop and to cover the screen. When a video becomes active again after the user scrolls back to it, restarting it from the beginning usually feels more natural than resuming midway.

Exactly one video should ever be playing, with sound. Other good behaviour follows from the same flag. When the user leaves the feed tab, the screen loses focus, and the active video should pause; a focus hook from the router makes this easy, by treating "screen not focused" as "no active video". When the app goes into the background, playback should stop too. Tapping a playing video can toggle pause, with a large translucent play icon shown while paused.

Smoothness depends on starting playback quickly. The next video can begin loading before it becomes active, so it starts instantly when the user swipes. Because FlatList keeps neighbouring items mounted within its window, their players can load their first segments in advance, while only the active one plays. The window size balances this: a larger window preloads more clips but uses more memory, and on lower-end phones a small window of three, the current item and one on each side, is usually best.

Loading states and more videos

The feed should handle every state. While the first query runs, a spinner on a black background fits the app. If the query fails, a message with a retry button is better than an empty screen. If there are no videos yet, a friendly prompt to record the first one turns an empty state into a call to action. Inside each item, a placeholder or thumbnail can show until the player reports that it is ready, avoiding a flash of black.

As users scroll towards the end of the list, onEndReached can fetch the next batch of videos, using the creation time of the last video as a cursor so that new uploads do not shift the pages. TanStack Query's infinite queries manage the pages, and their results are flattened into one array for the list. With these pieces, the feed behaves like the real thing: snap, play, swipe, play. Test it with dozens of videos, not two or three, to see how it really performs.

Practice tasks

  1. Items are 700 points tall. After scrolling 245 points from video 0, what share of each video is visible, and which one is active with a threshold of 50?
  2. With a threshold of 80, at what scroll offset does video 1 become viewable, for 760-point items?
  3. The window height is 844 points, the tab bar 50 and the bottom inset 34. How tall should each item be if the feed sits above the tab bar?
  4. Explain why the viewability callback must be defined once rather than recreated on every render.
  5. Compare public buckets and signed URLs for video files, giving one advantage of each.
  6. Describe what should happen to the active video when the user switches to the Profile tab.

Install Chrysolites Tech Academy

Android / Chrome / Edge: open the browser menu and choose Install app or Add to Home screen.

iPhone / iPad (Safari): tap the Share button, then Add to Home Screen.