<?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: akamittal</title>
    <description>The latest articles on DEV Community by akamittal (@akamittal).</description>
    <link>https://dev.to/akamittal</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%2F518309%2F100c1cff-8158-4adf-a6b6-9b1a714f97b9.png</url>
      <title>DEV Community: akamittal</title>
      <link>https://dev.to/akamittal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akamittal"/>
    <language>en</language>
    <item>
      <title>Backbone.js Model defaults and parse</title>
      <dc:creator>akamittal</dc:creator>
      <pubDate>Sat, 20 Mar 2021 14:34:18 +0000</pubDate>
      <link>https://dev.to/akamittal/backbone-js-model-defaults-and-parse-2pgk</link>
      <guid>https://dev.to/akamittal/backbone-js-model-defaults-and-parse-2pgk</guid>
      <description>&lt;p&gt;I’ve got this Backbone.Model representing a Google Books API volume:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var Book = Backbone.Model.extend({

    defaults: {
        volumeInfo : {
            title: 'n.a.',
            authors: 'n.a.',
            publisher: 'n.a.',
            publishedDate: 'n.a.',
            imageLinks : {
                smallThumbnail: '/unavailable.jpg'
            }
        }
    },

    parse: function(resp) {
        if (resp.volumeInfo.authors) {
            resp.volumeInfo.authors = resp.volumeInfo.authors.join(',');
        }
        return resp;
    }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which is fed to this template:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script type="text/template" id="bookCollectionRow"&amp;gt;
    &amp;lt;tr&amp;gt;
        &amp;lt;td&amp;gt;&amp;lt;img class="thumbnail" src="&amp;lt;%= volumeInfo.imageLinks.smallThumbnail %&amp;gt;" /&amp;gt;&amp;lt;/td&amp;gt;
        &amp;lt;td&amp;gt;&amp;lt;a target="_blank" href="&amp;lt;%= volumeInfo.canonicalVolumeLink %&amp;gt;"&amp;gt;&amp;lt;%= volumeInfo.title %&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;/td&amp;gt;
        &amp;lt;td&amp;gt;&amp;lt;%= volumeInfo.authors %&amp;gt;&amp;lt;/td&amp;gt;
        &amp;lt;td&amp;gt;&amp;lt;%= volumeInfo.publisher %&amp;gt;&amp;lt;/td&amp;gt;
        &amp;lt;td&amp;gt;&amp;lt;%= volumeInfo.publishedDate %&amp;gt;&amp;lt;/td&amp;gt;
    &amp;lt;/tr&amp;gt;
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Upon parsing the template, when a volume JSON does not contain an imageLinks I receive this error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Uncaught TypeError: Cannot read property 'smallThumbnail' of undefined.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I know I could fix it by checking with an if in the Model or in the template but what’s the purpose of defaults model property then? Does that work only if not overriding parse?&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;A few things. First, you shouldn’t have nested objects as backbone model attributes in general – it can be OK if you can always treat the attribute atomically, but this is a perfect example of when you can’t. From a data-model perspective, imageLinks should be its own backbone model class, as should volumeInfo.&lt;/p&gt;

&lt;p&gt;Second, if defaults is an object literal ({}) instead of a function, the same object is used as the default attrs for each model instance. I think you want this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;defaults: function(){
    return {
        volumeInfo : {} // should be new VolumeInfo({}) imo
    };
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But the data model is the bigger issue – .defaults doesn’t do the kind of nested-object-template thing you seem to be going for, and for good reason: it doesn’t work well, this will just be the first of many gotchas you’ll run into if you don’t keep your instance data pretty flat.&lt;/p&gt;

</description>
      <category>backbonejs</category>
    </item>
    <item>
      <title>Pass PHP array onto Vue component using props</title>
      <dc:creator>akamittal</dc:creator>
      <pubDate>Sat, 20 Mar 2021 14:29:58 +0000</pubDate>
      <link>https://dev.to/akamittal/pass-php-array-onto-vue-component-using-props-56g4</link>
      <guid>https://dev.to/akamittal/pass-php-array-onto-vue-component-using-props-56g4</guid>
      <description>&lt;ol&gt;
&lt;li&gt;Building a Laravel app with a few Vue components&lt;/li&gt;
&lt;li&gt;Want to pass a PHP array onto a Vue component using props&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here’s an example of such PHP array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;["Foo" =&amp;gt; 100, "Bar" =&amp;gt; 50]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great. Here’s my attempt at passing them onto the Chart component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Chart points="{!! json_encode($points) !!}"&amp;gt;&amp;lt;/Chart&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This looks fine, but the strings (Foo and Bar) inside the &lt;code&gt;$points&lt;/code&gt; array get encapsulated with ” (double quotes) when using &lt;code&gt;json_encode()&lt;/code&gt;. This means that whenever the 1st string appears in the array, the browser thinks that the ” is meant to close the points attribute.&lt;/p&gt;

&lt;p&gt;Here’s what you get to see in the browser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Chart points="{"&amp;gt;Foo":100,"Bar":50}"&amp;lt;/Chart&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How do I go about this? I have been struggling with this for hours now, and I can’t wrap my head around it.&lt;/p&gt;

&lt;p&gt;Can’t use ” (double quotes) since the browser interprets it as the closing quote for an attribute and messes up the HTML&lt;br&gt;
Can’t use ‘ (single quotes) since that’s invalid JSON (and json_encode doesn’t support it)&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Chart points='{!! json_encode($points) !!}'&amp;gt;&amp;lt;/Chart&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just use singular quotes.&lt;/p&gt;

</description>
      <category>vue</category>
      <category>php</category>
      <category>props</category>
    </item>
    <item>
      <title>React Native Animation – Easy, Step By Step Guide</title>
      <dc:creator>akamittal</dc:creator>
      <pubDate>Sat, 17 Oct 2020 17:25:17 +0000</pubDate>
      <link>https://dev.to/akamittal/react-native-animation-easy-step-by-step-guide-jfc</link>
      <guid>https://dev.to/akamittal/react-native-animation-easy-step-by-step-guide-jfc</guid>
      <description>&lt;p&gt;React Native Animation is an interesting topic where a dull application could be converted into an interactive and beautiful app. Whether you are creating a &lt;a href="https://www.akashmittal.com/login-page-react-native-animation/" rel="noopener noreferrer"&gt;simple login screen&lt;/a&gt; or a complex component transition, animation acts as a driver of user interface. Working with animations could look a bit overwhelming at first but it is essentially a process of just 3 steps.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a reference variable of &lt;code&gt;Animated.Value&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Change the value of this reference variable using &lt;code&gt;Animated.Spring&lt;/code&gt; or &lt;code&gt;Animated.Timing&lt;/code&gt; function.&lt;/li&gt;
&lt;li&gt;Create interpolation of this reference variable in styles of component to animate.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this guide you will learn about the basic principles of React Native animation. I have divided it into two sections. If you are a complete beginner, you can get ready to play with animations quickly by having minimal but most important information by going through section 1. But if you want to learn more then feel free to proceed to section 2. After completing the first section, you will be able to work with animations. Happy animations &amp;amp; let’s get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  Before you begin
&lt;/h2&gt;

&lt;p&gt;This guide won’t teach you the concepts of React Native, else it will teach you react native animations. So, a prior knowledge of React Native is required. This article is written for version 0.63.&lt;/p&gt;

&lt;h2&gt;
  
  
  Section 1 – Essentials of React Native animation &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;In this section, I included the three essential steps to work with animations. After completing this, you will be able to run animations in your app. I have also included a working example at the end.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a reference variable of Animated.Value &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The first step involves the creation of animation variable. This variable defines the styling on the component throughout the whole animation lifecycle.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const animationVariable = useRef(new Animated.Value(0)).current;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I am setting the variable, &lt;code&gt;animationVariable&lt;/code&gt; with initial value of 0. This is it. Step completed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Change the value of &lt;code&gt;animationVariable&lt;/code&gt; using Animated.Spring or Animated.Timing function &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Since our variable is already defined, we now need a way to change the value of it. &lt;strong&gt;Why the &lt;code&gt;animationVariable&lt;/code&gt; value needs to be changed?&lt;/strong&gt; Because the change in this value derives the changes in the style properties of components which ultimately animates them. For example, suppose an image needs to be scaled twice its size. We can do that by changing the value of animationVariable and interpolate the changes with the amount of scaling we require. Not understood? Worry not, we will see that in third step.&lt;/p&gt;

&lt;p&gt;To change the value of animation variable, we use &lt;strong&gt;Spring&lt;/strong&gt; and &lt;strong&gt;Timing&lt;/strong&gt; functions of Animated component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Animated.spring(animationVariable, {
     toValue: 1,
     useNativeDriver: true,
}).start();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  Code Explanation –
&lt;/h6&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Code Terms&lt;/th&gt;
&lt;th&gt;Explanation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://reactnative.dev/docs/animated#spring" rel="noopener noreferrer"&gt;Animated.spring&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;This is the prebuilt function by React-Native to change the value of variable in a manner so that it gives the feel of spring while changing style values. Check other animation function here.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;toValue&lt;/td&gt;
&lt;td&gt;It indicates the new value for our animationVariable. Remember, we set it to 0 in first step? Now the spring function will change it to 1.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://reactnative.dev/docs/animations#using-the-native-driver" rel="noopener noreferrer"&gt;useNativeDriver&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;In React Native, our app runs on native thread and JS thread. Using this option will run our animation on native thread which will improve the performance. The only drawback is that few styles are supporting native drivers, as of now. Check the list of all supporting styles
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;start&lt;/td&gt;
&lt;td&gt;Calling start will run the animation. Means, animationVariable value will change from 0 to 1 in spring form within the time period defined by React-Native in Animated.spring method.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Create interpolation of &lt;code&gt;animationVariable&lt;/code&gt; in styles of component to animate &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Now we will use this &lt;code&gt;animationVariable&lt;/code&gt; to change the style of component according to how we wish to animate it. Like in our earlier example, suppose we want to scale an &lt;code&gt;Image&lt;/code&gt; or a &lt;code&gt;View&lt;/code&gt;, we will do –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Animated.View
    style = {{
                 transform: [
                               {
                                  scale : animationVariable.interpolate({
                                             inputRange: [0, 1],
                                             outputRange: [1, 2],
                                          }),
                               },
                            ],
            }}
&amp;gt;
&amp;lt;/Animated.View&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  Code Explanation
&lt;/h6&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Code Terms&lt;/th&gt;
&lt;th&gt;Explanation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Animated.View&lt;/td&gt;
&lt;td&gt;No, you can’t run animation on simple components. You need special components provided by Animated component. So, we are using Animated.View. You can read more about it here.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://reactnative.dev/docs/animated#interpolation" rel="noopener noreferrer"&gt;interpolate&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Interpolate converts the &lt;code&gt;animationVariable&lt;/code&gt; value into the required scale value.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;inputRange&lt;/td&gt;
&lt;td&gt;It defines the range of value for &lt;code&gt;animationVariable&lt;/code&gt;. Here it is changing from 0 to 1.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;outputRange&lt;/td&gt;
&lt;td&gt;It is defining the value of scale for the corresponding &lt;code&gt;animationVariable&lt;/code&gt; value. So, when &lt;code&gt;animationVariable&lt;/code&gt; is 0, scale will be 1. When it is 1, scale will be 2.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now you know how React Native animation works. Here is an infographic to clear your understandings further.&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%2Fi1.wp.com%2Fwww.akashmittal.com%2Fwp-content%2Fuploads%2F2020%2F10%2FReact-Native-Animation-How-Animation-Works.png%3Fresize%3D668%252C445%26ssl%3D1" 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%2Fi1.wp.com%2Fwww.akashmittal.com%2Fwp-content%2Fuploads%2F2020%2F10%2FReact-Native-Animation-How-Animation-Works.png%3Fresize%3D668%252C445%26ssl%3D1" title="Step by step description of how animation works" alt="How react native animation works by changing the animationValue and interpolating it to the meaningful style value."&gt;&lt;/a&gt;Infographic of how animation works in react native&lt;/p&gt;

&lt;h3&gt;
  
  
  Working Example &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, {useRef} from 'react';
import {Animated, Button, View} from 'react-native';
const App = () =&amp;gt; {
   const animationValue = useRef(new Animated.Value(0)).current;

   const runAnimationOnClick = () =&amp;gt; {
      Animated.spring(animationValue, {
          toValue: 1,
          useNativeDriver: true,
      }).start();
   }

   return (
     &amp;lt;View&amp;gt;
       &amp;lt;Animated.View
          style = {{
              height: 200,
              width: 200,
              backgroundColor: 'red',
              transform: [
                           {
                             scale: animationValue.interpolate({
                                       inputRange: [0, 1],
                                       outputRange: [1, 2],
                                    }),
                           },
                         ],
          }}
       /&amp;gt;
       &amp;lt;Button title="Scale Up" onPress={runAnimationOnClick} /&amp;gt;
     &amp;lt;/View&amp;gt;
   );
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/great-wind-xi8ww"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  End of Section 1
&lt;/h3&gt;

&lt;p&gt;In this tutorial we learned the three basic steps of working with React Native animation. First we create the animation variable. Second, we change the value of variable using spring or timing function. Third, we interpolate the variable into useful style values.&lt;/p&gt;

&lt;h2&gt;
  
  
  Section 2 – If you want to learn more about React Native animation &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Note 1.&lt;/strong&gt; You can initialize any value for your animationVariable. We set 0 but doesn’t mean you have to do the same. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note 2.&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;There are in total 6 components in react native which could be animated – &lt;code&gt;Animated.View&lt;/code&gt;, &lt;code&gt;Animated.Image&lt;/code&gt;, &lt;code&gt;Animated.Text&lt;/code&gt;, &lt;code&gt;Animated.ScrollView&lt;/code&gt;, &lt;code&gt;Animated.FlatList&lt;/code&gt;, &lt;code&gt;Animated.SectionList&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note 3.&lt;/strong&gt; &lt;a&gt;&lt;/a&gt; There are few types of animation function like &lt;a href="https://reactnative.dev/docs/animated#timing" rel="noopener noreferrer"&gt;Animated.timing()&lt;/a&gt;, &lt;a href="https://reactnative.dev/docs/animated#spring" rel="noopener noreferrer"&gt;Animated.spring()&lt;/a&gt; and &lt;a href="https://reactnative.dev/docs/animated#decay" rel="noopener noreferrer"&gt;Animated.decay()&lt;/a&gt;. Code snippets for these animation function are –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Animated.timing(animationValue, {
  toValue: 1,
  easing: Easing.back(),
  duration: 2000
,
  useNativeDriver: true,
}).start(({finished}) =&amp;gt; {});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Attention&lt;/strong&gt; : React Native says that the default value of useNativeDriver is false, but actually you need to provide this property otherwise debugger will crash.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://reactnative.dev/docs/easing" rel="noopener noreferrer"&gt;Easing&lt;/a&gt;defines the type of animation you want like bouncing, easeIn, easeOut, elastic etc.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{finished}&lt;/code&gt; parameter in start function helps in determining whether the animation is completed successfully or not. Because the function within start acts as a callback. It gets called either when animation got completed or interrupted in the middle. So, in case when you want to run something only when animation is completed, there you can use &lt;code&gt;finished&lt;/code&gt; property within &lt;code&gt;if&lt;/code&gt; condition.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;duration&lt;/code&gt; defines how long will it take to change the value of &lt;code&gt;animationValue&lt;/code&gt; from current to &lt;code&gt;toValue&lt;/code&gt;. Like, in our case it will take 2000 ms (2 seconds) to change &lt;code&gt;animationValue&lt;/code&gt; from 0 to 1.&lt;/p&gt;

&lt;p&gt;Overall in summary, our animation will run for 2 seconds using &lt;code&gt;easing.back&lt;/code&gt; (it will run the animation in opposite direction a bit and then move forward. Think of it like Lion taking few steps back before jumping. So, suppose you use this animation for scaling a &lt;code&gt;View&lt;/code&gt; from 1 to 2 then the &lt;code&gt;View&lt;/code&gt; first scale down a bit and then go to 2).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note 4.&lt;/strong&gt; Sometimes you feel the need to combine animations together. For example, suppose you want to show 3 buttons in fadeIn style where 2nd button will start showing up only when first has reached half opacity. So, it will give the illusion of appearing buttons but with a delay. I will show this effect with code demo. There are 4 &lt;a href="https://reactnative.dev/docs/animated#composing-animations" rel="noopener noreferrer"&gt;composing animations&lt;/a&gt; provided by React Native – &lt;a href="https://reactnative.dev/docs/animated#delay" rel="noopener noreferrer"&gt;Animated.delay()&lt;/a&gt;, &lt;a href="https://reactnative.dev/docs/animated#parallel" rel="noopener noreferrer"&gt;Animated.parallel()&lt;/a&gt;, &lt;a href="https://reactnative.dev/docs/animated#sequence" rel="noopener noreferrer"&gt;Animated.sequence()&lt;/a&gt;, &lt;a href="https://reactnative.dev/docs/animated#stagger" rel="noopener noreferrer"&gt;Animated.stagger()&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note 5.&lt;/strong&gt; &lt;code&gt;Animated.sequence()&lt;/code&gt; is used to run different animations one after the other. So, suppose there are three boxes and you want to move them one after the other then we will use Animated.sequence(). See this code –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Animated.sequence([
    Animated.timing(box1AnimationVariable, {
        toValue: 1,
        duration: 1000,
        useNativeDriver: true,
    }),
    Animated.timing(box2AnimationVariable, {
        toValue: 1,
        duration: 1000,
        useNativeDriver: true,
    }),
]).start()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/kind-agnesi-gp2nf"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note 6.&lt;/strong&gt; &lt;code&gt;Animated.delay()&lt;/code&gt; is used with Animated.sequence and its purpose is to add a delay between two animations. For example, if you want to move the blue box after 1 second of completion of red box. Check this code snippet –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Animated.sequence([
    Animated.timing(box1AnimationVariable, {
        toValue: 1,
        duration: 1000,
        useNativeDriver: true,
    }),
       Animated.delay(1000),
    Animated.timing(box2AnimationVariable, {
        toValue: 1,
        duration: 1000,
        useNativeDriver: true,
    }),
]).start()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/broken-glitter-c6ey9"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note 7.&lt;/strong&gt; &lt;code&gt;Animated.parallel()&lt;/code&gt; is similar to &lt;code&gt;Animated.sequence()&lt;/code&gt; but here all the animations will run at the same time. Check out this code –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Animated.parallel([
    Animated.timing(box1AnimationVariable, {
        toValue: 1,
        duration: 1000,
        useNativeDriver: true,
    }),
    Animated.timing(box2AnimationVariable, {
        toValue: 1,
        duration: 1000,
        useNativeDriver: true,
    }),
]).start()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/black-butterfly-1s6u6"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note 8.&lt;/strong&gt; &lt;code&gt;Animated.stagger()&lt;/code&gt; is pretty interesting. It runs the animations in parallel but with a fixed delay. Like the second animation will start after the provided delay of starting of first animation. Check the code –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Animated.stagger(200, [
    Animated.timing(box1AnimationVariable, {
        toValue: 1,
        duration: 1000,
        useNativeDriver: true,
    }),
    Animated.timing(box2AnimationVariable, {
        toValue: 1,
        duration: 1000,
        useNativeDriver: true,
    }),
]).start()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/confident-roentgen-t6mt4"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;Code snippet for the button fading example is –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as React from 'react';
import { Animated, View, Button, Text, TouchableHighlight } from 'react-native';

