<?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: Rafael E. Poveda</title>
    <description>The latest articles on DEV Community by Rafael E. Poveda (@raerpo).</description>
    <link>https://dev.to/raerpo</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%2F125196%2F311829fa-aebd-4c81-b14f-ecf61e6cb4b1.jpeg</url>
      <title>DEV Community: Rafael E. Poveda</title>
      <link>https://dev.to/raerpo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/raerpo"/>
    <language>en</language>
    <item>
      <title>My mistakes with Dates on JavaScript</title>
      <dc:creator>Rafael E. Poveda</dc:creator>
      <pubDate>Fri, 21 Feb 2020 18:39:03 +0000</pubDate>
      <link>https://dev.to/raerpo/my-mistakes-with-dates-on-javascript-35el</link>
      <guid>https://dev.to/raerpo/my-mistakes-with-dates-on-javascript-35el</guid>
      <description>&lt;p&gt;Dates are hard! And I'm not talking about that human to human interaction (those are hard too but differently). I'm talking about describing the time in programming, especially in JavaScript. Not only the widely known quirks with JavaScript and dates make it hard, but if you add timezones and different types of representations that exist everything becomes even harder.&lt;/p&gt;

&lt;p&gt;I recently had a bug (that turned out to be two bugs) that made me realize that there were a lot of missing pieces in my understanding of how dates work on JavaScript so I made a journey to fill those gaps and learned a few tricks on the way that I want to share with you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a new date with a wrong string parameter
&lt;/h2&gt;

&lt;p&gt;You can create a new date using the &lt;code&gt;Date&lt;/code&gt; constructor with 4 types of parameters.&lt;/p&gt;

&lt;p&gt;1) without parameters.&lt;/p&gt;

&lt;p&gt;This is very common. You just use &lt;code&gt;new Date()&lt;/code&gt; and you will get a Date object in your local time. It's very useful for scenarios when you don't need to represent a Date in multiple timezones.&lt;/p&gt;

&lt;p&gt;2) With timestamp.&lt;/p&gt;

&lt;p&gt;A timestamp in JavaScript is the number of milliseconds that have been passed since January 1, 1970 (&lt;a href="https://en.wikipedia.org/wiki/Unix_time"&gt;Epoch time&lt;/a&gt;). One thing that is important here is that JavaScript handles Epoch time in milliseconds while other languages and even Unix-based operating systems do it in seconds.&lt;/p&gt;

&lt;p&gt;For example, in Python I get this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="mf"&gt;1582293096.868101&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But in JavaScript I will get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="mi"&gt;1582293096211&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So it's very important that if you receive a timestamp as date representation, you need to check if it's in milliseconds or seconds (it will be more likely the latter). If you are not aware of this you will get different dates.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Parsing Date with timestamp from Python
new Date(1582293096.868101)
Date Mon Jan 19 1970 04:31:33 GMT-0300 (Chile Summer Time)

// Parsing Date with timestamp from JS
new Date(1582293096211)
Date Fri Feb 21 2020 10:51:36 GMT-0300 (Chile Summer Time)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this case, a quick fix is to multiply the timestamp by 1000 before using it in a JavaScript app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Date with timestamp from Python adding milliseconds
new Date(1582293096.868101*1000)
Date Fri Feb 21 2020 10:51:36 GMT-0300 (Chile Summer Time)

// Date with timestamp from JS
new Date(1582293096211)
Date Fri Feb 21 2020 10:51:36 GMT-0300 (Chile Summer Time)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;3) With date arguments.&lt;/p&gt;

&lt;p&gt;In this option, we specify the year, month, day, hours, minutes, seconds and milliseconds as parameters in the Date constructor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2020&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;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Date Thu Feb 20 2020 13:20:00 GMT-0300 (Chile Summer Time)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this way of creating a date, you will also get a date in your system's timezone. This way is commonly ignored by developers because of the month being zero-based 🤷🏻‍♂️. One cool thing about this way is that all of the undefined values in the parameter list will default to 0.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2020&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="c1"&gt;// Date Sat Feb 01 2020 00:00:00 GMT-0300 (Chile Summer Time)&lt;/span&gt;

