<?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: Vitor Capretz</title>
    <description>The latest articles on DEV Community by Vitor Capretz (@vcapretz).</description>
    <link>https://dev.to/vcapretz</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%2F54438%2Fd7077526-16c2-48a1-b1e4-f701e0577f0b.jpg</url>
      <title>DEV Community: Vitor Capretz</title>
      <link>https://dev.to/vcapretz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vcapretz"/>
    <language>en</language>
    <item>
      <title>Instagram Like button in React Native and Reanimated v2</title>
      <dc:creator>Vitor Capretz</dc:creator>
      <pubDate>Mon, 18 Jan 2021 09:56:30 +0000</pubDate>
      <link>https://dev.to/vcapretz/instagram-like-button-in-react-native-and-reanimated-v2-3h3k</link>
      <guid>https://dev.to/vcapretz/instagram-like-button-in-react-native-and-reanimated-v2-3h3k</guid>
      <description>&lt;p&gt;One of the ways to make an app feel more polished is by introducing animations and transitions that lead to better visual feedback when users are interacting with your app.&lt;/p&gt;

&lt;p&gt;For me personally, it has been fun looking for small things that I can do and will make up a great addition in the end.&lt;/p&gt;

&lt;p&gt;Let's explore how to bring a better experience when users are toggling a "like" button by reproducing (or trying to) the Instagram version.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Reanimated?
&lt;/h2&gt;

&lt;p&gt;React Native has its own animation APIs and they work great for a lot of cases. The issue is that for more complex scenarios it will not support running those animations in the native thread, which can cause performance issues and unresponsiveness since it then competes for resources with JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/software-mansion/react-native-reanimated" rel="noopener noreferrer"&gt;React Native Reanimated&lt;/a&gt; is a library that allows developers to write smooth animations on React Native by making sure they are rendered in the native thread and not blocking JavaScript from handling other stuff at the same time.&lt;/p&gt;

&lt;p&gt;They recently rewrote the entire library by re-thinking both the architecture and all of the APIs they provide.&lt;/p&gt;

&lt;p&gt;One of the goals being to make it easier for developers to use and understand, so I've been using version 2 for some time and I think you should too!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As of today the v2 is in &lt;a href="https://en.wikipedia.org/wiki/Software_release_life_cycle#Release_candidate" rel="noopener noreferrer"&gt;Release candidate&lt;/a&gt; so it will soon be ready for production.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  A simple "like" button
&lt;/h2&gt;

&lt;p&gt;To start a simple like button, we would need a component that can handle a press event, a state to toggle between &lt;strong&gt;liked&lt;/strong&gt; and &lt;strong&gt;not-liked&lt;/strong&gt;, and an icon.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you want to follow along go to your terminal and hit &lt;code&gt;npx crna --template with-reanimated2&lt;/code&gt; to start a brand new React Native project with Expo and Reanimated v2 installed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The minimal code would look like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&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;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Pressable&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;react-native&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;MaterialCommunityIcons&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;@expo/vector-icons&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;LikeButton&lt;/span&gt; &lt;span class="o"&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;liked&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLiked&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Pressable&lt;/span&gt; &lt;span class="na"&gt;onPress&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setLiked&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;isLiked&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;isLiked&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MaterialCommunityIcons&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;liked&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;heart&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;heart-outline&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;liked&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;black&lt;/span&gt;&lt;span class="dl"&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;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Pressable&lt;/span&gt;&lt;span class="p"&gt;&amp;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;So we used:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;useState&lt;/code&gt; to store and toggle the &lt;em&gt;like&lt;/em&gt; value;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Pressable&lt;/code&gt; component to handle the press event, toggling the state based on the current previous value of &lt;code&gt;liked&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MaterialCommunityIcons&lt;/code&gt; component that either shows an outline version of the heart or the filled one based on the state.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you're following along and you render that component in a page you will have a result like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzh01h1wzlq9d27qjy9cl.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzh01h1wzlq9d27qjy9cl.gif" alt="Simple heart button which reacts to a press event, toggling between an outline state and a red filled heart one"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It works, but it's boring.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introducing animations
&lt;/h2&gt;

&lt;p&gt;Instagram's Like button brings a nice experience by fading in and out with &lt;strong&gt;scale&lt;/strong&gt; its state when it's pressed.&lt;/p&gt;

