<?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: Scott Price</title>
    <description>The latest articles on DEV Community by Scott Price (@sayes2x).</description>
    <link>https://dev.to/sayes2x</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%2F92453%2F1ad8e6aa-009c-4400-8825-86707c179168.jpeg</url>
      <title>DEV Community: Scott Price</title>
      <link>https://dev.to/sayes2x</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sayes2x"/>
    <language>en</language>
    <item>
      <title>Looking for feedback - A React component that will function as a "heartbeat", or timer, for an application.</title>
      <dc:creator>Scott Price</dc:creator>
      <pubDate>Mon, 13 Aug 2018 14:52:58 +0000</pubDate>
      <link>https://dev.to/sayes2x/looking-for-feedback---a-react-component-that-will-function-as-a-heartbeat-or-timer-for-an-application-212b</link>
      <guid>https://dev.to/sayes2x/looking-for-feedback---a-react-component-that-will-function-as-a-heartbeat-or-timer-for-an-application-212b</guid>
      <description>

&lt;p&gt;I have never seen React used like this before so I am curious what other developers will think about it.&lt;/p&gt;

&lt;p&gt;Here is the gist: &lt;a href="https://gist.github.com/sayes2x/72d884c415662701e38c08b0d696c768"&gt;https://gist.github.com/sayes2x/72d884c415662701e38c08b0d696c768&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This component is a React implementation of 'accurate interval' developed by @Squeegy which can be found here: &lt;a href="https://gist.github.com/Squeegy/1d99b3cd81d610ac7351"&gt;https://gist.github.com/Squeegy/1d99b3cd81d610ac7351&lt;/a&gt;. Here is how Squeegy's function works: after each setTimeout call, a function compares when setTimeout was called with when setTimeout should have been called. The next call to setTimeout is adjusted to keep it as close as possible to the desired interval.&lt;/p&gt;

&lt;p&gt;This component accepts two props:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;heartbeatInterval: provides an interval in milliseconds, The Heartbeat component will call a function repeatedly at this timer interval as long as it is rendered. The default value for this interval is 1000 milliseconds.&lt;/li&gt;
&lt;li&gt;heartbeatFunction: a function that will be called every time the heartbeatInterval expires.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This component does not render anything, all the render function returns is a Fragment. The only thing this component does is call a function in a parent component at the set interval.&lt;/p&gt;

&lt;p&gt;Here is how I use it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In the parent component I have a state that is a boolean, in this case the state is called paused:&lt;/li&gt;
&lt;/ol&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;paused&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;In the render function of the parent component, before the return, I have a ternary operator that sets a variable to null if paused is true and sets the variable to the Heartbeat component if paused is false. In this case I used the default value of 1000 milliseconds for the heartbeatInterval prop:&lt;/li&gt;
&lt;/ol&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="nx"&gt;timer&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;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;paused&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; 
  &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; 
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Heartbeat&lt;/span&gt; &lt;span class="nx"&gt;heartbeatFunction&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&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;countdown&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;In the return part of the render function of the parent component I render the variable defined in step 2, along with whatever else I want this component to render:&lt;/li&gt;
&lt;/ol&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Fragment&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;timer&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;OtherComponents&lt;/span&gt; &lt;span class="nx"&gt;or&lt;/span&gt; &lt;span class="nx"&gt;HTML&lt;/span&gt; &lt;span class="nx"&gt;tags&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Fragment&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;I like this implementation because all I have to do is change a boolean in the state of my component to turn the timer on or off. Since I have not seen a Componant implemented in this way before, I am looking for feedback from other developers.&lt;/p&gt;

&lt;p&gt;To see this componenant used in a project go to &lt;a href="https://github.com/sayes2x/pomodoro-clock/tree/master/src/components"&gt;https://github.com/sayes2x/pomodoro-clock/tree/master/src/components&lt;/a&gt;&lt;br&gt;
This component is in the heartBeat.js file and it is used in the timer.js file&lt;/p&gt;


</description>
      <category>timerheartbeat</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