&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2020&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;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Date Mon Feb 10 2020 03:00:00 GMT-0300 (Chile Summer Time)&lt;/span&gt;

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



&lt;p&gt;4) With strings&lt;/p&gt;

&lt;p&gt;In this way, you passed a string to initialize your date. The most common way to represent a Date is an ISO string like this: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;"2020-02-21T14:19:35.926Z"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It has a standard structure, with year-month-day-hours-minutes-seconds and the "Z" representing UTC or GMT-0. So when you pass that string as parameter to the Date constructor, the date will be converted to your local system timezone.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2020-02-21T14:19:35.926Z&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Date Fri Feb 21 2020 11:19:35 GMT-0300 (Chile Summer Time)&lt;/span&gt;

&lt;span class="c1"&gt;// As you can see, it subtracts the 3 hours corresponding to my timezone.&lt;/span&gt;

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



&lt;p&gt;Soooo, after all this introduction I want to show you the cause of my second bug.&lt;/p&gt;

&lt;p&gt;I had a string date like this &lt;code&gt;'2020-02-21'&lt;/code&gt; and I just passed it like that to a Date constructor and I got this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2020-02-21&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Date Thu Feb 20 2020 21:00:00 GMT-0300 (Chile Summer Time)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🤦🏻‍♂️🤦🏻‍♂️🤦🏻‍♂️🤦🏻‍♂️🤦🏻‍♂️🤦🏻‍♂️🤦🏻‍♂️&lt;/p&gt;

&lt;p&gt;So instead of getting the date that I (naively) expected, I got the day before that. And it makes total sense since JavaScript will parse the date in UTC and then apply the timezone change to set it in my system timezone.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;My first lesson is to be consistent with how do you represent dates. If you get strings as input just make sure that they have the right format. And if not, make sure to modify them to have the right format.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this case, as I couldn't get a different string from backend, I could've done two things:&lt;/p&gt;

&lt;p&gt;1) Split the string and pass it as date arguments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;year&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;month&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;day&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2020-02-21&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;-&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;year&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;month&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;day&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It works, but the &lt;code&gt;Number(month) - 1&lt;/code&gt; part doesn't feel right 🤔.&lt;/p&gt;

&lt;p&gt;2) Adding an explicit time&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2020-02-21&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;T00:00&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This way is cleaner and it will work since I'm telling JavaScript that I don't want the time to be inferred so it doesn't need to subtract hours (or add them if I happen to be in the + side of the UTC).&lt;/p&gt;

&lt;h2&gt;
  
  
  Bug in an older version of &lt;code&gt;date-fns-tz&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;After reaching this point, you probably are thinking "pff I never have these kinds of problems because I use &lt;code&gt;moment/date-fns/another-date-library&lt;/code&gt; and let them take care of those things".&lt;/p&gt;

&lt;p&gt;This bug was hard to find. &lt;code&gt;date-fns-tz&lt;/code&gt; is a library that allows you to add timezone support to date-fns dates. I had a date defined in UTC and I need it to be in multiple timezones. For this purpose, I used a function named &lt;code&gt;utcToZonedTime&lt;/code&gt; and its use is pretty straightforward.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;utcToZonedTime&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;date-fns-tz&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;myDate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;utcToZonedTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2020-02-17T03:00:00Z&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;America/Santiago&lt;/span&gt;&lt;span class="dl"&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;em&gt;America/Santiago&lt;/em&gt; is a GMT-3 timezone, so, as you would expect, from the date that I passed the result would be &lt;code&gt;Mon Feb 17 2020 00:00:00&lt;/code&gt;. But surprisingly, the result was &lt;code&gt;Tue Feb 18 2020 00:00:00&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/aZ3LDBs1ExsE8/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/aZ3LDBs1ExsE8/giphy.gif" alt="WTF"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It was adding a full day and I did not understand why it was happening. After a LOT of debugging my own code, I noticed that I was using the version 1.0.8 of &lt;code&gt;date-fns-tz&lt;/code&gt; so as last resort after not finding anything in my code I tried to upgrade the library to the latest version (1.0.10) and finally I got the &lt;code&gt;Mon Feb 17 2020 00:00:00&lt;/code&gt; that I was expecting.&lt;/p&gt;

