<?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>Forem: Charlie Charlson</title>
    <description>The latest articles on Forem by Charlie Charlson (@ch_charlson).</description>
    <link>https://forem.com/ch_charlson</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%2F316447%2F510c093a-39d9-4eee-bbae-bf8b7fc4625a.jpg</url>
      <title>Forem: Charlie Charlson</title>
      <link>https://forem.com/ch_charlson</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ch_charlson"/>
    <language>en</language>
    <item>
      <title>Fade-in and fade-out animation with spring and transition React Hooks</title>
      <dc:creator>Charlie Charlson</dc:creator>
      <pubDate>Mon, 10 Feb 2020 17:23:02 +0000</pubDate>
      <link>https://forem.com/atherosai/fade-in-and-fade-out-animation-with-spring-and-transition-react-hooks-4gli</link>
      <guid>https://forem.com/atherosai/fade-in-and-fade-out-animation-with-spring-and-transition-react-hooks-4gli</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The &lt;a title="React Spring" href="https://react-spring.io/"&gt;React Spring&lt;/a&gt; library has a modern way of approaching animations by using a physics-based model. You can configure animations using real-life values like &lt;strong&gt;mass&lt;/strong&gt;, &lt;strong&gt;friction&lt;/strong&gt;, and &lt;strong&gt;tension&lt;/strong&gt;, which allow you to create more believable transitions. There is also an option for duration-based approach. The library has great performance and an easy-to-use API. &lt;a title="React Spring" href="https://react-spring.io/"&gt;React Spring&lt;/a&gt; is cross-platform and has quite a optimal bundle size.&lt;/p&gt;

&lt;h2&gt;
  
  
  Render props API vs hooks API
&lt;/h2&gt;

&lt;p&gt;The release of &lt;a title="React Version 16.8" href="https://reactjs.org/blog/2019/02/06/react-v16.8.0.html"&gt;React version 16.8&lt;/a&gt; introduced hooks, which enable you to use &lt;strong&gt;state&lt;/strong&gt;, &lt;strong&gt;component methods&lt;/strong&gt;, and other features without writing a class. The React Spring team introduced a new Hooks API in version 7.0. At the moment, there are 5 React Spring hooks: &lt;strong&gt;useSpring&lt;/strong&gt;, &lt;strong&gt;useSprings&lt;/strong&gt;, &lt;strong&gt;useTrail&lt;/strong&gt;, &lt;strong&gt;useTransition&lt;/strong&gt;, and &lt;strong&gt;useChain&lt;/strong&gt;. Hooks allow us to create much cleaner code and avoid so-called &lt;strong&gt;render props hell&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GSewRpTs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0i5tdh87av2u5v73lm1v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GSewRpTs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0i5tdh87av2u5v73lm1v.png" alt="Render Props Hell"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing
&lt;/h2&gt;

&lt;p&gt;You can check out final code on our &lt;a title="GitHub" href="https://github.com/atherosai/next-react-graphql-apollo-hooks"&gt;GitHub&lt;/a&gt;. Clone GitHub repository using this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone git@github.com:atherosai/next-react-graphql-apollo-hooks.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Then install npm modules using:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;And finally, use the following command to run the node server:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Animating carousel, changing the text (fade-in, fade-out)
&lt;/h2&gt;