&lt;p&gt;The strategy is then to scale the non-liked version from &lt;strong&gt;1&lt;/strong&gt; to &lt;strong&gt;0&lt;/strong&gt; (making it disappear) and then the liked version from &lt;strong&gt;0&lt;/strong&gt; to &lt;strong&gt;1&lt;/strong&gt; (making it appear on top).&lt;/p&gt;

&lt;p&gt;We start by having two icons stacked on top of each other instead of only one:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Pressable&lt;/span&gt; &lt;span class="na"&gt;onPress&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setLiked&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;isLiked&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;isLiked&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Animated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;View&lt;/span&gt;
    &lt;span class="na"&gt;style&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="nx"&gt;StyleSheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;absoluteFillObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="na"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;liked&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="mi"&gt;1&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="si"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MaterialCommunityIcons&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;heart-outline&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;black&lt;/span&gt;&lt;span class="dl"&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;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Animated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Animated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;View&lt;/span&gt; &lt;span class="na"&gt;style&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;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="na"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;liked&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="mi"&gt;0&lt;/span&gt; &lt;span class="p"&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;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MaterialCommunityIcons&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;heart&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;red&lt;/span&gt;&lt;span class="dl"&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;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Animated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Pressable&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Animated.View&lt;/code&gt; is used to wrap the icons, which we will animate. import the &lt;code&gt;Animated&lt;/code&gt; from &lt;code&gt;react-native-reanimated&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;We use absolute positioning in the first view, so the other one can stay on top;&lt;/li&gt;
&lt;li&gt;We apply the &lt;code&gt;scale&lt;/code&gt; styles based on the state's value, nothing has changed so far. Still boring.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With this setup, we can now animate the scale. Exciting!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;react-native-reanimated&lt;/code&gt; provides some very useful hooks for us, we will use them.&lt;/p&gt;

&lt;p&gt;We change React's &lt;code&gt;useState&lt;/code&gt; by Reanimated's &lt;code&gt;useSharedValue&lt;/code&gt;, which also gives us a state, but this one can be used in both JavaScript and Native threads.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;liked&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useSharedValue&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;We will now use numbers instead of booleans so we can interpolate and use them directly into the styles as well.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code&gt;onPress&lt;/code&gt; event now has to be changed too since we don't have &lt;code&gt;setState&lt;/code&gt; anymore:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;onPress&lt;/span&gt;&lt;span class="o"&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="nx"&gt;liked&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;withSpring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;liked&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Note that we use &lt;code&gt;.value&lt;/code&gt; here to reference the actual value of the state.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;withSpring&lt;/code&gt; is part of the magic, that is a method provided by Reanimated that tells it to change the animated value using a spring animation with default values.&lt;/p&gt;

&lt;p&gt;Finally, we need to update the styles correctly based on the animated value, we will use &lt;code&gt;useAnimatedStyle&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;outlineStyle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useAnimatedStyle&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;transform&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="na"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;interpolate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;liked&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;Extrapolate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;CLAMP&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fillStyle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useAnimatedStyle&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;transform&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="na"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;liked&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;useAnimatedStyle&lt;/code&gt; hook receives a function that returns a style that gets updated based on animation values, we moved the inline styles from &lt;code&gt;Animated.View&lt;/code&gt; to this hook instead.&lt;/p&gt;

&lt;p&gt;For the &lt;em&gt;outline&lt;/em&gt; style we need to interpolate the animated value in the opposite way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;liked.value&lt;/code&gt; is 0 it means the scale for the outline style should be 1, if &lt;code&gt;liked.value&lt;/code&gt; is 1 then the scale should be 0.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;em&gt;fill&lt;/em&gt; style needs no interpolation since it follows &lt;code&gt;liked.value&lt;/code&gt; linearly.&lt;/p&gt;

