<?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: Nat Ravenhill</title>
    <description>The latest articles on DEV Community by Nat Ravenhill (@natravenhill).</description>
    <link>https://dev.to/natravenhill</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%2F320305%2Fcd1546d0-4db3-4f06-89f1-c81ab627b5dd.jpeg</url>
      <title>DEV Community: Nat Ravenhill</title>
      <link>https://dev.to/natravenhill</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/natravenhill"/>
    <language>en</language>
    <item>
      <title>Refactoring the Pomodoro Timer: Components</title>
      <dc:creator>Nat Ravenhill</dc:creator>
      <pubDate>Thu, 03 Jun 2021 10:52:04 +0000</pubDate>
      <link>https://dev.to/natravenhill/refactoring-the-pomodoro-timer-components-1ag9</link>
      <guid>https://dev.to/natravenhill/refactoring-the-pomodoro-timer-components-1ag9</guid>
      <description>&lt;p&gt;Previously I wrote a Pomodoro timer app. The code is contained in &lt;a href="https://github.com/NatRavenhill/PomodoroTimer/blob/original/src/Session.jsx"&gt;a single React component.&lt;/a&gt; This is generally bad practice - we want components to each be responsible for a single bit of functionality and be reusable, so we will we split out functionality into smaller components.&lt;/p&gt;

&lt;p&gt;First I made a folder called "components", which will house the sub components and their styles.&lt;/p&gt;

&lt;p&gt;There are 3 elements we can immediately split out:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The main timer and button ("Tomato" element)&lt;/li&gt;
&lt;li&gt;The Break length controls&lt;/li&gt;
&lt;li&gt;The Session length controls&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffj1sv14gz0onp5dmvko2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffj1sv14gz0onp5dmvko2.png" alt="image" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Break Length
&lt;/h2&gt;

&lt;p&gt;This section has a &lt;code&gt;breakLength&lt;/code&gt; value contained in the state and two button handler methods to increase/decrease this value.&lt;/p&gt;

&lt;p&gt;First, we create a new component called &lt;code&gt;BreakLength&lt;/code&gt; and paste the JSX into the render method. As we are now in a child component, and React has unidirectional data flow, we cannot update the &lt;code&gt;breakLength&lt;/code&gt; value in the parent component from the &lt;code&gt;BreakLength&lt;/code&gt; component. &lt;/p&gt;

&lt;p&gt;Therefore, we pass a reference to the method into the &lt;code&gt;BreakLength&lt;/code&gt; component as a property and call this in a new &lt;code&gt;onClick&lt;/code&gt; handler. This will call the parent method and update its state.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;breakLength&lt;/code&gt; value is passed to the new component as a child property. When the state is changed in the parent, this will change the property value and re-render the &lt;code&gt;BreakLength&lt;/code&gt; component.&lt;/p&gt;


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


&lt;h2&gt;
  
  
  Session Length
&lt;/h2&gt;

&lt;p&gt;This is refactored in the same way:&lt;/p&gt;


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


&lt;h2&gt;
  
  
  Tomato
&lt;/h2&gt;

&lt;p&gt;The Tomato has 2 functions - &lt;code&gt;handleStartStop&lt;/code&gt; and &lt;code&gt;handleReset&lt;/code&gt;. Again, we pass references to these as properties and call them in the &lt;code&gt;onClick&lt;/code&gt; handlers in the child Tomato component. The minutes and second properties are passed as props to the Tomato component.&lt;/p&gt;


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


&lt;p&gt;Now our main component is much smaller and we have a better separation of concerns. We can also extract some of the styles in &lt;code&gt;Session.css&lt;/code&gt; that only apply to the Tomato into a new CSS file and include it just in this component.&lt;/p&gt;

</description>
      <category>react</category>
      <category>refactorit</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Project: A Pomodoro Timer In React</title>
      <dc:creator>Nat Ravenhill</dc:creator>
      <pubDate>Fri, 28 May 2021 13:38:10 +0000</pubDate>
      <link>https://dev.to/natravenhill/project-a-pomodoro-timer-in-react-46bi</link>
      <guid>https://dev.to/natravenhill/project-a-pomodoro-timer-in-react-46bi</guid>
      <description>&lt;h1&gt;
  
  
  Intro
&lt;/h1&gt;

&lt;p&gt;I recently created a Pomodoro timer in React, as part of the Free Code Camp front end course. The task involved creating a Pomodoro timer, which counts down from a predefined number of session minutes, then switches to a break timer, which counts down a specified break length. The session and break lengths can be altered, and the timer can be stopped and reset:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flb79s82uice6oofsofzg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flb79s82uice6oofsofzg.png" alt="pomodor-screenshot" width="800" height="517"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  What I learned
&lt;/h1&gt;

