<?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: Regina Liu</title>
    <description>The latest articles on DEV Community by Regina Liu (@regexyl).</description>
    <link>https://dev.to/regexyl</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F394042%2Fccaba49c-0d76-43b4-8e6b-50e7d61263bd.JPG</url>
      <title>DEV Community: Regina Liu</title>
      <link>https://dev.to/regexyl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/regexyl"/>
    <language>en</language>
    <item>
      <title>How AnimatePresence in framer-motion works</title>
      <dc:creator>Regina Liu</dc:creator>
      <pubDate>Sat, 06 Jan 2024 19:32:30 +0000</pubDate>
      <link>https://dev.to/regexyl/how-animatepresence-in-framer-motion-works-161p</link>
      <guid>https://dev.to/regexyl/how-animatepresence-in-framer-motion-works-161p</guid>
      <description>&lt;p&gt;Suppose you want to have a React component perform an animation before it exits. It's tricky, because there's no simple native way to do so. A component is either &lt;em&gt;in&lt;/em&gt; the React tree or &lt;em&gt;not&lt;/em&gt; and there's no in-between. It also does not have the knowledge of when exactly it gets removed from the tree prior to its removal.&lt;/p&gt;

&lt;p&gt;An intuitive, non-Reacty way is to (1) start the exit animation, (2) wait till the animation is done, then (3) remove the element from the DOM. That's kind of a chore, isn't it? It's probably also not declarative&lt;sup id="fnref1"&gt;1&lt;/sup&gt; enough for our tastes, so perhaps this is where external libraries are able to step in and create a workaround.&lt;/p&gt;

&lt;p&gt;The two most popular choices now (circa Jan 2024) are &lt;a href="https://reactcommunity.org/react-transition-group/"&gt;React Transition Group&lt;/a&gt;, started in &lt;a href="https://github.com/reactjs/react-transition-group/commits?after=3341075c524bcf466241f5eafbc14bd407d24bc9+0"&gt;2016&lt;/a&gt;, and &lt;a href="https://framer.com/motion"&gt;Framer Motion&lt;/a&gt;, started in &lt;a href="https://github.com/framer/motion/commits?after=3105d6f745159c5f193510a221154797459c6732+0"&gt;2018&lt;/a&gt;. I'm not too familiar with the former, so this article solely dives into the workings of &lt;code&gt;AnimatePresence&lt;/code&gt; from Framer Motion and how it's able to enable exit animations.&lt;/p&gt;

&lt;h2&gt;
  
  
  A brief introduction to &lt;code&gt;AnimatePresence&lt;/code&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;AnimatePresence&lt;/code&gt; allows components to animate out when they're removed from the React tree.&lt;sup id="fnref2"&gt;2&lt;/sup&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is what it looks like in code:&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;motion&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;AnimatePresence&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;framer-motion&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;isVisible&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;AnimatePresence&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isVisible&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;motion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;
        &lt;span class="na"&gt;initial&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;animate&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;AnimatePresence&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see that &lt;code&gt;&amp;lt;motion.div&amp;gt;&lt;/code&gt; is controlled by a conditional, &lt;code&gt;isVisible&lt;/code&gt;, while &lt;code&gt;AnimatePresence&lt;/code&gt; is an immediate wrapper component around its children with an exiting animation.&lt;/p&gt;

&lt;p&gt;The exit animation is triggered when &lt;code&gt;isVisible&lt;/code&gt; becomes &lt;code&gt;false&lt;/code&gt;. Theoretically, &lt;code&gt;&amp;lt;motion.div&amp;gt;&lt;/code&gt; should have been removed from the React tree - yet logically it doesn't make sense, since at the point of its "removal", &lt;code&gt;&amp;lt;motion.div&amp;gt;&lt;/code&gt; is still performing its exit animation, so that means it still is somehow within the DOM!&lt;/p&gt;

&lt;p&gt;Here's where &lt;code&gt;&amp;lt;AnimatePresence&amp;gt;&lt;/code&gt; does something sneaky.&lt;/p&gt;

&lt;h2&gt;
  
  
  The secret sauce: &lt;code&gt;React.useRef&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Did someone say "give me a tool that reliably stores existing state during re-renders &lt;em&gt;and&lt;/em&gt; does not cause a component re-render when its state is updated"? &lt;code&gt;React.useRef&lt;/code&gt; does just that. &lt;/p&gt;