&lt;p&gt;Here's the component so far and the current result:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;LikeButton&lt;/span&gt; &lt;span class="o"&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="nx"&gt;liked&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useSharedValue&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;outlineStyle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useAnimatedStyle&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;transform&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="na"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;interpolate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;liked&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;Extrapolate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;CLAMP&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fillStyle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useAnimatedStyle&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;transform&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="na"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;liked&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&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="k"&gt;return &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;Pressable&lt;/span&gt; &lt;span class="na"&gt;onPress&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;liked&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;withSpring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;liked&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&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="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="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Animated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;View&lt;/span&gt; &lt;span class="na"&gt;style&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="nx"&gt;StyleSheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;absoluteFillObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;outlineStyle&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MaterialCommunityIcons&lt;/span&gt;
          &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;heart-outline&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
          &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;black&lt;/span&gt;&lt;span class="dl"&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;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Animated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Animated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;View&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;fillStyle&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MaterialCommunityIcons&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;heart&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;red&lt;/span&gt;&lt;span class="dl"&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;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Animated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Pressable&lt;/span&gt;&lt;span class="p"&gt;&amp;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;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9shrmxblhmfduxl11dfq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9shrmxblhmfduxl11dfq.gif" alt="A heart icon that toggles based on a press event but this time with an animation, which fades out the outline version, bringing in the filled one"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That is much better 🎉&lt;/p&gt;

&lt;p&gt;If you pay close attention you'll see that the filled icon bounces too much in the end and can still be seen.&lt;/p&gt;

&lt;p&gt;We can fix that by also styling the opacity, so it's not visible.&lt;/p&gt;

&lt;p&gt;Change the &lt;code&gt;fillStyle&lt;/code&gt; so it looks like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fillStyle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useAnimatedStyle&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="na"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;liked&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&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="nx"&gt;liked&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&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;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F15htiu34xbmdbaltrq9a.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F15htiu34xbmdbaltrq9a.gif" alt="A heart icon that toggles based on a press event but this time with an animation, which fades out the outline version, bringing in the filled one"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nice one!&lt;/p&gt;

&lt;p&gt;I posted the completed example in this &lt;a href="https://gist.github.com/vcapretz/e7b172154774e24487d3c46677b08de2" rel="noopener noreferrer"&gt;gist with the entire component&lt;/a&gt; so you can more easily understand it and even copy/paste in our own apps.&lt;/p&gt;

&lt;h2&gt;
  
  
  We're done
&lt;/h2&gt;

&lt;p&gt;It's always interesting to explore these smaller interactions that can make a real difference in an app, let me know if you have any others you usually apply by &lt;a href="https://twitter.com/vcapretz" rel="noopener noreferrer"&gt;reaching out on Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I hope you learned something fun, and as always, I'm open to any kind of feedback at all you might have for me.&lt;/p&gt;

&lt;p&gt;Thank's for your time 👋&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This post was initially published in &lt;a href="https://vcapretz.com" rel="noopener noreferrer"&gt;my own blog&lt;/a&gt; 😄&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>reactnative</category>
      <category>reanimated</category>
      <category>instagram</category>
    </item>
    <item>
      <title>7 tips on how to switch teams inside your company</title>
      <dc:creator>Vitor Capretz</dc:creator>
      <pubDate>Wed, 24 Jun 2020 14:53:05 +0000</pubDate>
      <link>https://dev.to/vcapretz/7-tips-on-how-to-switch-teams-inside-your-company-54ce</link>
      <guid>https://dev.to/vcapretz/7-tips-on-how-to-switch-teams-inside-your-company-54ce</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;First of all, I just want to acknowledge that I'm privileged enough to be in a company where I have good relationships with my managers, and where I can switch teams without a negative impact on my career. I hope other companies take note of this.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Sometimes change is not only inevitable but natural. Your goals might change, and you might want to make career moves that better align with your current goals. That might be focusing on different technologies, assuming more responsibility, working closely with people that can teach you a lot about a certain subject, or simply working on a different set of projects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And to achieve that, you might not need to look externally, but internally.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;My previous team was focused much more on Backend tasks. I really care about UX, design, and Frontend in general, that’s where I wanted to focus my career, and the natural solution would be to maybe look at different companies.&lt;/p&gt;

&lt;p&gt;However, I love my colleagues, the company is exciting and I feel like I could do much more here. And then the stars aligned... &lt;/p&gt;

&lt;p&gt;Last year, around September, the company I work for announced some structural changes. Some new teams were being created, and I instantly spotted one that sparked my interest. I directly scheduled a meeting with my manager to express that I would like to be a part of that team.&lt;/p&gt;

&lt;p&gt;Here are some things I learned about the process of switching teams and that may be useful to you as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  1 - Be aware
&lt;/h2&gt;