export default function App() {
  const button1AnimationValue = React.useRef(new Animated.Value(0)).current;
  const button2AnimationValue = React.useRef(new Animated.Value(0)).current;
  const button3AnimationValue = React.useRef(new Animated.Value(0)).current;

  const buttonPressed = () =&amp;gt; {
    button1AnimationValue.setValue(0);
    button2AnimationValue.setValue(0);
    button3AnimationValue.setValue(0);
    Animated.stagger(100, [
      Animated.timing(button1AnimationValue, {
        toValue: 1,
        duration: 300,
        useNativeDriver: true,
      }),
      Animated.timing(button2AnimationValue, {
        toValue: 1,
        duration: 300,
        useNativeDriver: true,
      }),
      Animated.timing(button3AnimationValue, {
        toValue: 1,
        duration: 300,
        useNativeDriver: true,
      }),
    ]).start(({finished}) =&amp;gt; {})
  }

  return (
    &amp;lt;View style={{alignItems: 'center'}}&amp;gt;
      &amp;lt;Animated.View style={{
          marginTop: 5,
          marginBottom: 5,
          opacity: button1AnimationValue.interpolate({
            inputRange: [0, 1],
            outputRange: [0, 1],
          }),
        }}&amp;gt;
          &amp;lt;Button title={'Button 1'} color={'red'} /&amp;gt;
      &amp;lt;/Animated.View&amp;gt;
      &amp;lt;Animated.View style={{
          marginTop: 5,
          marginBottom: 5,
          opacity: button2AnimationValue.interpolate({
            inputRange: [0, 1],
            outputRange: [0, 1],
          }),
        }}&amp;gt;
          &amp;lt;Button title={'Button 2'} color={'cyan'} /&amp;gt;
      &amp;lt;/Animated.View&amp;gt;
      &amp;lt;Animated.View style={{
          marginTop: 5,
          marginBottom: 5,
          opacity: button3AnimationValue.interpolate({
            inputRange: [0, 1],
            outputRange: [0, 1],
          }),
        }}&amp;gt;
          &amp;lt;Button title={'Button 2'} color={'green'} /&amp;gt;
      &amp;lt;/Animated.View&amp;gt;
      &amp;lt;Button title={'Run Animation'} onPress={buttonPressed} /&amp;gt;
      &amp;lt;Text&amp;gt;Total fadein animation completes in 300ms but there is staggering delay of 100ms. So, second button will start fading in after 100ms of first button.&amp;lt;/Text&amp;gt;
    &amp;lt;/View&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/fervent-yalow-n36jg"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note 9.&lt;/strong&gt; &lt;a&gt;&lt;/a&gt; You can set &lt;code&gt;useNativeDriver: true&lt;/code&gt; only in case of few styles like &lt;em&gt;translate&lt;/em&gt;, &lt;em&gt;scale&lt;/em&gt;, &lt;em&gt;rotate&lt;/em&gt;, &lt;em&gt;opacity&lt;/em&gt; but you can’t use native drivers for changing width and height of the components. In that case you will have to set it to false.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note 10.&lt;/strong&gt; &lt;code&gt;interpolate&lt;/code&gt; can be used for some non-numeric output ranges. For example, you can use it over colors and angles range. Check the code below –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;backgroundColor: animationValue.interpolate({
            inputRange: [0, 1],
            outputRange: ['rgb(255,0,0)', 'rgb(0,0,255)'],
        }),
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This interpolation will change the background color from red to blue by passing through different color ranges in between.&lt;/p&gt;

&lt;p&gt;Another example of interpolation of angles might look like this –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rotateX: animationValue.interpolate({
            inputRange: [0, 1],
            outputRange: ['0deg', '90deg'],
        }),
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This interpolation will rotate the component in x direction from 0deg to 90deg.&lt;/p&gt;

&lt;p&gt;That’s it, we reached the end. In this article we learned about how react native animation works and what are the different functions we need to remember. If you want to gain more in depth knowledge about each topic, you can refer to the official documentation of React Native from here – &lt;a href="https://reactnative.dev/docs/animations" rel="noopener noreferrer"&gt;https://reactnative.dev/docs/animations&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://www.akashmittal.com/react-native-animation-easy-step-by-step-guide/" rel="noopener noreferrer"&gt;React Native Animation – Easy, Step By Step Guide&lt;/a&gt; appeared first on &lt;a href="https://www.akashmittal.com" rel="noopener noreferrer"&gt;Learn ReactJS &amp;amp; React Native With Me&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>react</category>
      <category>javascript</category>
      <category>animation</category>
    </item>
  </channel>
</rss>
