<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: cclike hh</title>
    <description>The latest articles on DEV Community by cclike hh (@cclike_hh_099e9dace9af0d1).</description>
    <link>https://dev.to/cclike_hh_099e9dace9af0d1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4247183%2F5f99090c-3a9e-45cc-a883-a4e1f2e85157.png</url>
      <title>DEV Community: cclike hh</title>
      <link>https://dev.to/cclike_hh_099e9dace9af0d1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cclike_hh_099e9dace9af0d1"/>
    <language>en</language>
    <item>
      <title>Why preloaded videos can restart when you scroll in React</title>
      <dc:creator>cclike hh</dc:creator>
      <pubDate>Mon, 16 Nov 2026 07:47:17 +0000</pubDate>
      <link>https://dev.to/cclike_hh_099e9dace9af0d1/why-preloaded-videos-can-restart-when-you-scroll-in-react-32a9</link>
      <guid>https://dev.to/cclike_hh_099e9dace9af0d1/why-preloaded-videos-can-restart-when-you-scroll-in-react-32a9</guid>
      <description>&lt;p&gt;A video feed has an awkward timing problem: you want the next clip to load before someone reaches it, but you only want the current clip to play.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://vhoo.ai/" rel="noopener noreferrer"&gt;Vhoo&lt;/a&gt;, the AI image and video app I’m building, the mobile template feed handles this with two separate values: &lt;code&gt;retainSource&lt;/code&gt; and &lt;code&gt;active&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That distinction matters because a React effect can accidentally reset a video at exactly the moment you want to use its preloaded data.&lt;/p&gt;

&lt;p&gt;Imagine three cards:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Card&lt;/th&gt;
&lt;th&gt;Keep its source attached?&lt;/th&gt;
&lt;th&gt;Eligible for playback?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Previous&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Current&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Next&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Cards farther away have their video source removed.&lt;/p&gt;

&lt;p&gt;The feed calculates those values like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="n"&gt;active&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;activeIndex&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;retainSource&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;activeIndex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now consider what happens when the user moves from card A to card B.&lt;/p&gt;

&lt;p&gt;Card B already has its source attached. It becomes active, but &lt;code&gt;retainSource&lt;/code&gt; remains &lt;code&gt;true&lt;/code&gt;. Ideally, the player simply starts using the media it has been loading.&lt;/p&gt;

&lt;p&gt;This is where an effect’s dependencies can cause trouble.&lt;/p&gt;

&lt;p&gt;Suppose one effect handles both assigning the video source and starting playback. It depends on &lt;code&gt;src&lt;/code&gt;, &lt;code&gt;retainSource&lt;/code&gt;, and &lt;code&gt;active&lt;/code&gt;. Its cleanup pauses the video and removes the source.&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;active&lt;/code&gt; changes, React runs the old cleanup before running the effect again. That is the normal &lt;a href="https://react.dev/reference/react/useEffect" rel="noopener noreferrer"&gt;effect lifecycle&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The resulting sequence is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Card B preloads while it is next in the feed.&lt;/li&gt;
&lt;li&gt;The user scrolls to B.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;active&lt;/code&gt; changes.&lt;/li&gt;
&lt;li&gt;The effect cleans up B’s existing media source.&lt;/li&gt;
&lt;li&gt;The effect attaches the same source and loads it again.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The player has reset just as it becomes visible.&lt;/p&gt;

&lt;p&gt;Calling &lt;code&gt;video.load()&lt;/code&gt; resets the media element and starts source selection again. It can also abort pending playback operations. It is a meaningful lifecycle operation, as the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/load" rel="noopener noreferrer"&gt;media API documentation&lt;/a&gt; explains.&lt;/p&gt;

&lt;p&gt;The browser may reuse cached bytes, so this does not necessarily mean downloading the entire file again. But the application has still restarted the player’s loading process unnecessarily.&lt;/p&gt;

&lt;p&gt;The fix is to give source retention its own effect.&lt;/p&gt;

&lt;p&gt;Here is the source-management portion, using a &lt;code&gt;player&lt;/code&gt; ref attached to the video element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;video&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;video&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;retainSource&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;video&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;video&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;video&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pause&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;video&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'src'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;video&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;retainSource&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This effect has no playback decision to make, so it does not read &lt;code&gt;active&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It runs when the URL changes, when the card enters or leaves the retained range, and during mounting or cleanup. Promoting the next card to the current card leaves its source alone.&lt;/p&gt;

&lt;p&gt;Playback belongs in a separate effect. That effect responds to &lt;code&gt;active&lt;/code&gt;, pauses when the card becomes inactive, and handles whether playback is currently allowed.&lt;/p&gt;

&lt;p&gt;In our feed, that includes a hidden browser tab, the user’s reduced-motion preference, and a manual pause. Returning to the tab should not restart a video the user deliberately paused.&lt;/p&gt;

&lt;p&gt;There is also an asynchronous detail: &lt;code&gt;video.play()&lt;/code&gt; returns a promise. Playback can be rejected, including when browser policy blocks autoplay. The interface should reflect the result instead of assuming that calling the method means the video is playing. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/play" rel="noopener noreferrer"&gt;MDN’s &lt;code&gt;play()&lt;/code&gt; reference&lt;/a&gt; describes these cases.&lt;/p&gt;

&lt;p&gt;If a user scrolls away while a play request is pending, an old result must not update the newly active card’s state. Our hook uses a request counter to ignore outdated results, and its &lt;code&gt;playing&lt;/code&gt; handler checks that the card is still active.&lt;/p&gt;

&lt;p&gt;Source retention and playback now have different cleanup behavior:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Leaving the active position pauses the video.&lt;/li&gt;
&lt;li&gt;Leaving the retained range removes its source.&lt;/li&gt;
&lt;li&gt;Becoming active again while still retained does not call &lt;code&gt;load()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Stable React keys matter here too. The feed uses each item’s ID as its key. Remounting the video element when its active state changes would defeat the separation.&lt;/p&gt;

&lt;p&gt;Keeping one neighbor on each side is a deliberate tradeoff. It gives likely next clips a chance to load, but it may fetch video that the user never watches. Longer clips or tighter data budgets may call for a smaller retained range.&lt;/p&gt;

&lt;p&gt;And &lt;code&gt;preload="auto"&lt;/code&gt; remains a browser hint, not a promise that the next clip is fully buffered. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/preload" rel="noopener noreferrer"&gt;The preload documentation&lt;/a&gt; makes that distinction explicit.&lt;/p&gt;

&lt;p&gt;To check this behavior in your own feed, follow one card as it moves from next, to current, to previous. Watch calls to &lt;code&gt;load()&lt;/code&gt; and changes to its source. Becoming current should trigger a playback decision without resetting the source.&lt;/p&gt;

&lt;p&gt;That is the useful boundary: keep the media resource alive for as long as the retention policy allows, and let playback change independently as the user scrolls.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