&lt;p&gt;No matter the size of the company you work for, it will go through changes over time. New teams will be created, old teams might be merged with others, new products will arrive, and old products will be deprecated and much more.&lt;/p&gt;

&lt;p&gt;If you are aware of these changes you will already have a head start for when you're looking for new opportunities.&lt;/p&gt;

&lt;p&gt;Company-wide meetings can be worth attending as when changes are announced you are one of the first to know, and thus, one of the first to act. Maybe your company has an internal website with newsletters that you can subscribe to. Or your manager can tell you about the overall direction of the company over one-to-one meetings.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Be proactive, be aware.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2 - Be open
&lt;/h2&gt;

&lt;p&gt;Openly express that you're available to different opportunities that might give you advantages over time.&lt;/p&gt;

&lt;p&gt;If one of these new teams needs people with your current set of skills, and you have already mentioned to some people you would like to jump on different projects, your name may naturally come up in early-stage conversation.&lt;/p&gt;

&lt;p&gt;By being open, you might be presented with interesting opportunities.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Be open, be ready.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3 - Maintain good relationships
&lt;/h2&gt;

&lt;p&gt;In my case, I was called to a temporary project where I met and worked with some senior engineers. When this new team was being created they were the first to be included. So when I said that I would like to be part of that team and they were asked if it was a good idea, I already had them on my side.&lt;/p&gt;

&lt;p&gt;Maintaining good relationships with your colleagues is key and you never know when you might need them supporting you.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Be nice, maintain good relationships.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4 - Create your image
&lt;/h2&gt;

&lt;p&gt;In my particular case, where I'm working for a company with over 2000 employees, it can be very hard to be noticed for the work you do outside your team.&lt;/p&gt;

&lt;p&gt;It's impossible for you to meet every engineer, every product manager, and keep track of all the people coming and going.&lt;/p&gt;

&lt;p&gt;What you can do is to produce internal blog posts, host talks internally, offer help in open Slack channels and ask publicly when you have issues setting up some projects for instance.&lt;/p&gt;

&lt;p&gt;Being known can give you advantages since people might already know you're a good professional and that you're a nice person to work with. It's not about being "famous" or having popularity for the sake of being cool, but it is about getting your work noticed and increasing your network.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Produce content, create your image.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5 - Be clear
&lt;/h2&gt;

&lt;p&gt;Be clear about your career goals with your manager, tell them constantly about what you're trying to achieve, and what makes you happy at work.&lt;/p&gt;

&lt;p&gt;This can be useful not only if you decide to switch teams, but if you’re clear, you might not even get to the point where you feel the need to switch teams.&lt;/p&gt;

&lt;p&gt;If you’re clear with your goals, a good manager can help you align your goals within your current team.&lt;/p&gt;

&lt;p&gt;But if you do come to the point where you decide to switch, they will be aware of your motives, they will not have hard feelings or have the belief that this came "out of nowhere".&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Communicate more, be clear about what you’re trying to achieve.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  6 - Give a reasonable notice period
&lt;/h2&gt;

&lt;p&gt;This is usually a requirement that is even stated in your contract when you join a company. It states that if you would leave, you have to first complete X weeks/months for the notice period.&lt;/p&gt;

&lt;p&gt;That can also apply when switching teams internally.&lt;/p&gt;

&lt;p&gt;Even if it may not be a hard requirement, it's better for you to align with your current managers and have a reasonable notice period, after all, you may even go back to working with them.&lt;/p&gt;

&lt;p&gt;If your team is the middle of a big project, it might be a good idea for you to stick around longer until they launch the MVP (Minimum Viable Product), or they might want you to help onboard a person filling your current position.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Be empathetic, give a notice period before switching to new opportunities.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  7 - Be kind
&lt;/h2&gt;

&lt;p&gt;I believe kindness is the key to so many things in life.&lt;/p&gt;

&lt;p&gt;If you're switching teams, thank everyone you worked with, they all might have taught you a lot of things in the time you worked together.&lt;/p&gt;

&lt;p&gt;Maybe even tell them that they can reach out if they come across one of the lines of code you wrote that is not clear for them.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Be thankful, be kind.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Many people are always looking for improvements in their careers.&lt;/p&gt;