&lt;p&gt;I picked up some new things while working on this project.&lt;/p&gt;

&lt;h2&gt;
  
  
  padStart
&lt;/h2&gt;

&lt;p&gt;I found this string method useful when I was counting down to single seconds, but wanted them to display with a leading zero.&lt;/p&gt;

&lt;p&gt;It takes a number and a string as parameters, then adds copies of that string to the start until the entire string length is equal to the first parameter.&lt;/p&gt;

&lt;p&gt;Therefore my usage was &lt;code&gt;second.padStirng(2, '0')&lt;/code&gt;, which adds zeroes to the seconds output to ensure it is always a 2 digit number.&lt;/p&gt;

&lt;p&gt;See more &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  setState callbacks
&lt;/h2&gt;

&lt;p&gt;I had a situation where I wanted to start the timer after setting &lt;code&gt;isRunning&lt;/code&gt; to true. I initially started the timer after setting the state. This did not work because the &lt;code&gt;setState&lt;/code&gt; changes are asynchronous, so the tick method was acting as though &lt;code&gt;isRunning&lt;/code&gt; was false.&lt;/p&gt;

&lt;p&gt;To combat this, the &lt;code&gt;setState&lt;/code&gt; method has a callback parameter. Here we can add behaviour that will happen immediately after &lt;code&gt;isRunning&lt;/code&gt; is set to true. Calling the tick method from the callback made the timer start ticking as expected.&lt;/p&gt;

&lt;p&gt;See more &lt;a href="https://reactgo.com/react-setstate-callback/"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  setTimeout
&lt;/h2&gt;

&lt;p&gt;I wanted a way to get the timer to update every second. I was able to do this using the &lt;code&gt;setTimeout&lt;/code&gt; Javascript method. This method takes a method as a parameter and a time in milliseconds. It then calls the method after the specified time. I could put this in a loop to do it the number of times defined in the session length. &lt;/p&gt;

&lt;p&gt;There is also a &lt;code&gt;setInterval&lt;/code&gt; method which works similarly, but happens an infinite number of times or until &lt;code&gt;clearInterval&lt;/code&gt; is called.&lt;/p&gt;

&lt;p&gt;See more &lt;a href="https://www.w3schools.com/jsref/met_win_settimeout.asp"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Audio element (HMTL5)
&lt;/h2&gt;

&lt;p&gt;I needed to play a beep sound when the time runs out.  A new HTML element was added in HTML5 that does what I need. The &lt;code&gt;&amp;lt;audio&amp;gt;&lt;/code&gt; tag allows the user to specify a source, then there are DOM methods, that allow playing, stopping and restarting the source audio .&lt;/p&gt;

&lt;p&gt;See more &lt;a href="https://www.w3schools.com/html/html5_audio.asp#:~:text=HTML5%20Audio.%20Audio%20on%20the%20Web.%20Before%20HTML5%2C,browser%20version%20that%20fully%20supports%20the%20%3Caudio%3E%20element"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  React refs
&lt;/h2&gt;

&lt;p&gt;I couldn't directly access the audio element in my React component to use its methods. To get around this we can use refs. We create a ref variable in the component:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;this.beepRef = React.createRef();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and attach this to the audio element:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;audio ... ref={this.beepRef} .../&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we can call the audio methods on this reference within the component's lifecycle:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;this.beepRef.current.play();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;See more &lt;a href="https://reactjs.org/docs/refs-and-the-dom.html"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Importing a script into a React component
&lt;/h2&gt;

&lt;p&gt;I wanted to test my solution using a script file that FreeCodeCamp had provided. I did not know how to directly add a script file in. I initially tried adding a script tag to the render method, but this did not run the script. I then discovered that I needed to add it into the document during the componentWIllMount stage of the life cycle:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;componentWillMount() {
    const scriptTag = document.createElement("script");
    scriptTag.src =
      "https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js";
    scriptTag.async = true;
    document.body.appendChild(scriptTag);
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See more &lt;a href="https://reactgo.com/render-script-tag-component/"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;This was a great project to get some more practice with creating custom components and I also learnt quite a bit about how to implement timers.&lt;/p&gt;

&lt;p&gt;You can see the app running &lt;a href="https://natravenhill.com/PomodoroTimer/"&gt;here&lt;/a&gt;&lt;/p&gt;

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