&lt;p&gt;In the following example we will code multiple changing titles, which you can apply to your carousel or whichever way you see fit.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YmIM2kQc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ctvvm76k36v8ssygjq9o.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YmIM2kQc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ctvvm76k36v8ssygjq9o.gif" alt="Fading Titles"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we want to use items that are added and removed, we need to import the &lt;strong&gt;useTransition&lt;/strong&gt; hook. When we import an &lt;strong&gt;animated&lt;/strong&gt;, we need to put it in front of &lt;strong&gt;div&lt;/strong&gt; to signalise that it is an &lt;strong&gt;animated div&lt;/strong&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Using React hooks we can initialise the items array, where each object contains a &lt;strong&gt;title&lt;/strong&gt; string, which will be shown on screen, and an &lt;strong&gt;id&lt;/strong&gt; number, which we will need later on in the &lt;strong&gt;transition&lt;/strong&gt; hooks to identify the specific &lt;strong&gt;title&lt;/strong&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Also, we need to define the &lt;strong&gt;index&lt;/strong&gt; and &lt;strong&gt;setIndex&lt;/strong&gt; state hooks to control the current index of &lt;strong&gt;items&lt;/strong&gt; shown on screen.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now we will finally add the &lt;strong&gt;transition&lt;/strong&gt; hook:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The first argument is the current item of the array; the second one is the item &lt;strong&gt;id&lt;/strong&gt;; and the third is the object where we define the three different stages of animation. These stages are as follows: an animation starts at the &lt;strong&gt;from&lt;/strong&gt; variable, goes to &lt;strong&gt;enter&lt;/strong&gt; and ends at &lt;strong&gt;leave&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For this animation, we are going to use the &lt;strong&gt;opacity&lt;/strong&gt; variable, but you can work with any CSS variable for other animations. In this &lt;strong&gt;config&lt;/strong&gt;, we set the &lt;strong&gt;tension to 220&lt;/strong&gt; and the &lt;strong&gt;friction to 120&lt;/strong&gt;, but try to play around with these values to see what they really do. You can also use some presets for this animation, but they have quite short durations. There is also an option to use a duration-based approach. If you want to use a precise time in milliseconds, simply type &lt;strong&gt;config: { duration: 1000 }&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;These lines in the function component adds a loop using the &lt;strong&gt;setInterval&lt;/strong&gt; function, where the current index is set to another one every 4 seconds. The &lt;strong&gt;modulo operator&lt;/strong&gt; expression assures that when the current index points to the last item, it gets set to the first item. Make sure you clear the interval to avoid memory leaks.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You have to create a &lt;strong&gt;map&lt;/strong&gt; function of the transition hooks with the following arguments. The first one is the current item of an array; props are the CSS values; and the key is the specific id you set in the &lt;strong&gt;useTransition&lt;/strong&gt; hook.&lt;/p&gt;

&lt;p&gt;Then you can add a special HTML tag, &lt;strong&gt;animated.div&lt;/strong&gt;, which activates the React Spring animation. For this example, I used &lt;strong&gt;inline styles&lt;/strong&gt; so you can see it more clearly. However, the only important style is the &lt;strong&gt;absolute&lt;/strong&gt; position in the &lt;strong&gt;div&lt;/strong&gt; container, so the texts can overlap each other.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;If you want to delve deeper into &lt;a title="React Spring" href="https://react-spring.io/"&gt;React Spring&lt;/a&gt; animations, visit the official &lt;a title="React Spring" href="https://react-spring.io/"&gt;React Spring&lt;/a&gt; website. You can learn different animation methods using &lt;strong&gt;useTrail&lt;/strong&gt; or &lt;strong&gt;useChain&lt;/strong&gt; hooks, for example, or explore the countless examples posted on the website. Also check out our &lt;a title="High-performance boilerplate" href="https://github.com/atherosai/next-react-graphql-apollo-hooks"&gt;high-performance boilerplate&lt;/a&gt; consisting of &lt;strong&gt;React&lt;/strong&gt;, &lt;strong&gt;Apollo&lt;/strong&gt;, &lt;strong&gt;Next.js&lt;/strong&gt;, &lt;strong&gt;GraphQL&lt;/strong&gt;, and &lt;strong&gt;Node.js&lt;/strong&gt;. &lt;a title="High-performance boilerplate" href="https://github.com/atherosai/next-react-graphql-apollo-hooks/blob/feat/typescript/components/HomePage/CarouselHooks/CarouselHooks.tsx"&gt;Here&lt;/a&gt; you can find the final code as well as two other basic loading animations for carousels.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Did you like this post? You can clone &lt;a title="GitHub repository" href="https://github.com/atherosai/next-react-graphql-apollo-hooks"&gt;the repository with the examples and project set-up&lt;/a&gt;. &lt;br&gt;
Feel free to send any questions about the topic to &lt;a href="mailto:karel@atheros.ai" title="Contact"&gt;&lt;/a&gt;&lt;a href="mailto:karel@atheros.ai"&gt;karel@atheros.ai&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>node</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