&lt;p&gt;By that I mean they are often looking for challenges, trying to work on interesting projects, expanding their networks, getting their work recognized, or trying to impact more and more people.&lt;/p&gt;

&lt;p&gt;That’s not any different for me.&lt;/p&gt;

&lt;p&gt;But maybe like me, you faced a dilemma: you liked your company, your colleagues and the business. But, you still knew you were ready for different challenges.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Instead of looking outside, I decided to look inside.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Importantly, when I decided to switch teams I made sure I would be on good terms with everyone involved. I like the company I'm working for and I want to make the best out of my time.&lt;/p&gt;

&lt;p&gt;I hope you do too!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/vcapretz"&gt;Follow me on Twitter @vcapretz&lt;/a&gt; and send me your thoughts about this subject, I would love to hear your feedback! &lt;/p&gt;

&lt;p&gt;I hope you have a great day! 👋&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Cover picture by &lt;a href="https://unsplash.com/@epicantus?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Daria Nepriakhina&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/teams?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>career</category>
    </item>
    <item>
      <title>Writing modern Redux in 2020 - The powerful createSlice</title>
      <dc:creator>Vitor Capretz</dc:creator>
      <pubDate>Fri, 24 Jan 2020 15:38:40 +0000</pubDate>
      <link>https://dev.to/vcapretz/writing-modern-redux-in-2020-the-powerful-createslice-1i5m</link>
      <guid>https://dev.to/vcapretz/writing-modern-redux-in-2020-the-powerful-createslice-1i5m</guid>
      <description>&lt;p&gt;In my last post, I wrote a short &lt;a href="https://dev.to/vcapretz/writing-modern-redux-in-2020-redux-toolkit-b9"&gt;introduction to Redux Toolkit&lt;/a&gt; and its basic functionalities. But I still need to talk about their most important function - called &lt;code&gt;createSlice&lt;/code&gt; - which is probably the only one you will need to use for most applications you're writing.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is &lt;code&gt;createSlice&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;If you've ever built an application with Redux you probably ended up with a store that contains multiple reducers, each of them dealing with certain parts of the app. Then you combine all of them in what's usually called a &lt;em&gt;rootReducer&lt;/em&gt;, which is then used to create the store itself.&lt;/p&gt;

&lt;p&gt;So your store creation would look something like: &lt;/p&gt;


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


&lt;p&gt;Each of these key-value pairs is a "slice" of your application. You can read more about the logic for splitting reducers in the &lt;a href="https://redux.js.org/recipes/structuring-reducers/splitting-reducer-logic/"&gt;Redux documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;createSlice&lt;/code&gt; comes in handy because it deals with everything you need for each &lt;em&gt;slice&lt;/em&gt;, so instead of calling &lt;code&gt;createAction&lt;/code&gt; and &lt;code&gt;createReducer&lt;/code&gt; manually, you would use one single function as a replacement.&lt;/p&gt;

&lt;p&gt;A basic example would be: &lt;/p&gt;


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


&lt;p&gt;Where the returned object from &lt;code&gt;createSlice&lt;/code&gt; will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;todos&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;newState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;actions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;addTodo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;payload&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="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;todos/addTodo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;}),&lt;/span&gt;
    &lt;span class="nx"&gt;toggleTodo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;payload&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="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;todos/toggleTodo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;caseReducers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;addTodo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;newState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;toggleTodo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;newState&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;So now we've got everything we need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;name&lt;/code&gt; parameter will be the prefix for all of your action types,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;initialState&lt;/code&gt; is used for the store;&lt;/li&gt;
&lt;li&gt;The reducers are again methods in an object, where they can still mutate the state directly - thanks to &lt;a href="https://github.com/immerjs/immer"&gt;immer&lt;/a&gt; - it works exactly like &lt;code&gt;createReducer&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;One action is created for each reducer, they all have the same function signature and the &lt;em&gt;type&lt;/em&gt; property is derived from the slice name plus the reducer method - like "todos/addTodo" and "todos/toggleTodo";&lt;/li&gt;
&lt;li&gt;The actions receive only a "payload" parameter, which is recommended according to the &lt;a href="https://github.com/redux-utilities/flux-standard-action"&gt;Flux Standard Action pattern&lt;/a&gt;. If you need to pass in more than one parameter, put them in an object.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Working with multiple slices
&lt;/h2&gt;