&lt;p&gt;We also want to be able to control when exactly children within &lt;code&gt;AnimatePresence&lt;/code&gt; re-render to minimize unnecessary overhead, so choosing &lt;code&gt;useRef&lt;/code&gt; instead of &lt;code&gt;useState&lt;/code&gt; comes in handy here.&lt;/p&gt;

&lt;p&gt;With that in mind, here's how framer-motion implemented &lt;code&gt;AnimatePresence&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;Imagine there's reality and alternate reality. Picture React's version of the DOM tree as reality, and framer-motion's version as alternate reality. Because we want to enhance our React codebase with exiting animations, let's call this 'alternate reality' solely for the purpose of this article. 'Alternate reality' is what we want to show to users of our app, or whatever we're building, so that they see the fancy-schmancy version instead of the dull, old vanilla version of things.&lt;/p&gt;

&lt;p&gt;Two states comes into play in our alternate reality:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;currentChildren&lt;/code&gt;: elements that are in the React tree - our source of truth&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;allChildren&lt;/code&gt;: elements that are in the React tree (i.e. &lt;code&gt;currentChildren&lt;/code&gt;) + elements that are &lt;em&gt;exiting&lt;/em&gt; the DOM&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Suppose that we have &lt;code&gt;Child1&lt;/code&gt; and &lt;code&gt;Child2&lt;/code&gt; wrapped within &lt;code&gt;AnimatePresence&lt;/code&gt;, and none of them are exiting the DOM yet. Here's what the above two states like would look in framer-motion:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0WG9LhL4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cr2rhnby2bf44n9fw9oe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0WG9LhL4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cr2rhnby2bf44n9fw9oe.png" alt="Before any exit animations" width="800" height="354"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, imagine if &lt;code&gt;Child2&lt;/code&gt; needs to be removed and we need to trigger its exit animation.&lt;/p&gt;

&lt;p&gt;React updates &lt;code&gt;currentChildren&lt;/code&gt; once &lt;code&gt;Child2&lt;/code&gt; is removed, showing that only &lt;code&gt;Child1&lt;/code&gt; remains. From this, we can derive a third state: &lt;code&gt;exitingChildren&lt;/code&gt;, which takes the difference between &lt;code&gt;allChildren&lt;/code&gt; and &lt;code&gt;currentChildren&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This third state, &lt;code&gt;exitingChildren&lt;/code&gt;, is crucial. Firstly, we want to trigger the exit animations once an element is registered as 'exiting'. Secondly, we want to ensure that this particular element is deleted from &lt;code&gt;AnimatePresence&lt;/code&gt; so that we don't keep unnecessary references to elements that don't exist in the React tree.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cNuWM1BB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bp0dseqvyvfnoo2jdhuh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cNuWM1BB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bp0dseqvyvfnoo2jdhuh.png" alt="Child2 being removed from React tree, right before it exits the tree" width="800" height="204"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is what the final state looks like after &lt;code&gt;Child2&lt;/code&gt; has completely exited the DOM: &lt;code&gt;AnimatePresence&lt;/code&gt; does not maintain a trace to it anymore.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5orU0T4P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wmq4ils01sxgjd111nlx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5orU0T4P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wmq4ils01sxgjd111nlx.png" alt="Child2 finishes its exit animation" width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So there you have it - it's a bit of React trickery behind the scenes! The framer-motion state names have been altered for the purposes of this article, but this distills the essence of how &lt;code&gt;AnimatePresence&lt;/code&gt; works.&lt;/p&gt;

&lt;p&gt;P.s. while reading the implementation of &lt;code&gt;AnimatePresence&lt;/code&gt;, I've noticed &lt;a href="https://github.com/framer/motion/pull/2477"&gt;a bug which caused some components to not exit in sequence&lt;/a&gt;. The &lt;a href="https://github.com/framer/motion/pull/2477"&gt;PR&lt;/a&gt; for fixing this has been submitted and is awaiting review.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/1784664/what-is-the-difference-between-declarative-and-imperative-paradigm-in-programmin"&gt;What is the difference between declarative and imperative paradigm in programming?&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;&lt;a href="https://www.framer.com/motion/animate-presence/"&gt;AnimatePresence in Motion docs&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>opensource</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