&lt;p&gt;Apparently it was a &lt;a href="https://github.com/marnusw/date-fns-tz/issues/41"&gt;bug on that particular version that matched my use case&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;So my first lesson that could be very obvious is &lt;strong&gt;always keep your dependencies updated&lt;/strong&gt;. When we add a dependency we are trusting that everything will work perfectly but every piece of software is susceptible to have bugs and it's something that we need to deal with, especially on open source tools.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  A new trick!
&lt;/h2&gt;

&lt;p&gt;In this application that I'm working on, I need to make sure to show the right date and time for certain events independently of where the user is. For example, if something is set to happen on February 21, 2020 at 10:00 am in Chilean time (GMT-3), a user in Mexico City or in Sao Paulo should see that at the same time and not transform into their timezones.&lt;/p&gt;

&lt;p&gt;In order to do this, I can open Chrome setting a timezone and even a language. In that way I'm able to test my app on different timezones and check that everything is correct.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;TZ&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'America/Sao_Paulo'&lt;/span&gt; open &lt;span class="nt"&gt;-na&lt;/span&gt; &lt;span class="s2"&gt;"Google Chrome"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will open a new Google Chrome window as if I were in Sao Paulo timezone wise.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://imgur.com/a/7maxfc5"&gt;https://imgur.com/a/7maxfc5&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Or if run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;TZ&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'America/Bogota'&lt;/span&gt; open &lt;span class="nt"&gt;-na&lt;/span&gt; &lt;span class="s2"&gt;"Google Chrome"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I will get:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://imgur.com/a/jHo1SSt"&gt;https://imgur.com/a/jHo1SSt&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this little trick, I'm able to test my applications as if I were in a different timezone and check if my dates behave in the way that I expect.&lt;/p&gt;

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

&lt;p&gt;Every mistake is an opportunity to learn something new and this issue was very useful for me to learn things that I ignored after years of working with Dates. I sincerely hope you find something useful here. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>dates</category>
    </item>
    <item>
      <title>Contributing to open source...</title>
      <dc:creator>Rafael E. Poveda</dc:creator>
      <pubDate>Sun, 02 Jun 2019 16:35:06 +0000</pubDate>
      <link>https://dev.to/raerpo/contributing-to-open-source-2eoi</link>
      <guid>https://dev.to/raerpo/contributing-to-open-source-2eoi</guid>
      <description>&lt;p&gt;Contributing to open source is one of the things that I love to do the most. It's a nice feeling being able to contribute to a project that is helping other developers and having the change to work with people from who you can learn is always appreciated.&lt;/p&gt;

&lt;p&gt;I opened a &lt;a href="https://github.com/nickpisacane/react-dynamic-swiper/issues/25"&gt;Github issue&lt;/a&gt; in a library that I used in my previous work and I would love to show you the process of how I fix it by contributing with code and maybe hopefully motive you to make your own contributions to this or other projects.&lt;/p&gt;

&lt;p&gt;The project is &lt;a href="https://github.com/nickpisacane/react-dynamic-swiper"&gt;react-dynamic-swiper&lt;/a&gt; which is a React wrapper around the awesome iDangerous swiper library.&lt;/p&gt;

&lt;h2&gt;
  
  
  Identifying an issue
&lt;/h2&gt;