&lt;p&gt;Most projects will contain many slices, it's a nice approach to separate them by features so they contain smaller logic for handling each part of the store. For these projects &lt;code&gt;createSlice&lt;/code&gt; is probably all you're going to need, where each slice should be in separate files containing a default export for the reducer and named exports for the actions.&lt;/p&gt;

&lt;p&gt;Putting it all together you would end up with something like:&lt;/p&gt;


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



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

&lt;p&gt;Since Redux Toolkit is an opinionated tool and Redux has been more explicit about what rules you should follow and how you should structure your app, you can have the peace of mind that your apps are going to follow those rules by default.&lt;/p&gt;

&lt;p&gt;I believe that now we are all able to write better applications with Redux, removing a lot of the boilerplate code we wrote in the past years and making it easier to maintain our codebases. Software development keeps evolving and sometimes it's natural we need to learn new things. I hope now you have enough information to start using Redux Toolkit in your projects!&lt;/p&gt;

&lt;p&gt;Please check &lt;a href="https://redux-toolkit.js.org/tutorials/intermediate-tutorial"&gt;this page with a step by step guide on how to migrate to the new approach&lt;/a&gt;. And if you want to understand more deeply about some of the guidelines mentioned in this article check the &lt;a href="https://redux.js.org/style-guide/style-guide"&gt;Redux Style Guide page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;All feedback is welcome! Comment below if you have any questions and like the post if you enjoyed reading it 😄&lt;/p&gt;

&lt;p&gt;Feel free to follow me - &lt;a href="https://twitter.com/vcapretz"&gt;@vcapretz&lt;/a&gt; - on Twitter to keep in touch, my DM's are always open!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thank you &lt;a href="https://github.com/erikengervall"&gt;Erik Engervall&lt;/a&gt; for reviewing the post for me!&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Cover photo by &lt;a href="https://unsplash.com/@_evstratov_?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Денис Евстратов&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/modern?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>redux</category>
      <category>javascript</category>
      <category>reactredux</category>
    </item>
    <item>
      <title>Writing modern Redux in 2020 - Redux Toolkit</title>
      <dc:creator>Vitor Capretz</dc:creator>
      <pubDate>Sun, 19 Jan 2020 19:58:58 +0000</pubDate>
      <link>https://dev.to/vcapretz/writing-modern-redux-in-2020-redux-toolkit-b9</link>
      <guid>https://dev.to/vcapretz/writing-modern-redux-in-2020-redux-toolkit-b9</guid>
      <description>&lt;p&gt;I wanted to build a React Native app from scratch so I could learn new technologies and find out what I could bring to my workplace and make our app better. Trying out new technologies might be harder to do when you're working in a team for an app that already has millions of users and a couple of hundreds of contributors, such as we do at Klarna. &lt;/p&gt;

&lt;p&gt;So I found a &lt;a href="https://learn.handlebarlabs.com/p/react-native-basics-build-a-currency-converter"&gt;React Native tutorial&lt;/a&gt; which was simple enough for me to try stuff like &lt;a href="https://github.com/wix/react-native-navigation"&gt;react-native-navigation&lt;/a&gt;, &lt;a href="https://github.com/styled-components/styled-components"&gt;styled-components&lt;/a&gt; and learn how to use Redux in a more modern way. &lt;/p&gt;

&lt;h2&gt;
  
  
  What do I mean by "Modern Redux"?
&lt;/h2&gt;

&lt;p&gt;In tech, things evolve quickly and new versions of libraries we use on a daily basis are released with a frequency that makes us wonder every once in a while what the cost of updating such libraries is.&lt;/p&gt;

&lt;p&gt;Keeping libraries up to date is important for a variety of reasons, e.g. security-vulnerabilities fixes, performance improvements and sometimes even easier implementation.&lt;/p&gt;

&lt;p&gt;When talking about React, we recently had a huge update introducing &lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;Hooks&lt;/a&gt;, which enabled a lot of the libraries in the ecosystem to leverage this approach and to give developers the possibility of moving out from the &lt;em&gt;class&lt;/em&gt; approach if they wanted to.&lt;/p&gt;

