<?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: Santiago Pereira</title>
    <description>The latest articles on DEV Community by Santiago Pereira (@santper).</description>
    <link>https://dev.to/santper</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%2F39135%2Fae826388-d871-4cf3-a1e1-0fa5522f7016.jpg</url>
      <title>DEV Community: Santiago Pereira</title>
      <link>https://dev.to/santper</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/santper"/>
    <language>en</language>
    <item>
      <title>How short is your life? Use React to find out. (Part 2)</title>
      <dc:creator>Santiago Pereira</dc:creator>
      <pubDate>Tue, 30 Jul 2019 21:52:14 +0000</pubDate>
      <link>https://dev.to/santper/how-short-is-your-life-use-react-to-find-out-part-2-2i14</link>
      <guid>https://dev.to/santper/how-short-is-your-life-use-react-to-find-out-part-2-2i14</guid>
      <description>&lt;p&gt;[&lt;strong&gt;DISCLAIMER, again&lt;/strong&gt;: the codePen embedding might make the pages look somewhat weird. to appreciate them in all their glory, open the CodePen links or the lifedots github repo on my dev.to profile]&lt;/p&gt;

&lt;p&gt;Last time we had arrived to the visualization part, but we were still only seeing blue and red dots. In this part we will add some green ones to represent the part of life that is spent in different activities (for example, working or studying).&lt;/p&gt;

&lt;p&gt;Let's start by adding the part of your life you will spend working. &lt;/p&gt;

&lt;p&gt;[&lt;strong&gt;DISCLAIMER 2&lt;/strong&gt;: if the app has no data about your age and country, no dots will be rendered because the app has no idea who you are. to fix it enter the CodePen links or replicate this on your computer cloning the GitHub repo]&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/sntgp/embed/MNmKwy?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;I could not find the retirement age for every country on Earth, and of course retirement is a personal decision and not everybody retires at the same age, even in the same country. So I went on and estimated that you will work for five decades before being able to retire. If you are an adult, we also have to deduct the years that you've worked already. &lt;/p&gt;

&lt;p&gt;How do we do this on React? We already had created a const &lt;strong&gt;style&lt;/strong&gt; that used a ternary operator to assign the &lt;em&gt;backgroundColor&lt;/em&gt; attribute to "blue" to the balls that represented past years, and "red" for those that represent the future. To add a new condition we use something called nested ternary operators. The let style, then, is going to look something like this:&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
let style = {backgroundColor: this.state.count &amp;lt; age ? 'blue': this.state.count &amp;lt; age + work ? 'green' : 'red'}

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