&lt;p&gt;There are two ways in which you can find issues to start fixing.&lt;/p&gt;

&lt;p&gt;1) You found an issue in a library that you are using and notice that it hasn't been reported by anybody else.&lt;/p&gt;

&lt;p&gt;2) You can find open issues with labels like &lt;code&gt;help wanted&lt;/code&gt; or &lt;code&gt;low hanging fruit&lt;/code&gt; in projects even thou you haven't experienced the issue by yourself. This &lt;a href="http://github-help-wanted.com/"&gt;page&lt;/a&gt; can help you to find issues.&lt;/p&gt;

&lt;p&gt;In the case of &lt;code&gt;react-dynamic-swipper&lt;/code&gt; I notice that it had a missing feature so I open an issue asking the author if it was a mistake or if the feature wasn't include in the library's road map.&lt;/p&gt;

&lt;p&gt;It's important to try to find is the issue was already reported or if somebody else is working on the solution. It's super frustrating when you spent time working on a solution and finding out that the somebody else published one first or even worst that the issue never existed on the first place.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/nickpisacane/react-dynamic-swiper/issues/25#issue-429471387"&gt;https://github.com/nickpisacane/react-dynamic-swiper/issues/25#issue-429471387&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After a few days, the author confirmed that this was effectively a missing feature and if I want it I can make a pull request to solve the problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preparing the stage
&lt;/h2&gt;

&lt;p&gt;Now that the issue is assign to you, you can start working on it. The first step is making a fork of the project.&lt;/p&gt;

&lt;p&gt;To make a fork you should use the &lt;code&gt;Fork&lt;/code&gt; button that is located in the project's github page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XC_vKgQ1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ir7tqfkuug9eyv7a2lct.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XC_vKgQ1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ir7tqfkuug9eyv7a2lct.png" alt="Github fork button"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you click on the fork button, Github will take you to the same project but under your username. And now you can &lt;a href="https://help.github.com/en/articles/cloning-a-repository"&gt;clone it into your machine&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Go to your recently created project, create a new branch and now you are ready to make the changes in the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Do the actual change
&lt;/h2&gt;

&lt;p&gt;This part is the most difficult to explain because is different in each case. The change could be a typo in the documentation (this kind of changes are very important for project maintainers), could be adding a new feature o even a complete rewrite.&lt;/p&gt;

&lt;p&gt;I can't help you too much on this step but I can give you some advices that are transversal to every type of contribution.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Try at your best of making the change following the project's contribution guidelines. If the project doesn't have one you can contribute by adding it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When you have a proposal even if it is not finished open a PR indicating that you need a maintainer's opinion.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Be open to criticism and please don't take other people comments in a personal way. If they ask for modifications take them in consideration. Changes are, they know the project better than you so take that as an opportunity to learn about the project.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the &lt;a href="https://github.com/nickpisacane/react-dynamic-swiper/pull/27"&gt;change that I proposed for this library&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make the Pull request a wait for it to be included
&lt;/h2&gt;

&lt;p&gt;This part could be fast or could take a long time depending on the project. Sometimes your PR cannot be include it at all because it's not something the maintainer agrees with (this sucks, i know :().&lt;/p&gt;

&lt;p&gt;Please be patient and avoid making comments to rush the maintainer to merge your change.&lt;/p&gt;

&lt;h2&gt;
  
  
  Profit
&lt;/h2&gt;

&lt;p&gt;Once your contribution is include it you are now part of the project and can be proud that you help to build something that other people use.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's next?
&lt;/h2&gt;

&lt;p&gt;Contributing to open source is an excellent way to improve your skills and helping people at the same time. A lot of people are able to get awesome jobs by dedicating time improving open source libraries.&lt;/p&gt;

&lt;p&gt;Also, once you start doing it you always feel the need to do it more often so it is a win-win situation.&lt;/p&gt;