&lt;p&gt;react-redux introduced some &lt;a href="https://react-redux.js.org/api/hooks"&gt;hooks&lt;/a&gt; too, which I felt was a great addition. The Redux team also introduced &lt;a href="https://redux-toolkit.js.org/"&gt;@reduxjs/toolkit&lt;/a&gt; which is what I'm going to talk about in this post.&lt;/p&gt;

&lt;h2&gt;
  
  
  Redux architecture and too much boilerplate
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Quick disclaimer: I'm not recommending Redux nor am I going to discuss whether it's a good solution for your app, you make that decision according to your own needs and constraints.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When doing the React Native tutorial the author himself was constantly writing the default boilerplate code for setting up a Redux store with its action dispatchers, reducers and a bunch of files for each of these things. &lt;/p&gt;

&lt;p&gt;A common approach is to have a folder that contains a file for the actions, a file for the reducer and probably a file for shared constants for action names. &lt;/p&gt;

&lt;p&gt;But this causes discussions on whether or not they should be separate files, how you are going to deal with naming conventions, the selectors you're going to declare, etc. &lt;/p&gt;

&lt;p&gt;Some other issues when setting up a Redux store also involve remembering not to mutate the store inside the reducers, choosing and setting up middlewares (e.g. for &lt;em&gt;async&lt;/em&gt; actions).&lt;/p&gt;

&lt;p&gt;Talking to a friend at Klarna about my frustrations and how I was bored just by thinking about all of this, he introduced me to the Redux Toolkit.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the Redux toolkit?
&lt;/h2&gt;

&lt;p&gt;The above-mentioned issues (and probably a bunch more) are actually quite common and a lot of people have also shown their frustrations with them. So the Redux team came up with an opinionated toolkit to help us developers move faster by taking some decisions for us whilst making sure we don't break the conventions.&lt;/p&gt;

&lt;p&gt;Keep in mind that anything that is opinionated might still bring frustrations for whoever doesn't agree with those opinions, but if they are from the very same people that maintain a library and set the standards for it, I would say it's good enough to trust it and move on so we can focus much more on the users and what they actually need instead of bikeshedding the same problems over and over again.&lt;/p&gt;

&lt;p&gt;So the Redux toolkit is an opinionated tool that will handle all the common scenarios for you, removing a lot of the boilerplate code. While it will not solve all the problems you might have with Redux, it does bring benefits to the common scenarios.&lt;/p&gt;

&lt;p&gt;Do check their docs for more examples, insights, and reasoning. But let's explore it a bit.&lt;/p&gt;

&lt;h3&gt;
  
  
  configureStore, createAction and createReducer
&lt;/h3&gt;

&lt;p&gt;To create a simple Redux store you can use these three functions as replacements for the conventional approach.&lt;/p&gt;

&lt;p&gt;I'll briefly introduce each of them and then show some code with each of them applied.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;configureStore&lt;/code&gt; replaces &lt;code&gt;createStore&lt;/code&gt;, where you can still pass your reducers and middlewares as parameters.&lt;br&gt;
It provides some useful default middlewares (some of them are applied only in a development environment), &lt;code&gt;redux-thunk&lt;/code&gt; is the chosen library as a solution for &lt;em&gt;async&lt;/em&gt; actions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;createAction&lt;/code&gt; removes some of the boilerplate you usually have to write for each action, like the &lt;code&gt;type&lt;/code&gt; parameter, how the &lt;code&gt;payload&lt;/code&gt; looks and the argument name you will use.&lt;br&gt;
The action's &lt;code&gt;type&lt;/code&gt; is also something you need to share with the reducer so people usually have to set up a constants file to keep track of it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;createReducer&lt;/code&gt; is the last piece of the puzzle and the one with the most interesting differences compared to the conventional approach.&lt;br&gt;
Instead of declaring a &lt;code&gt;switch/case&lt;/code&gt; for each action type, you can use the actions themselves as parameters and have methods for how each of them should change the store's state.&lt;br&gt;
Because &lt;code&gt;createReducer&lt;/code&gt; uses &lt;em&gt;immer&lt;/em&gt; (a library that creates the next immutable state tree while mutating the current one), you can actually mutate the state in any way you want, while still preserving the immutability required by Redux.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Code examples
&lt;/h3&gt;

&lt;p&gt;So now you're probably wondering how all of this is actually implemented. I'll introduce some examples to show the basic scenarios.&lt;/p&gt;


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