&lt;p&gt;Remember that &lt;em&gt;this.state.count&lt;/em&gt; is a counter than increases every time the for loop in componentWillMount() renders a new "year" (that is, three dots). If this.state.count is less than the user's age, the dots will be blue. If this.state.count is less than the user's age + the years the user will spend working (but not less than the user's age alone), the dots are green. If this.state.count is more than this sum, the dots are red.&lt;/p&gt;

&lt;p&gt;The next step is adding the years you will spend sleeping. The calculation here is a little bit easier: we suppose you sleep 8 hours every day of your life, which means 1/3 of every day. Obviously this isn't exactly true: you might sleep more during your youngest years and then do it less as you start working, for example. But since we don't have a time machine (and if we did, we probably wouldn't use it to track someone's sleep) we need to use an estimate like this.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/sntgp/embed/GVmqXG?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Again the logic is the same: we use ternary operators to handle three different conditions: assign &lt;em&gt;backgroundColor&lt;/em&gt; to "blue" if a year has already passed, green if it will be spent either working or sleeping, red if none of those are true.&lt;/p&gt;

&lt;p&gt;We do the same thing to add the years the user will spend studying, and using social media.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/sntgp/embed/zgwBbK?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Again to calculate this one we made a supposition: we're guessing you will go through elementary, middle school and high school, plus four years of college.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/sntgp/embed/ymbaBY?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;This one comes from an &lt;a href="https://www.socialmediatoday.com/marketing/how-much-time-do-people-spend-social-media-infographic"&gt;actual study you can read here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This final instance has two new lines we hadn't written yet: the ones we are using to store GreenTime (the time spent working, studying, sleeping and using social media) and RedTime (life expectancy minus GreenTime minus age) on localStorage. With these new data stored we can pass to the final phase of this project: the summary.&lt;/p&gt;

&lt;p&gt;Speaking strictly about React, this is nothing difficult: three components (BlueTime, RedTime and GreenTime) that each render a value from localStorage.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/sntgp/embed/XvRjdM?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;And this is the end for our React tutorial!&lt;/p&gt;

&lt;p&gt;You can find the whole project in &lt;a href="https://github.com/SantiagoPereira/lifedots"&gt;this github repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So, this was the second part to my first post in dev.to!&lt;/strong&gt; Did you find this tutorial easy to follow? Did you learn something about React you didn't know before? Please leave your feedback in the comments, and if you liked it follow me here and on Twitter 👇👇 &lt;/p&gt;


&lt;div class="ltag__user ltag__user__id__39135"&gt;
  
    .ltag__user__id__39135 .follow-action-button {
      background-color: #BAFF5C !important;
      color: #FFFFFF !important;
      border-color: #BAFF5C !important;
    }
  
    &lt;a href="/santper" class="ltag__user__link profile-image-link"&gt;
      &lt;div class="ltag__user__pic"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vmOV7zHY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--07GeFs_G--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/39135/ae826388-d871-4cf3-a1e1-0fa5522f7016.jpg" alt="santper image"&gt;
      &lt;/div&gt;
    &lt;/a&gt;
  &lt;div class="ltag__user__content"&gt;
    &lt;h2&gt;
&lt;a class="ltag__user__link" href="/santper"&gt;Santiago Pereira&lt;/a&gt;
&lt;/h2&gt;
    &lt;div class="ltag__user__summary"&gt;
      &lt;a class="ltag__user__link" href="/santper"&gt;16 year old wannabe programmer on the way to greatness (?&lt;/a&gt;
    &lt;/div&gt;
    &lt;p class="ltag__user__social"&gt;
        &lt;a href="https://twitter.com/santper_" rel="noopener"&gt;
          &lt;img class="icon-img" alt="twitter logo" src="https://res.cloudinary.com/practicaldev/image/fetch/s--oEHrSmvE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/twitter-logo.svg"&gt;santper_
        &lt;/a&gt;
        &lt;a href="https://github.com/SantiagoPereira" rel="noopener"&gt;
          &lt;img class="icon-img" alt="github logo" src="https://res.cloudinary.com/practicaldev/image/fetch/s--C74Jn3f1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/github-logo.svg"&gt;SantiagoPereira
        &lt;/a&gt;
    &lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;


</description>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How short is your life? Use React to find out. (Part 1)</title>
      <dc:creator>Santiago Pereira</dc:creator>
      <pubDate>Tue, 30 Jul 2019 17:01:13 +0000</pubDate>
      <link>https://dev.to/santper/how-short-is-your-life-use-react-to-find-out-part-1-2fff</link>
      <guid>https://dev.to/santper/how-short-is-your-life-use-react-to-find-out-part-1-2fff</guid>
      <description>&lt;p&gt;[&lt;strong&gt;DISCLAIMER:&lt;/strong&gt; some of the pages might look odd because of the CodePen embedding, open the link to see them in all its glory]&lt;/p&gt;

&lt;p&gt;Have you ever wondered how much time of your life some of your daily activities consume? In this article we're going to learn how to make a simple React app to visualize an estimate of it.&lt;/p&gt;

&lt;p&gt;Let's start with the basic &lt;em&gt;index.html&lt;/em&gt; file:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/sntgp/embed/oKZmLK?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;You'll see that there's an h1 asking what your name is and a button to submit it, but there is no input to actually write your name. That is where React kicks in!&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/sntgp/embed/ymMwer?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;We've created a simple &lt;em&gt;NameInput&lt;/em&gt; component that stores the input's value in &lt;a href="https://developer.mozilla.org/es/docs/Web/API/Storage/LocalStorage"&gt;Local Storage&lt;/a&gt; with the name of "nameValue". The name is only used in this case to personalize the experience a little bit, but if you're replicating this, you can choose to give it some other use.&lt;/p&gt;

&lt;p&gt;After the &lt;em&gt;index.html&lt;/em&gt; file, we create a &lt;em&gt;country.html&lt;/em&gt; file, for the user to submit the country where he/she lives. The logic for the React component is pretty much the same: the &lt;em&gt;CountryInput&lt;/em&gt; component stores the value in LocalStorage as "countryValue" 👇&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/sntgp/embed/xvqBvL?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;You'll see that we've now added two new components: &lt;em&gt;EnglishTitle&lt;/em&gt; and &lt;em&gt;SpanishTitle&lt;/em&gt;, which render the phrase "What is your country? + the name you submitted before (if you start from here and haven't submitted a name in the latter part, "nameValue" returns null)&lt;/p&gt;

&lt;h3&gt;
  
  
  Now comes the fun stuff
&lt;/h3&gt;

&lt;p&gt;Next we create an &lt;em&gt;age.html&lt;/em&gt; file where the user can submit his/her age.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/sntgp/embed/oKZRNR?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;We have the regular &lt;em&gt;Input&lt;/em&gt;, &lt;em&gt;EnglishTitle&lt;/em&gt; and &lt;em&gt;SpanishTitle&lt;/em&gt; components, but we've also added a function. The function makes an XML HTTP request to the file &lt;em&gt;lifeexpectancy.json&lt;/em&gt;, saved inside the &lt;em&gt;data&lt;/em&gt; folder. The &lt;em&gt;lifeexpectancy.json&lt;/em&gt; file stores the life expectancy for every nation on Earth. The XML request makes the data accesible to myFunction, which uses a for loop to look for the country value submitted as &lt;em&gt;countryValue&lt;/em&gt;. Finally, it stores the life expectancy value corresponding to the user's country as &lt;em&gt;lifeExpectancyValue&lt;/em&gt; using Local Storage.&lt;/p&gt;

&lt;p&gt;Once the age is submitted,  we go to the next page which is in this case just the instructions. There are no React components this time, just text (with the option to read in Spanish)&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/sntgp/embed/wVJVWV?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Starting the visualization
&lt;/h3&gt;

&lt;p&gt;At this point, had you wondered why the app was named LifeDots? Now you are going to &lt;a href="https://codepen.io/sntgp/pen/gVWYoL"&gt;find out why&lt;/a&gt;. From &lt;em&gt;instructions.html&lt;/em&gt; we pass to &lt;em&gt;firstinstance.html&lt;/em&gt;. The file presents an amount of colored dots equal to the life expectancy of the user's country three times (every dot represents four months). You will see, because it's only the first instance, that the balls are either red or blue, but not green. It's explained down in the page that the blue balls represent the time you've already lived (your age), and the red balls represent the time you still have left (according to your country's life expectancy). Like in the prior stages, if you haven't submitted anything, the component doesn't render.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/sntgp/embed/gVWYoL?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;How do we achieve this using React? First we create a unique component called . This component's state has three values: num, for the stored lifeExpectancyValue, count (a counter) and items, an empty array. Inside componentWillMount(), we'll find the let arr (another empty array) and a for loop that executes a number of times equal to the life expectancy. Every time it executes, this loop pushes a div of classname "year", composed of three dots, into the &lt;em&gt;items&lt;/em&gt; array in the component's state. Finally we find inside the return(), this very array. This way, the Life component will render an amount of "years" equal to the life expectancy value stored in LocalStorage.&lt;/p&gt;

&lt;p&gt;In Part 2 we'll start adding green dots to represent the time you will spend in different activities like studying, sleeping, or working.&lt;/p&gt;

&lt;p&gt;You can find the whole project in &lt;a href="https://github.com/SantiagoPereira/lifedots"&gt;this github repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So, this was my first post in dev.to!&lt;/strong&gt; Did you find this tutorial easy to follow? Did you learn something about React you didn't know before? Please leave your feedback in the comments, and if you liked it follow me here and on Twitter 👇👇 &lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__user ltag__user__id__39135"&gt;
  
    .ltag__user__id__39135 .follow-action-button {
      background-color: #BAFF5C !important;
      color: #FFFFFF !important;
      border-color: #BAFF5C !important;
    }
  
    &lt;a href="/santper" class="ltag__user__link profile-image-link"&gt;
      &lt;div class="ltag__user__pic"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vmOV7zHY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--07GeFs_G--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/39135/ae826388-d871-4cf3-a1e1-0fa5522f7016.jpg" alt="santper image"&gt;
      &lt;/div&gt;
    &lt;/a&gt;
  &lt;div class="ltag__user__content"&gt;
    &lt;h2&gt;
&lt;a class="ltag__user__link" href="/santper"&gt;Santiago Pereira&lt;/a&gt;
&lt;/h2&gt;
    &lt;div class="ltag__user__summary"&gt;
      &lt;a class="ltag__user__link" href="/santper"&gt;16 year old wannabe programmer on the way to greatness (?&lt;/a&gt;
    &lt;/div&gt;
    &lt;p class="ltag__user__social"&gt;
        &lt;a href="https://twitter.com/santper_" rel="noopener"&gt;
          &lt;img class="icon-img" alt="twitter logo" src="https://res.cloudinary.com/practicaldev/image/fetch/s--oEHrSmvE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/twitter-logo.svg"&gt;santper_
        &lt;/a&gt;
        &lt;a href="https://github.com/SantiagoPereira" rel="noopener"&gt;
          &lt;img class="icon-img" alt="github logo" src="https://res.cloudinary.com/practicaldev/image/fetch/s--C74Jn3f1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/github-logo.svg"&gt;SantiagoPereira
        &lt;/a&gt;
    &lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;


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