&lt;p&gt;If you want to go deeper on how to contribute to open source projects there are awesome resources to start with:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://git-scm.com/book/en/v2/GitHub-Contributing-to-a-Project"&gt;https://git-scm.com/book/en/v2/GitHub-Contributing-to-a-Project&lt;/a&gt;&lt;br&gt;
&lt;a href="https://egghead.io/courses/how-to-contribute-to-an-open-source-project-on-github"&gt;https://egghead.io/courses/how-to-contribute-to-an-open-source-project-on-github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>opensource</category>
    </item>
    <item>
      <title>defaultProps or default parameters</title>
      <dc:creator>Rafael E. Poveda</dc:creator>
      <pubDate>Wed, 03 Apr 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/raerpo/defaultprops-or-default-parameters-42pp</link>
      <guid>https://dev.to/raerpo/defaultprops-or-default-parameters-42pp</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;TLDR; When you use default parameters to set default props in a React component you don't get proptypes validation and also you need to define those default parameters in every class method in which you want to use those props.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Recently, in a code review that I was doing, I saw a code like this in a React component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;render&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="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&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;count&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;My first thought was that it was wrong because you should define default props adding a property called &lt;code&gt;defaultProps&lt;/code&gt; or using an static method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Setting a defaultProps property&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;render&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="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&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;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;defaultProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;count&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="c1"&gt;// Adding an static method to the component&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nx"&gt;defaultProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;count&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;render&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="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&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;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&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;But after trying the code, and to my surprise, it worked! So I was wondering if this is a valid practice inside a React component since I haven't seen it in any place. Even thought this works there are a few things that won't work but they aren't so obvious right away.&lt;/p&gt;

&lt;h2&gt;
  
  
  PropTypes validation
&lt;/h2&gt;

&lt;p&gt;Acording to React docs:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The propTypes typechecking happens after defaultProps are resolved, so typechecking will also apply to the defaultProps.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This means, that when you define proptypes, its validations are made in the props AND the default props that you set either with &lt;code&gt;static defaultProps&lt;/code&gt; or &lt;code&gt;defaultProps&lt;/code&gt; method but not when you use default paramaters.&lt;/p&gt;

&lt;p&gt;So, for example if we do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;render&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&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;count&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;propTypes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PropTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;defaultProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We're going to get this warning:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;index.js:1446 Warning: Failed prop type: Invalid prop `count` of type `string` supplied to `App`, expected `number`.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But if we use default parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;render&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&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;count&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;propTypes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PropTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We won't get any errors or warnings because React doesn't have any way to make a runtime analysis.&lt;/p&gt;

&lt;h2&gt;
  
  
  Possible inconsistency in props values between class methods
&lt;/h2&gt;

&lt;p&gt;When we define &lt;code&gt;defaultProps&lt;/code&gt; the values define inside of it will be available on every method inside a React component but if we use defalt parameters we must define the default value on every method. Let me explain this with an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;componentDidMount&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;count in CDM: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;render&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;count in Render: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&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;count&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;propTypes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PropTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;defaultProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we run this code, we are going to get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;count in Render:  10
count in CDM:  10
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see, on every method the default value will be the same so we can be sure that if the prop is not passed the default value will be the same in all places.&lt;/p&gt;

&lt;p&gt;In contrast, if you use default parameters the prop could be different on every class method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;componentDidMount&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;count in CDM: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;render&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;count in Render: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&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;count&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;propTypes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PropTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this example we will get this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;count in Render:  10
count in CDM:  undefined
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It's very bad to have a different value for the same prop in a different part of the component.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;This kind of things reminds me that almost always &lt;strong&gt;it's possible to write wrong code that just works&lt;/strong&gt; so we need to understand the impact of the decision that we made when we write applications. This can be seen as a problem with a small component when you can keep an eye on everything that is happening, but once the application gets bigger it gets very hard to track down this kind of bugs.&lt;/p&gt;

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