&lt;p&gt;The above implementation is one of the most straight forward ones, but it does show the main differences with the conventional approach:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;While you do have to pass a unique identifier to the actions, you don't need to share them with the reducers. Each action now has a &lt;code&gt;toString&lt;/code&gt; method which returns that identifier, so we use that as keys to the reducer object;&lt;/li&gt;
&lt;li&gt;We pass the initial state as the first parameter for &lt;code&gt;createReducer&lt;/code&gt; and an object as the second one;&lt;/li&gt;
&lt;li&gt;The reducer itself doesn't contain a &lt;code&gt;switch/case&lt;/code&gt; statement, each action handler is now a key in an object, we don't need to worry about unknown actions here since it will simply return the current state as it is.&lt;/li&gt;
&lt;li&gt;Because we are using &lt;code&gt;configureStore&lt;/code&gt;, the toolkit is adding some default middlewares for us, you can check the exact ones in the &lt;a href="https://redux-toolkit.js.org/api/getDefaultMiddleware"&gt;API Reference for &lt;code&gt;getDefaultMiddleware&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This next example shows how we can mutate the state inside each action handler and how the toolkit handles this for us:&lt;/p&gt;


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


&lt;p&gt;Pushing items to an array or modifying a value inside it directly is not recommended when writing conventional Redux, while it's nice to have immutability it may cause more confusion and be less straightforward.&lt;/p&gt;

&lt;p&gt;This is what it would look like if you were to avoid direct mutation:&lt;/p&gt;


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


&lt;p&gt;You may or may not agree with this piece of code, but now we have the option to use it.&lt;/p&gt;

&lt;p&gt;That's it! With these examples, I hope you now have some understanding of the Redux toolkit. The toolkit also introduces a function called &lt;code&gt;createSlice&lt;/code&gt;, I also didn't mention the hooks you can use with React and how to write &lt;em&gt;async&lt;/em&gt; actions using &lt;code&gt;redux-thunk&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let me know if you're interested in those subjects and I’ll write more posts in the future.&lt;/p&gt;

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

&lt;p&gt;This was a short introduction with the goal to document what I discovered about the toolkit and also, hopefully, to spark some curiosity in you. &lt;/p&gt;

&lt;p&gt;If you want to know more, please go to the &lt;a href="https://redux-toolkit.js.org/introduction/quick-start"&gt;Redux toolkit quick start&lt;/a&gt; and then try it out in your application if you're already using Redux.&lt;/p&gt;

&lt;p&gt;Please let me know if you have any feedback about this article and &lt;a href="https://twitter.com/vcapretz"&gt;follow me on Twitter - @vcapretz&lt;/a&gt; to keep in touch! &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cover image by &lt;a href="https://unsplash.com/@pixelatelier?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Christian Holzinger&lt;/a&gt; on &lt;a href="https://unsplash.com/?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>redux</category>
      <category>react</category>
      <category>javascript</category>
      <category>reactredux</category>
    </item>
    <item>
      <title>Is it worth reading source code? </title>
      <dc:creator>Vitor Capretz</dc:creator>
      <pubDate>Fri, 19 Jan 2018 15:32:22 +0000</pubDate>
      <link>https://dev.to/vcapretz/does-it-worth-reading-source-codes-55n2</link>
      <guid>https://dev.to/vcapretz/does-it-worth-reading-source-codes-55n2</guid>
      <description>&lt;p&gt;Hey! I was wondering about reading source codes of libraries that you may use on a daily-basis.&lt;/p&gt;

&lt;p&gt;Like, if I decided to learn React and Redux, it's worth taking some time to go to the source codes and dig deep into how it works? Or it's not necessary and just using it a lot will make me understand enough about it? &lt;/p&gt;

&lt;p&gt;I don't have some clearly opinions about it and I never had really looked to a source code of something big. &lt;/p&gt;

&lt;p&gt;Sometimes maybe it's easier because in the React example, it's also written in JavaScript, but if I wanted to understand de Node.js core I would have to understand C++, and it would take more time.&lt;/p&gt;

&lt;p&gt;What do you think? &lt;/p&gt;

</description>
      <category>discuss</category>
      <category>javascript</category>
      <category>react</category>
      <category>node</category>
    </item>
  </channel>
</rss>
