<?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: Holy-Elie Scaïde</title>
    <description>The latest articles on DEV Community by Holy-Elie Scaïde (@skydevht).</description>
    <link>https://dev.to/skydevht</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%2F101464%2F1cd5a499-c35d-476d-b270-24483eea591f.jpg</url>
      <title>DEV Community: Holy-Elie Scaïde</title>
      <link>https://dev.to/skydevht</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/skydevht"/>
    <language>en</language>
    <item>
      <title>The usage of React Hooks</title>
      <dc:creator>Holy-Elie Scaïde</dc:creator>
      <pubDate>Mon, 07 Feb 2022 20:25:01 +0000</pubDate>
      <link>https://dev.to/skydevht/the-usage-of-react-hooks-1pki</link>
      <guid>https://dev.to/skydevht/the-usage-of-react-hooks-1pki</guid>
      <description>&lt;p&gt;Functional components were a part of React before the introduction of Hooks. But their usage was restricted to creating pure components, as they did not possess the more complex lifecycle and state management of a class component. Hooks add these to functional components and allow us an easier way to reuse functionalities.&lt;/p&gt;

&lt;p&gt;What are hooks? Hooks are functions. And like all functions, you provide them with arguments, and they return values. To understand the usage of hooks is to understand where your arguments and the returned values fit into your component usage.&lt;/p&gt;

&lt;p&gt;Let's start with the basic: the function component. It accepts props as parameters and return a component tree. You can take the following as an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;Message&lt;/span&gt; &lt;span class="o"&gt;=&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="o"&gt;=&amp;gt;&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;p&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;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;text&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;/p&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;A bare component should be pure. If the &lt;code&gt;props&lt;/code&gt; object does not change — by altering the &lt;code&gt;text&lt;/code&gt; prop —, neither should the returned component tree.  The lifecycle is the stages a component goes through from creation to deletion. For a functional one, the lifecycle is another execution of the component’s code. Mutating the props can trigger this new execution.&lt;/p&gt;

&lt;p&gt;What if you want to change the color of the text when the user clicks on it? You could add a new prop called &lt;code&gt;color&lt;/code&gt;, but the parent component will then be responsible for updating this new prop. The consequence is a tight coupling — one depends on the other — between the two components. The parent will have the code to declare and update the prop, while our component is the one using it and is responsible for triggering the change. To resolve the situation, we shall use the &lt;code&gt;state&lt;/code&gt; concept.&lt;/p&gt;

&lt;p&gt;To explain state, we can take water as an analogy. Water can have multiple aspects: liquid, vapor, and ice. Which all depends on the same measure inherent to the water — temperature. In other words, the temperature of the water determines the current state of the water. And if we know the current value of the temperature, it’s easy to know its aspect. Like temperature, our above component can have a variable called &lt;code&gt;color&lt;/code&gt; which will always be the current color of the text. But, this has its limitations.&lt;/p&gt;

&lt;p&gt;If we create the variable inside the function of the component, it will be deleted when the function returns. And props are currently the only way we're able to update the component. This is where &lt;strong&gt;useState&lt;/strong&gt; comes in. ** useState** will provide you with a value that will not be destroyed when the function ends and, when changed, will trigger an update for the component — the function of the component will be executed again. &lt;strong&gt;useState&lt;/strong&gt; returns an array: The first element is our value, the second is the function to update the value. Assigning a new value directly will not work. This is our updated component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;Message&lt;/span&gt; &lt;span class="o"&gt;=&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="o"&gt;=&amp;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;color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setColor&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&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;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&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;setColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&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="s1"&gt;red&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="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&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;p&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;}&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;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;text&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;/p&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;Note that I set an initial value for the color. A rough overview on what happens under the hood:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For the initial execution of our code, we called &lt;strong&gt;useState&lt;/strong&gt; with the initial value we want. It returns this value, and we store it in &lt;code&gt;color&lt;/code&gt;. The second element is the update function, and we store it in &lt;code&gt;setColor&lt;/code&gt;. React guarantees that the function will never change.&lt;/li&gt;
&lt;li&gt;On the subsequent runs of the code, useState returns the same value. If the update function was called, the modified value will now be the one returned. The initial value will no longer be used.&lt;/li&gt;
&lt;li&gt;If the component is unmounted — removed from the webpage —, we go back to step one on the next mounting.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, every time we click on our message, its color will alternate between blue and red. This feature is self contained, making the component reusable. But right now, our state  changes only when we act on it. Why not make it evolve by itself? Let’s make it update the color when the text changes.&lt;/p&gt;

&lt;p&gt;Remember that our component started pure, always returning the same tree when provided with the same props. We added state, but to manipulate it, we need an external event. To link state and props together, and to react to changes to both, we need a way to detect when they’ve been modified. And that’s what &lt;strong&gt;useEffect&lt;/strong&gt; give us. With &lt;strong&gt;useEffect&lt;/strong&gt;, you can have a piece of logic that will run once the component is mounted — created and added to the web page — and when any element of a provided set of state variables and props — the dependencies — is updated. For our component, we have a unique element to observe — the text prop. Here is the new code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;Message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setColor&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&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="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;setColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nx"&gt;setColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&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="s1"&gt;red&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="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&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;text&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&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;p&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;}}&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;text&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;/p&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;That’s when it’s got tricky. We now have multiple stages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The component is created, returning an initial component tree and registering our effect.&lt;/li&gt;
&lt;li&gt;Our effect is run once for the creation of the component.&lt;/li&gt;
&lt;li&gt;Then it will run for every change to its dependency.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s why we started with &lt;code&gt;null&lt;/code&gt; as the value of the &lt;code&gt;color&lt;/code&gt; state. The main logic is to alternate between two colors, we need a third value to detect when  it’s the initial run. In the component tree, we then alleviate for this third value, as it’s not a correct value for the specifications of our component. A special note: If you return a function inside your effect, it will be executed when the component is destroyed. And if the dependencies is an empty set, the effect will be executed only once, right after the component is created, which is useful for initialization.&lt;/p&gt;

&lt;p&gt;With these two hooks, you can replicate the majority of the features that were only possible with class component. Another two hooks I find useful are &lt;strong&gt;useRef&lt;/strong&gt; and &lt;strong&gt;useMemo&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;useRef&lt;/strong&gt; comes in when you want to store a value after the code of the component is executed for the next execution, but you don’t want its mutation to trigger a new execution. It acts like a global variable in respect to the component. If we take the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;Message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;interval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useRef&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setColor&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&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;interval&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(()&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;setColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&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="s1"&gt;red&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="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&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;p&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;}}&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;text&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;/p&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;The component is now flashing between blue and red every second. When we unmount the component, we need to remove the interval to stop &lt;code&gt;setColor&lt;/code&gt; being called. The long-lived aspect that &lt;strong&gt;useRef&lt;/strong&gt; provides is useful in that case. Notice that we don’t update the object returned by &lt;strong&gt;useRef&lt;/strong&gt;, but its property &lt;code&gt;current&lt;/code&gt;. We removed the &lt;code&gt;text&lt;/code&gt; prop from the dependencies set, as our effect role is to initialize the interval. Additionally, it returns a function to be executed when the component is unmounted, clearing the interval.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;useMemo&lt;/strong&gt; is for improving the performance of our code. Sometimes, we have to do computation on our state and props, resulting in a new value. If we add the code to the body of our component, it will be run each update. &lt;strong&gt;useMemo&lt;/strong&gt; allows us to run the computation when the dependencies set changes and not on every render. Let’s take a look at an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;Message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setColor&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&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="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;setColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nx"&gt;setColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&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="s1"&gt;red&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="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&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;text&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;bgColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;getInvertedColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&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;p&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;bgColor&lt;/span&gt;&lt;span class="p"&gt;}}&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;text&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;/p&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;&lt;code&gt;getInvertedColor&lt;/code&gt; is our heavy computation code. &lt;strong&gt;useMemo&lt;/strong&gt;, takes a function and a dependencies array. The body of that function should be statements that we could have put inside the body of the component, and should follow the same pure paradigm — no side effect. The return value is returned directly by useMemo. The function executes on the mounting stage and when the dependencies update. But the return value will be stored — memoized — and return directly otherwise. We can mention the &lt;code&gt;useCallback&lt;/code&gt; hook, which memoizes a function instead.  &lt;/p&gt;

&lt;p&gt;Tehe most important part is that you can refactor the above code to create your own hook, making it possible to share functionalities between components.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;useColorFromText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setColor&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&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="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;setColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nx"&gt;setColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&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="s1"&gt;red&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="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&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;text&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;color&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="nx"&gt;Message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useColorFromText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&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;p&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;}}&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;text&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;/p&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;Original posted on &lt;a href="https://hescaide.me/blog/2022/02/02/the-usage-of-react-hooks/"&gt;hescaide.me&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>hooks</category>
    </item>
    <item>
      <title>A day with Ansible</title>
      <dc:creator>Holy-Elie Scaïde</dc:creator>
      <pubDate>Mon, 31 Jan 2022 22:58:04 +0000</pubDate>
      <link>https://dev.to/skydevht/a-day-with-ansible-44ka</link>
      <guid>https://dev.to/skydevht/a-day-with-ansible-44ka</guid>
      <description>&lt;p&gt;While my work has been focused on frontend, I had to interact with backend systems on a few projects where I was either the most knowledgeable person on the team or working alone. In these cases, my usual solution was either serverless (Firebase Functions, AWS Lambda) or write an API using Node with Express and Laravel and hosting it with Heroku. I like the container paradigm of Heroku, starting fresh at every deployment and everything happens automatically upon pushing the changes.&lt;/p&gt;

&lt;p&gt;But costs with Heroku ramp up quickly when scaling. I'm working on a side project and I decided to explore less expensive options, settling on a VPS at the end. I then quickly set it up one and my project was online.&lt;/p&gt;

&lt;p&gt;I found that I'm not comfortable with manual operations,  when there is a non-zero chance that I will have to redo them in the future. Which, I assume, will be in a more stressful situation. I looked into automation tools and I found &lt;a href="https://www.chef.io/"&gt;Chef&lt;/a&gt;, &lt;a href="https://www.ansible.com/"&gt;Ansible&lt;/a&gt;, and &lt;a href="https://puppet.com/"&gt;Puppet&lt;/a&gt; among others. I settled on Ansible due to being less complex to get started with.&lt;/p&gt;

&lt;h1&gt;
  
  
  So, Ansible?
&lt;/h1&gt;

&lt;p&gt;Installing Ansible was straightforward on both my M1 MBA and my Linux box. I needed something to test it with, so I used my Linux server as my learning environment because Vagrant (with VirtualBox) does not support M1 processors. A quick &lt;em&gt;Vagrantfile&lt;/em&gt; later, and my virtual machine was ready.&lt;/p&gt;

&lt;p&gt;As I learned more quickly by doing, I cloned a project that was doing the same thing I was aiming for, creating a deployment environment for Laravel. The project was last updated in 2018, so I figured there will be ample stuff to update in it.&lt;/p&gt;

&lt;p&gt;The first thing I learned was “Inventory”. Inventory files lists all the hosts that you need to interact with, alongside their connection parameters. You can assign a name to them and group them. When using that inventory files, you can filter the particular hosts you want by using a pattern. When using Ansible to provision the virtual machine(s) created by vagrant, the latter will generate an inventory files that will automatically be used when provisioning.&lt;/p&gt;

&lt;p&gt;The second thing I learned was “Roles”. Roles are collections of tasks and other related things (Variables, Templates, …) inside a common directory, which can later be shared if needed. Tasks are action items that will be executed against the hosts. I had one single host that has everything, but if you wanted a more modular architecture, you could have roles for the web servers, the database servers, the storage servers, including a shared role that contains the common tasks.&lt;/p&gt;

&lt;p&gt;The third thing I learned was “Playbooks”. Playbooks are instruction manuals describing policies or executing a particular set of tasks. Inside the project I was using as a reference, there were playbooks for each particular server type, which included the &lt;code&gt;common&lt;/code&gt; role and the role for that particular server and targeted a specific set of hosts inside the inventory. There was another playbook that included the others that could be used to provision a whole site.&lt;/p&gt;

&lt;h1&gt;
  
  
  In the end
&lt;/h1&gt;

&lt;p&gt;I've written a few bash and python scripts before. But, I was amazed by the possibilities that Ansible offered. My first use case was writing a playbook that will update my web application using git and install the new dependencies using composer when needed (which I used a lot as I'm actively developing it). There are numerous modules built by the community that will make the process a breeze (the above playbook only has two tasks). The documentation aspect alone is worth it when in a team context.&lt;/p&gt;

&lt;p&gt;Originally posted on &lt;a href="https://hescaide.me/blog/2022/01/29/a-day-with-ansible/"&gt;hescaide.me&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ansible</category>
    </item>
    <item>
      <title>Writing native modules</title>
      <dc:creator>Holy-Elie Scaïde</dc:creator>
      <pubDate>Sat, 08 Jan 2022 23:11:04 +0000</pubDate>
      <link>https://dev.to/skydevht/writing-native-modules-3212</link>
      <guid>https://dev.to/skydevht/writing-native-modules-3212</guid>
      <description>&lt;p&gt;One of the milestones of being a React Native developer is writing your first native module. At first, it may appear a daunting task, but the module part is easy to wrap your head around. Here are a few important points to keep in mind. The same applies to Cordova.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Bridge
&lt;/h2&gt;

&lt;p&gt;The bridge concept is not a unique to React Native. It is a common occurrence when you’re dealing with many languages or multiples platforms. It is how two different layers in a single system communicate with each other.&lt;/p&gt;

&lt;p&gt;The main logic of the application resides usually in one of the layer, while the feature will be implemented in the other layer. The first will be the caller and the second will be the callee. For the two to communicate, there should be a common ground between the two, which we can call the host.&lt;/p&gt;

&lt;p&gt;The host can be the same as one of the layers. In game engines, which often need to add a scripting layer, the host is the caller and the callee will be the scripts that are written to customize components’ behaviors. In our case (React Native and Cordova) the host is the callee. Our code reside in the JavaScript interpreter (custom engine or web view).&lt;/p&gt;

&lt;p&gt;As the host is the one running the layers, it is trivial for it to customize their engine or interpreter. It will provide new functions in addition to the standard ones, which will be how the layer will get back to the host. Those functions may be arranged in a more cohesive set for a more intuitive use. It will replicate the caller layer’s data types in the callee layer.&lt;/p&gt;

&lt;h2&gt;
  
  
  A module is a plugin
&lt;/h2&gt;

&lt;p&gt;Occasionally, the callee layer will need to execute before getting called, or even before the main layer is executed. In those cases, there will be a general framework to follow with hooks. The framework is to tell the host the functions to call, initialize or destroy the module. The hooks are there to add new features to the host without the other layer intervention (which can not access these parts directly in our case). These hooks are why we normally need a module in the first place.&lt;/p&gt;

&lt;h2&gt;
  
  
  A module is a library
&lt;/h2&gt;

&lt;p&gt;But, how do we architect the module in the first place? From the host's perspective, it needs to be executable logic. While the host is initializing, it may run initialization code in the module. The same when the host is terminated. Logic in the module will mirror the host lifecycle. But from the perspective of the caller layer, it needs to be a library.&lt;/p&gt;

&lt;p&gt;Ordinarily, the module purpose is to provide a new feature to the caller layer. It needs to be architected the same way as the libraries native to the caller layer. Which is to provide a consistent set of functions (API) for the caller layer to use. At the callee side, we need to mark those functions, and they need to follow the same signature (using the replicated data types).&lt;/p&gt;

&lt;h2&gt;
  
  
  In conclusion
&lt;/h2&gt;

&lt;p&gt;The above is my mental model when writing a native module. The implementation will differ based on the platforms, but it provides a quick way to pick the relevant parts to learn from the documentation and example codes.&lt;/p&gt;




&lt;p&gt;Originally published on &lt;a href="https://hescaide.me/blog/2022/01/07/writing-native-modules/"&gt;hescaide.me&lt;/a&gt;&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>cordova</category>
    </item>
    <item>
      <title>My React Native Architecture Template</title>
      <dc:creator>Holy-Elie Scaïde</dc:creator>
      <pubDate>Wed, 14 Oct 2020 20:06:56 +0000</pubDate>
      <link>https://dev.to/skydevht/my-react-native-architecture-template-hoh</link>
      <guid>https://dev.to/skydevht/my-react-native-architecture-template-hoh</guid>
      <description>&lt;p&gt;I've been working with React Native for about 3 years now. I've built applications mainly for small businesses and entrepreneurs as a freelancer during this time. As the months go by, I find myself using pretty much the same architecture again and again.&lt;/p&gt;

&lt;p&gt;My coding has been influenced heavily by Laravel's framework principles as well as "Clean architecture" from Robert C. Martin. One of the core principles I follow is loose coupling. As I work mostly alone, it allows me to quickly replace modules I've implemented for another one.&lt;/p&gt;

&lt;h1&gt;
  
  
  Main files
&lt;/h1&gt;

&lt;p&gt;The most important files for me are the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;index.js&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;App.js&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;config.js&lt;/code&gt;,
These reside mostly at the root of the project folder. I consider &lt;code&gt;index.js&lt;/code&gt; as the loading logic. I rarely modify it from the default that is generated when creating the project using &lt;code&gt;react-native-cli&lt;/code&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;App.js&lt;/code&gt; Is the super container. It represents the root of the React root and it is where I integrate any React Native library that deals with context, like Theme, Navigation and Redux. It is the only React files that depends on non-react modules.&lt;/p&gt;

&lt;p&gt;I seldom use a config file. It mostly reserved if I have constants in the business logic that can be modified later or any temporary service configurations.&lt;/p&gt;

&lt;h1&gt;
  
  
  Navigation
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;react-navigation&lt;/code&gt; library has been enough for my needs so far. I usually configure it inside one file name &lt;code&gt;navigator.js&lt;/code&gt;. It is the interface between the screens and the main App component. It does not handle navigation logic, just the configuration.&lt;/p&gt;

&lt;h1&gt;
  
  
  Screens
&lt;/h1&gt;

&lt;p&gt;I'm a big fan of the functional paradigm. Now I'm using functions for every React components I write. I think it is better for handling side effect from a readability point of view. My preferred method is using files directly like &lt;code&gt;Screen.js&lt;/code&gt; instead of &lt;code&gt;Screen/index.js&lt;/code&gt; I've seen people do.  Styling is written at the end of the file and I use &lt;strong&gt;flow&lt;/strong&gt; for specifying the type of the props. &lt;/p&gt;

&lt;p&gt;Screens are the main containers or smart components. That is where I write business logic and side effect code (like asynchronous code that deals with services). The UI is handled mostly by the components.&lt;/p&gt;

&lt;h1&gt;
  
  
  Components
&lt;/h1&gt;

&lt;p&gt;Most of the components are dumb. They display data or accept input from the user and hand out the result to the main container. They follow the same template as screens. I often write component in the same file of the screen that will host them (Row in a list). But reusable ones are stored under the &lt;code&gt;components&lt;/code&gt; subfolder of the project.&lt;/p&gt;

&lt;h1&gt;
  
  
  Services
&lt;/h1&gt;

&lt;p&gt;Services are for everything that interacts with the outside world. REST API, BaaS, Persistent Storage, ... all fall under that category. The key is to decouple the service from the main code. Apart from that file, there shouldn't be any mention of the service anywhere else (Apart from &lt;code&gt;config&lt;/code&gt;.js). It is mostly the configuration logic that will be inside the file and a collection of functions to use elsewhere.&lt;/p&gt;

&lt;h1&gt;
  
  
  Utility functions
&lt;/h1&gt;

&lt;p&gt;These are small functions that contains logic you find yourself reusing throughout the app. Things like concatenating the first name and last name of the user, formatting strings, calculating various values... I just group them by category (&lt;code&gt;user.js&lt;/code&gt;, &lt;code&gt;format.js&lt;/code&gt;) and put them inside a &lt;code&gt;util&lt;/code&gt; folder.&lt;/p&gt;

&lt;h1&gt;
  
  
  State Management
&lt;/h1&gt;

&lt;p&gt;This is an optional one. I mostly use Redux for caching API responses. It permits me to go from an asynchronous paradigm to a reactive one. I found myself not using it with Firebase and Apollo GraphQL as the latter ones have their own caching mechanism. When I do use Redux, I used the &lt;code&gt;rematch&lt;/code&gt; library is it eliminates a lot of the boilerplate.&lt;/p&gt;

&lt;h1&gt;
  
  
  Utility Libraries
&lt;/h1&gt;

&lt;p&gt;These deserve their own section. The most notable examples are theming and localization libraries. They do not deal with the outside world but are important enough to not be relegated to the &lt;code&gt;util&lt;/code&gt; folder. They are mostly stored alongside &lt;code&gt;App.js&lt;/code&gt; as they will be integrated there.&lt;/p&gt;

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

&lt;p&gt;This is my own internal template that I use when writing React Native app. There is not a linked GitHub repository as I modify parts of it when I'm working on a project depending on the requirements. It helps me switch whatever I want easily as the tight coupling is restricted to a few locations, everything else is connected together by conventions. Do you use something similar in your own React Native project?&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>react</category>
      <category>docs</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Back to drawing</title>
      <dc:creator>Holy-Elie Scaïde</dc:creator>
      <pubDate>Wed, 11 Dec 2019 17:58:10 +0000</pubDate>
      <link>https://dev.to/skydevht/back-to-drawing-mkb</link>
      <guid>https://dev.to/skydevht/back-to-drawing-mkb</guid>
      <description>&lt;p&gt;I think I wrote a comment here explaining how drawing helped me be a better programmer. And it has the additional benefits of being a hobby outside coding. While you can be creative as a programmer, those moments can be rare and most of the time, you're just redoing the same thing every day.&lt;/p&gt;

&lt;p&gt;I left drawing when I decided to learn to program full time. I was going through university and I couldn't handle the lectures, learning to code and drawing at the same time (gotta rest). I don't regret my decision as I'm now working full time as a programmer in a country where finding a good job is a rarity.&lt;/p&gt;

&lt;p&gt;Why I'm going back? Programming can be a very taxing activity mentally. Often, at the end of the days, I don't have any energy to spend on my own personal projects. And I don't really like spending my evening in front of the TV. I do sometimes read, but it requires the same amount of concentration (I read high fantasy stuff like The Malayan Book of the Fallen). So I chose to draw because, while it requires some level of concentration, it's more about eye-hand coordination than abstract thinking. And that fits perfectly my needs of low-key activity to do in my free time (And I think it's a good thing to have another marketable skill).&lt;/p&gt;

&lt;p&gt;And what about you? Any activities you like to do to get away from the programming world? Please share in the comments.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to create a website: The easy part</title>
      <dc:creator>Holy-Elie Scaïde</dc:creator>
      <pubDate>Fri, 01 Nov 2019 15:38:37 +0000</pubDate>
      <link>https://dev.to/skydevht/how-to-create-a-website-the-easy-part-2jm9</link>
      <guid>https://dev.to/skydevht/how-to-create-a-website-the-easy-part-2jm9</guid>
      <description>&lt;p&gt;My previous article was about the core technologies one has to know to create a website from scratch or modifying an existing one. But it takes time before one can be comfortable in these and developing a website using just HTML, CSS and JavaScript can be a tedious process. For one that is not experienced, there are easier ways to build a website.&lt;/p&gt;

&lt;h1&gt;
  
  
  CMS: Easy Configuration
&lt;/h1&gt;

&lt;p&gt;The first solution is to use a CMS (Content Management System). A CMS is a framework that does a lot of the heavy lifting for you, mainly the database. They come with a theme engine (allowing you to easily change the interface of the website), a plugin ecosystem (to add supplementary features to the built-in ones) and an admin panel for an easy configuration.&lt;/p&gt;

&lt;p&gt;One of the most popular ones is &lt;a href="https://wordpress.org/"&gt;WordPress&lt;/a&gt;. It is mostly used for blogging sites, but it is more versatile than that. There are also more specialised ones like &lt;a href="https://magento.com/products/magento-open-source"&gt;Magento&lt;/a&gt; for eCommerce and &lt;a href="https://moodle.org/"&gt;Moodle&lt;/a&gt; for eLearning. There are a lot of CMSs and you only have to choose what fits more your needs.&lt;/p&gt;

&lt;p&gt;But remember that although CMS are user-friendly tools, allowing a non-developer to manipulate the site, they are a development framework too. To get the most out of the CMS, you'd have to learn it fully and often you will see the need to develop some plugin to tie everything together or to rewrite a theme for visual that fits your brand. &lt;/p&gt;

&lt;h1&gt;
  
  
  Builder: For the non-dev
&lt;/h1&gt;

&lt;p&gt;But maybe a full-fledged CMS is not needed, especially if your content does not change often and you only want a way to display them. So what you need is a site builder. Site builders come with a WYSIWYG (What you see is what you get) editor and a library of widgets. Some also provide you with a collection of templates to choose from and edit later. Some allow you to edit the style by adding custom CSS or setting the value of a subset of the styling properties.&lt;/p&gt;

&lt;p&gt;An example of a site builder is &lt;a href="https://www.wix.com/"&gt;Wix&lt;/a&gt;. Another one is &lt;a href="https://www.squarespace.com/"&gt;Squarespace&lt;/a&gt;. They offer a no-code environment for people that do not need complicated features on their website and are fine with the default those platforms provide.&lt;/p&gt;




&lt;p&gt;As you have seen, you do not always need to go the tedious way of writing everything from scratch. It's often faster to go to with a CMS or a website builder. As a developer, the most efficient solution is not always the one you implement by yourself.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>cms</category>
      <category>websitebuilder</category>
    </item>
    <item>
      <title>How to create a website: The simple part</title>
      <dc:creator>Holy-Elie Scaïde</dc:creator>
      <pubDate>Sun, 27 Oct 2019 21:51:37 +0000</pubDate>
      <link>https://dev.to/skydevht/how-to-create-a-website-the-simple-part-235d</link>
      <guid>https://dev.to/skydevht/how-to-create-a-website-the-simple-part-235d</guid>
      <description>&lt;p&gt;Learning how to create a website is not the most difficult undertaking, but it can be tedious because of all the skills and technologies that may be required, especially if you're starting from scratch. And for beginners, it can be overwhelming. I remember getting stuck, my mind blank, not knowing what to do next. I'm here to give an overview of the different elements that go into creating your own site web.&lt;/p&gt;

&lt;h1&gt;
  
  
  A text editor: The tool of the trade
&lt;/h1&gt;

&lt;p&gt;First, you need a text editor. If you're on Windows, you can use Notepad because you only want the basic text (other programs add a lot of stuff in the file). Or you can download Visual Studio Code or Sublime Text which is more powerful than Notepad. And you can use them on other platforms too (Mac and Linux). Open the text editor, type &lt;code&gt;Hello, World!&lt;/code&gt; and save it as &lt;code&gt;first.html&lt;/code&gt;. Double click on the file created and it should be opened in your default browser. Congratulations! You've just created your first webpage.&lt;/p&gt;

&lt;h1&gt;
  
  
  HTML : Creating elements
&lt;/h1&gt;

&lt;p&gt;You may be wondering now how to add title or image (Who wouldn't?). For this, you need to use &lt;strong&gt;HTML&lt;/strong&gt; (HyperText Markup Language). It allows you to specify elements and their properties (attributes) on your document. Let's start with a default template:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!doctype html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Greetings Page&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Greetings&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello, World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The first line is a special one and it indicates that you will be writing &lt;strong&gt;HTML5&lt;/strong&gt; (There are multiple versions) which is the default on the web. The second line is the root tag. A tag is always written like these: &lt;code&gt;&amp;lt;*&amp;gt;children&amp;lt;/*&amp;gt;&lt;/code&gt; or &lt;code&gt;&amp;lt;*\&amp;gt;&lt;/code&gt;. The &lt;code&gt;*&lt;/code&gt; is the name of the element and the children can be other tags or text. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;html&lt;/code&gt; is a special tag and represents your whole document. It always has two children: &lt;code&gt;head&lt;/code&gt; and &lt;code&gt;body&lt;/code&gt;. The first one is mostly for specifying the document metadata like the title as we're doing now. The second one is where you put the content that will be showing on your web page. Now we're displaying a level 1 heading, specified by &lt;code&gt;h1&lt;/code&gt;, and a paragraph, specified by &lt;code&gt;p&lt;/code&gt;.&lt;br&gt;
You can learn more about the different tags with this interactive tutorial &lt;a href="https://www.w3schools.com/html/"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h1&gt;
  
  
  CSS: Make it pretty
&lt;/h1&gt;

&lt;p&gt;Your next question will be probably how to change the default styles for the text as we can do in a word processor (It's kinda boring now). You will be using another language called &lt;strong&gt;CSS&lt;/strong&gt; (Cascading StyleSheet) for that. Your browser has already a set of default styles for the different HTML tags. To create your own, there are two different ways.&lt;br&gt;
The first is to put it in the same HTML file. You will use the &lt;code&gt;style&lt;/code&gt; tag for that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can put this line of code inside the &lt;code&gt;head&lt;/code&gt; tag or the &lt;code&gt;body&lt;/code&gt; tag. The browser usually reads HTML files from top to bottom, so if you put your CSS after all elements, they will be briefly rendered with the default styles before the browser applies your own. So the usual way is to put them in the &lt;code&gt;head&lt;/code&gt; tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!doctype html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Greetings Page&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Greetings&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello, World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You should see that the heading text color is now red.&lt;/p&gt;

&lt;p&gt;The other way is to write a new file (let's call it first.css) and reference it inside the HTML file. The content of the new file will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&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;And you will reference it inside the HTML file like this (still in the &lt;code&gt;head&lt;/code&gt; tag):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!doctype html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Greetings Page&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"./first.css"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Greetings&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello, World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When writing CSS, you will specify the targeted elements by using a selector (now we're using &lt;code&gt;h1&lt;/code&gt; which targets every &lt;code&gt;h1&lt;/code&gt; element on the page. Then you can specify the properties that you want to change for the elements targeted, like the text color as we do now. By using various combinations of selectors and properties, you can create almost every layout you want (People have implemented really awesome designs with it).&lt;br&gt;
The same site has another tutorial just for CSS &lt;a href="https://www.w3schools.com/css/"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h1&gt;
  
  
  JavaScript: Action, yeah!!!
&lt;/h1&gt;

&lt;p&gt;So now, you know how to create elements and how to style them. But you have seen that a website is not only a document that presents information, but it can also have a lot of interactions (including those annoying popups). To interact with your web page, you will use a third language, JavaScript (Welcome to the Dev world, where almost everyone is a polyglot).&lt;br&gt;
As for CSS, you can use a tag or a file to include JavaScript code. But it will usually be placed at the end of the HTML file (inside the &lt;code&gt;body&lt;/code&gt; tag) to ensure that everything else has been created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!doctype html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Greetings Page&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"./first.css"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Greetings&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"greeting"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello, World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;greeting&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&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 JavaScript!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The same example using a JavaScript file (named &lt;code&gt;file.js&lt;/code&gt;):&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;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;greeting&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&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 JavaScript!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;and the HTML will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!doctype html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Greetings Page&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"./first.css"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Greetings&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"greeting"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hello, World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./first.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What we're doing in this code is wait 3 seconds (3000 milliseconds) then select the HTML element identified by greeting and change what's inside.&lt;br&gt;
&lt;a href="https://www.w3schools.com/js"&gt;Here&lt;/a&gt; is the tutorial for JavaScript.&lt;/p&gt;

&lt;h1&gt;
  
  
  Wrapping Up
&lt;/h1&gt;

&lt;p&gt;Those are the basics that you need to learn. Even with the advent of WYSIWYG tools, you need to be aware of these three in order to be proficient at using them.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>html</category>
      <category>css</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Being a Better (and Lazier) Programmer</title>
      <dc:creator>Holy-Elie Scaïde</dc:creator>
      <pubDate>Tue, 15 Oct 2019 15:22:47 +0000</pubDate>
      <link>https://dev.to/skydevht/being-a-better-and-lazier-programmer-bhf</link>
      <guid>https://dev.to/skydevht/being-a-better-and-lazier-programmer-bhf</guid>
      <description>&lt;p&gt;House building is one of the most common analogies to developing software. While you're required to follow some constraints (hardware, business), you're also given some leeway in how you go about it. However, we, the developers, have two advantages over house builders, which is the only physical work we do is entering text. And while the latter is required it's only a minor part in developing good and practical solutions.&lt;/p&gt;

&lt;p&gt;But often when beginning, we made the mistake that this is the only thing that matters. Writing code and the tools we use are more important than the solution that we use them for. I think most of our time should be devolved to planning and designing. And we can do so by reducing the time we take writing code. Here are some techniques that can help you do so.&lt;/p&gt;

&lt;h1&gt;
  
  
  Automation
&lt;/h1&gt;

&lt;p&gt;When starting a new project, you often have only one file and just a few commands to run the code in this file. But as your project grows, more commands need to be entered and often they will have conditional parameters based on your current intent. And entering those commands manually takes time.&lt;/p&gt;

&lt;p&gt;I believe that if I do something that is recurrent, it should be automated, even if it's only once per day. From shell scripts to specialized task runner, there are a plethora of solutions that you can use to reduce the time you spent compiling, testing and deploying things. The good news is that most of the online tools we use often provide a CLI equivalent which makes it more easier for us. And a lot of them can connect with each other easily or through third-party services.&lt;/p&gt;

&lt;h1&gt;
  
  
  Know your tools
&lt;/h1&gt;

&lt;p&gt;The Pragmatic Programmer book dedicates a whole section to the tools that we use to develop software (And it's still relevant, even if you're using an IDE as the latter is just a cohesive structure for those tools). You have to know your tools, their capabilities as well as their limitations. From the shell to your text editor, you'll often find that your productivity increases along with your mastery of the tools you're using.&lt;/p&gt;

&lt;p&gt;Even learning the shortcuts of your current editor can result in a huge productivity boost. It's faster using a sequence of keys that using the mouse. I use aliases for the commands that I find myself typing every day. I often read the manual just to explore in what new way I can use the tool. Being familiar with your tools is a necessary step if you want to reduce the time you spent using them and increase your productivity.&lt;/p&gt;

&lt;h1&gt;
  
  
  KISS
&lt;/h1&gt;

&lt;p&gt;As a programmer, we must resist the urge to overengineer our solution. While design patterns are useful, they are not a silver bullet. Remember that you're writing code for humans, not the computer. What you're writing will be maintained in the future, and your job is to make it easy for the next person to do so. Strive for clarity and simplicity. This one-liner could make you look smart, but it may result in lost hours trying to understand its purpose. A simple solution is always better than clever in the business world. So don't spend the whole day trying to optimize that function if it already satisfies the performance criteria.&lt;/p&gt;

&lt;p&gt;My belief as a programmer is that you don't have to work hard to bring value to your work. You're tasked with creating practical solutions to problems and your tools should not be a hindrance to that objective. And you should also keep in mind that your code (the physical aspect of your solution) will be the responsibility of another person in the future (or your future self). So you should make it easy for them to pick up where you left.&lt;/p&gt;

</description>
      <category>productivity</category>
    </item>
    <item>
      <title>How I understand React</title>
      <dc:creator>Holy-Elie Scaïde</dc:creator>
      <pubDate>Thu, 10 Oct 2019 13:32:59 +0000</pubDate>
      <link>https://dev.to/skydevht/how-i-understand-react-3pa2</link>
      <guid>https://dev.to/skydevht/how-i-understand-react-3pa2</guid>
      <description>&lt;p&gt;Every time I learn a new language, library or framework, I like to form a mental image of how it works and why it works that way. It helps tremendously with being able to provide practical solutions to business problems if you can discern their pros and cons easily. &lt;/p&gt;

&lt;p&gt;I don't learn the technical aspects by heart. You will retain them overtime by practicing regularly with the tool. Instead, I focus on the architectural level and I try to understand how everything fits together.&lt;/p&gt;

&lt;p&gt;As for React, I think there are three major aspects that one needs to understand well to be proficient with it. They are the virtual DOM, the components and the component's lifecycle.&lt;/p&gt;

&lt;h1&gt;
  
  
  The virtual DOM
&lt;/h1&gt;

&lt;p&gt;The DOM represents a document with a logical tree structure. Almost every UI library is represented with a tree structure because it helps with geometrical transformations and property inheritance. React's virtual DOM is a copy of that structure. Because modifying the real DOM is costly (Computing the new UI representation takes time), React executes the manipulation first on its copy, then compare the new and the old versions to determine the most performance effective way to update the real dom.&lt;/p&gt;

&lt;p&gt;That means that what you are writing and updating is not the real dom. It does not have the same properties and you should not treat it the same way. That also means that the React philosophy is also universal as the DOM in the browser is very similar in representation to the UI library in other platforms (Which explains React Native). It is still a tree structure but with new types of nodes. Here is &lt;a href="https://programmingwithmosh.com/react/react-virtual-dom-explained/"&gt;a post&lt;/a&gt; that explains the virtual DOM in more detail.&lt;/p&gt;

&lt;h1&gt;
  
  
  The components
&lt;/h1&gt;

&lt;p&gt;The components are each responsible for a section of the virtual DOM, which may contain other components. There are two types of components: classes, and functions. I like the latter as it's often easier to manipulate. With hooks, you can use a function instead of a class as I do now.&lt;/p&gt;

&lt;p&gt;As a developer, what you will be doing is create a tree of components that will include your owns and those provided by libraries. These components will accept props as inputs and will return the section of the tree they are responsible for. There are special components called HOC (Higher Order Components) which are functions that will return either your component with new additional props or a completely new component which will include your component as a child.&lt;/p&gt;

&lt;h1&gt;
  
  
  The component lifecycle
&lt;/h1&gt;

&lt;p&gt;So where does the business logic fits? In the case of a class component, there are various stages and React.Component class provides you with methods that will be called at each one of the stages. You choose the correct stage based on what you want to do. Some are called only once upon the creation (mounting) and the destruction (unmounting) of your component, other will be called many times when your component updates (triggered by a lot of things). Here is a &lt;a href="https://programmingwithmosh.com/javascript/react-lifecycle-methods/"&gt;more detailed explanation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Using functions make things cleaner. There is no more lifecycle to worry about. You are provided with the props as parameters and you need to return the tree's section. That's it. Now with hooks, you can do the same thing that the class component used to do. hooks are functions that will accept arguments based on the current props and optionally will return objects that can be used inside the functions. Those objects are not created inside the function scope so they won't be destroyed when the function return. I think they get destroyed when the section of the virtual DOM your component is responsible for is destroyed.&lt;/p&gt;

&lt;p&gt;So that's it, that is my mental model of what React is. There are a lot of other things like the relation between refs and real DOM, Babel, and JSX. But I think these three are the most important concepts you need to wrap your head around.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>react</category>
      <category>concepts</category>
    </item>
    <item>
      <title>Neovim after 2 years</title>
      <dc:creator>Holy-Elie Scaïde</dc:creator>
      <pubDate>Tue, 08 Oct 2019 15:34:02 +0000</pubDate>
      <link>https://dev.to/skydevht/neovim-after-2-years-3aa8</link>
      <guid>https://dev.to/skydevht/neovim-after-2-years-3aa8</guid>
      <description>&lt;p&gt;One of the most important things to being a developer is your development environment. Being productive depends a lot on what tools are available to you and how much are you familiar with them. As a coder, what software you use to write code will be one of your most important tools.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why Neovim?
&lt;/h1&gt;

&lt;p&gt;I strongly believe productivity is tied to comfort. And for me, being comfortable is being able to do more with less. I always tried to learn the keyboard shortcuts of every software I use as it's often the fastest and simplest way to do something. When I learned about the keyboard-driven nature of vim, it seemed perfect for me. It fitted with a goal I had at that time which was to lessen my usage of a mouse (I had a pretty bad trackpad so I had to use an external mouse).&lt;/p&gt;

&lt;h1&gt;
  
  
  My Needs
&lt;/h1&gt;

&lt;p&gt;Another thing I believe is a tool should be adapted to your needs, not the other way around. My expectations were shaped by my experience with the Jetbrains' products. Things that were essential for me were auto-completion (mostly for variables' names), linting code (a real time saver when fixing syntax errors), project management (mostly for launching commands inside a directory, git (for basic stuff like viewing diffs and committing) and snippets (that came later).&lt;/p&gt;

&lt;h2&gt;
  
  
  Auto-Completion
&lt;/h2&gt;

&lt;p&gt;For auto-completion, I started with &lt;a href="https://vimawesome.com/plugin/youcompleteme"&gt;youcompleteme&lt;/a&gt; inside of Vim. It was overly complex for me to install, so most of the time, I did not even bother and just use the default completion engine inside Vim. But after learning about &lt;a href="https://vimawesome.com/plugin/deoplete-nvim"&gt;deoplete&lt;/a&gt;, I made the switch to Neovim (it was easier to install in Neovim). Now, I'm using &lt;a href="https://vimawesome.com/plugin/coc-nvim"&gt;COC&lt;/a&gt; which is even easier to install and configure. The default completion engine would suffice, but using COC make it simpler for me.&lt;/p&gt;

&lt;h2&gt;
  
  
  Linting
&lt;/h2&gt;

&lt;p&gt;There's nothing that can speed up your coding like linting. Being able to correct syntax errors as well as correctly formatting your files help greatly during the development process. At first, I didn't know about Vim's &lt;code&gt;make&lt;/code&gt; system. I used &lt;a href="https://vimawesome.com/plugin/syntastic"&gt;Syntastic&lt;/a&gt;, then I switched to &lt;a href="https://vimawesome.com/plugin/ale"&gt;Ale&lt;/a&gt; when I moved to Neovim. Now, I know about the &lt;code&gt;make&lt;/code&gt; system and understand the edit-compile-fix process, but it is easier for me to use Ale.&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Management
&lt;/h2&gt;

&lt;p&gt;I don't need project management as the IDEs do. What I needed was switching between files quickly and launching commands in the context of the directory I opened the editor in. For the first one, I started with &lt;a href="https://vimawesome.com/plugin/ctrlp-vim-everything-has-changed"&gt;CtrlP&lt;/a&gt;. But, as soon as I heard about &lt;a href="https://vimawesome.com/plugin/fzf-vim"&gt;FZF&lt;/a&gt;, I switched to it. As for file explorer, I started by using the default one, Netrw, then nothing for a long time (FZF was enough). A few months back, I tried to used &lt;a href="https://vimawesome.com/plugin/denite-nvim"&gt;Denite&lt;/a&gt; and [The NERD Tree)(&lt;a href="https://vimawesome.com/plugin/nerdtree-red"&gt;https://vimawesome.com/plugin/nerdtree-red&lt;/a&gt;), but I could not feel comfortable with them so I moved back to FZF and Netrw.&lt;/p&gt;

&lt;h2&gt;
  
  
  Git
&lt;/h2&gt;

&lt;p&gt;My needs for git within the editor are the basic ones. I only need to be able to see which files were modified, to select which ones I'd stage and to quickly write the commit message. Since starting, I've used &lt;a href="https://vimawesome.com/plugin/fugitive-vim"&gt;fugitive&lt;/a&gt;. It fits perfectly within my process. I've added a couple of mappings to the most used commands.&lt;/p&gt;

&lt;h2&gt;
  
  
  Snippets
&lt;/h2&gt;

&lt;p&gt;I don't use snippets that much. But I've installed &lt;a href="https://vimawesome.com/plugin/ultisnips"&gt;UltiSnips&lt;/a&gt; and I've added a couple of snippets. It's very powerful and fast.&lt;/p&gt;

&lt;p&gt;And that's it. Neovim does everything I want from a text editor. I also analyze my coding process from time to time to see what I could improve in it. You can find my config here &lt;a href="https://gist.github.com/skydevht/87e76cd3354308804195fba0218e02c9"&gt;init.vim&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>neovim</category>
      <category>editor</category>
    </item>
    <item>
      <title>I wanna hack my keyboard</title>
      <dc:creator>Holy-Elie Scaïde</dc:creator>
      <pubDate>Wed, 25 Sep 2019 03:06:34 +0000</pubDate>
      <link>https://dev.to/skydevht/i-wanna-hack-my-keyboard-4e7f</link>
      <guid>https://dev.to/skydevht/i-wanna-hack-my-keyboard-4e7f</guid>
      <description>&lt;p&gt;A while ago, I took the plunge and decided to buy a mechanical keyboard. I've settled on the Ducky One 2 Mini (I should have bought the TKL version). And I don't regret it (Except for the missing dedicated arrow keys). I don't even notice it when I type and the RGB lights can be mesmerizing when I'm thinking.&lt;/p&gt;

&lt;p&gt;Last Saturday in the afternoon, I was updating the keyboard firmware when a thought struck me: Can I install custom firmware on it? I mean the default one is great, but I like to tinker with my devices at a lower level, not just with the options the manufacturer provided. I quickly searched on google to see if the firmware was open-sourced and unfortunately, it wasn't. But searching "ducky one 2 mini firmware" yielded promising results, particularly &lt;a href="https://geekhack.org/index.php?topic=100481.0"&gt;this post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This was the first time I heard about the &lt;a href="https://qmk.fm/keyboards/"&gt;QMK project&lt;/a&gt;. It's a custom firmware for mechanical keyboards and has a bunch of cool features. I liked what I was reading in the documentation and I decided to give it a go. &lt;/p&gt;

&lt;p&gt;I cloned the &lt;a href="https://github.com/GitWellBack/qmk_firmware"&gt;forked version&lt;/a&gt; of the guy (I still don't know his name) who worked on the version for Ducky and created a test branch. I first merged in the &lt;code&gt;mbi5042gp&lt;/code&gt; branch which I think it's the driver interface to control the RGB lights. Then I merged in the &lt;code&gt;duckyone2mini&lt;/code&gt; branch which contains the configuration for the keyboard. &lt;/p&gt;

&lt;p&gt;The first build failed due to missing files in the &lt;code&gt;lib/chibios-contrib&lt;/code&gt; folder. I recalled that this was a submodule and went in for the commit that may contain those missing files. Instead, I found a pull request made by the same guy (great work!). This pull request adds the missing files for the Microcontroller Unit included in the keyboard unit. So a quick &lt;code&gt;git remote add&lt;/code&gt;, then a &lt;code&gt;git checkout&lt;/code&gt; retrieved the needed files.&lt;/p&gt;

&lt;p&gt;Another build failed, but that was due to some errors in the Makefiles and a constant's name (I should make a pull request for that). They were easy to fix and a final build generated the &lt;code&gt;.hex&lt;/code&gt; file needed for the keyboard.&lt;/p&gt;

&lt;p&gt;I would say that the QMK firmware was very enjoyable and that it was fun to use. But I did not flash the keyboard. It's the only one I have and it will be $100+ gone if I bricked it in the process. And I do not have the necessary tools to do a full recovery (JTAG anyone). So here I am with a compiled firmware and no courage to try it. But I learned a few things during the process and will learn more about the QMK project in the feature.&lt;/p&gt;

</description>
      <category>hack</category>
      <category>keyboard</category>
      <category>microcontroller</category>
      <category>qmk</category>
    </item>
    <item>
      <title>Programming a peanut butter mill.</title>
      <dc:creator>Holy-Elie Scaïde</dc:creator>
      <pubDate>Mon, 23 Sep 2019 20:56:10 +0000</pubDate>
      <link>https://dev.to/skydevht/programming-a-peanut-butter-mill-gaj</link>
      <guid>https://dev.to/skydevht/programming-a-peanut-butter-mill-gaj</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;One of the end-of-year projects given to us by our microcontroller teacher was creating the technical specifications for a peanut butter mill powered by an 8051 microcontroller as well as writing the program that was going to run on the latter (Simulated as we were not going to build the actual mill). &lt;/p&gt;

&lt;h1&gt;
  
  
  Hardware
&lt;/h1&gt;

&lt;p&gt;The fictional mill should have a self-check process before starting and allows an optional second pass during milling. It should be able to self-clean at the end of the milling. Very much like this one, but where the container is enclosed as well as an additional container inside for the optional pass.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fg60622upi9wq7d02c7tc.jpg" 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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fg60622upi9wq7d02c7tc.jpg" alt="Peanut Butter Mill"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Requirements
&lt;/h1&gt;

&lt;p&gt;After pressing the start button, the program should check that at least 1/4 of the first container (The one containing the peanut) is filled and that the last container is empty. If the latter is not, it should turn on a warning light. When all these conditions are met, the milling process starts and the peanut butter is poured in the second container to be milled if the user has chosen to do two passes or the final container if he has chosen only one pass. The first motor stops when the first container is empty and the second motor will stop 5 minutes later if it was running&lt;/p&gt;

&lt;p&gt;The self-clean process is very simple. The first 2 containers are filled with water and start both motors. After 3 minutes, they are stopped and the containers are emptied into the final container. This is repeated one more time and the machine is reset.&lt;/p&gt;
&lt;h1&gt;
  
  
  Design
&lt;/h1&gt;

&lt;p&gt;Even if we are not building the actual mill, we need to design the interfaces to the microcontroller. The 8051 microcontroller has 4 byte-sized I/O ports. We choose to use 2 for the purpose of this program. &lt;/p&gt;
&lt;h2&gt;
  
  
  Inputs
&lt;/h2&gt;

&lt;p&gt;We have three switches and 3 sensors for the content's level of the containers. Two of the sensors are 2 bits wide because we needed 3 states:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;00 -&amp;gt; Empty&lt;/li&gt;
&lt;li&gt;01 -&amp;gt; 1/4&lt;/li&gt;
&lt;li&gt;11 -&amp;gt; Full&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The sensor for the final container is 1 bit, we only need to detect if it's empty. The three switches are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;S -&amp;gt; On if the machine is started&lt;/li&gt;
&lt;li&gt;A -&amp;gt; On if the cleaning process should be started&lt;/li&gt;
&lt;li&gt;M -&amp;gt; On if two passes are requested&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Outputs
&lt;/h2&gt;

&lt;p&gt;All 3 outputs are control bits. They are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mx -&amp;gt; Off if the motor x should be running&lt;/li&gt;
&lt;li&gt;P -&amp;gt; Off to fill the containers with water.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  Implementation
&lt;/h1&gt;

&lt;p&gt;As for the implementation process, we have used the [edsim51[(&lt;a href="https://edsim51.com" rel="noopener noreferrer"&gt;https://edsim51.com&lt;/a&gt;) for simulating the program we wrote. We have not used interrupts because it will only make it more complex. If it was a real machine, we would have to make it more asynchronous. A lot of looping as used when the machine was idling and when using the timers in the microcontroller.&lt;/p&gt;
&lt;h2&gt;
  
  
  Setup and Verification
&lt;/h2&gt;

&lt;p&gt;We start by setting the timer counter to be a 16 bits number (needed for later. We then read port 2 (inputs). We check if the final container is empty by reading its bit and light the warning light if it is set. if it's not, we check the first bit of sensor 1 (it's on if the first container is 1/4 full or more) and the start switch. We loop this part until all conditions are met.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight armasm"&gt;&lt;code&gt;&lt;span class="nl"&gt;mov&lt;/span&gt; &lt;span class="nb"&gt;tmod&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="nv"&gt;h&lt;/span&gt; &lt;span class="c"&gt;; set timer 1 to 16bit&lt;/span&gt;
&lt;span class="nl"&gt;verif&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;
&lt;span class="nl"&gt;mov&lt;/span&gt; &lt;span class="nb"&gt;p2&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="nv"&gt;ffh&lt;/span&gt; &lt;span class="c"&gt;; set p2 as input&lt;/span&gt;
&lt;span class="nl"&gt;mov&lt;/span&gt; &lt;span class="nb"&gt;acc&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;p2&lt;/span&gt;&lt;span class="c"&gt;; read p2 into the acc&lt;/span&gt;
&lt;span class="nl"&gt;jb&lt;/span&gt; &lt;span class="err"&gt;0&lt;/span&gt;&lt;span class="nb"&gt;e4h&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;ll&lt;/span&gt;&lt;span class="c"&gt;; jump if first bit of sensor 1 is not set (empty)&lt;/span&gt;
&lt;span class="nl"&gt;anl&lt;/span&gt; &lt;span class="nb"&gt;acc&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;41&lt;/span&gt;&lt;span class="nv"&gt;h&lt;/span&gt;&lt;span class="c"&gt;; isolate s and sensor 1 bits&lt;/span&gt;
&lt;span class="nl"&gt;xrl&lt;/span&gt; &lt;span class="nb"&gt;acc&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;41&lt;/span&gt;&lt;span class="nv"&gt;h&lt;/span&gt; &lt;span class="c"&gt;; acc will clear if s and sensor 1 first bit is set&lt;/span&gt;
&lt;span class="nl"&gt;jz&lt;/span&gt; &lt;span class="nb"&gt;lm1&lt;/span&gt; &lt;span class="c"&gt;; jump if acc is zero&lt;/span&gt;
&lt;span class="nl"&gt;jmp&lt;/span&gt; &lt;span class="nb"&gt;verif&lt;/span&gt;&lt;span class="c"&gt;; loop verification&lt;/span&gt;
&lt;span class="nl"&gt;ll&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;
&lt;span class="nl"&gt;clr&lt;/span&gt; &lt;span class="nb"&gt;p1.&lt;/span&gt;&lt;span class="err"&gt;0&lt;/span&gt; &lt;span class="c"&gt;; light warning led&lt;/span&gt;
&lt;span class="nl"&gt;jmp&lt;/span&gt; &lt;span class="nb"&gt;verif&lt;/span&gt; &lt;span class="c"&gt;; loop verification&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Milling
&lt;/h2&gt;

&lt;p&gt;Milling is very simple. We launch the first motor first. If two passes were selected (M is on), we wait until the second container is at least 1/4 full. Then, another looping until the first container is empty.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight armasm"&gt;&lt;code&gt;&lt;span class="nl"&gt;lm1&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;
&lt;span class="nl"&gt;clr&lt;/span&gt; &lt;span class="nb"&gt;p1.&lt;/span&gt;&lt;span class="err"&gt;4&lt;/span&gt; &lt;span class="c"&gt;; launch motor 1&lt;/span&gt;
&lt;span class="nl"&gt;jnb&lt;/span&gt; &lt;span class="nb"&gt;p2.&lt;/span&gt;&lt;span class="err"&gt;5,&lt;/span&gt; &lt;span class="nv"&gt;f1&lt;/span&gt; &lt;span class="c"&gt;; jump if clapet is not on&lt;/span&gt;
&lt;span class="nl"&gt;jnb&lt;/span&gt; &lt;span class="nb"&gt;p2.&lt;/span&gt;&lt;span class="err"&gt;2,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="c"&gt;; loop until sensor 2 says it's at least 1/4&lt;/span&gt;
&lt;span class="nl"&gt;clr&lt;/span&gt; &lt;span class="nb"&gt;p1.&lt;/span&gt;&lt;span class="err"&gt;5&lt;/span&gt; &lt;span class="c"&gt;;launch motor 2 if clapet is on&lt;/span&gt;
&lt;span class="nl"&gt;f1&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;
&lt;span class="nl"&gt;jb&lt;/span&gt; &lt;span class="nb"&gt;p2.&lt;/span&gt;&lt;span class="err"&gt;0,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="c"&gt;; loop until sensor 1 says empty&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As soon as the sensor reports that the container is empty, we stop the first motor, then wait 5 seconds (For the purpose of the simulation, we used seconds instead of minutes) to stop the second one. Then we loop until the clean process is instantiated (The switch A is on).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight armasm"&gt;&lt;code&gt;&lt;span class="nl"&gt;setb&lt;/span&gt; &lt;span class="nb"&gt;p1.&lt;/span&gt;&lt;span class="err"&gt;4&lt;/span&gt; &lt;span class="c"&gt;; stop motor 1&lt;/span&gt;
&lt;span class="nl"&gt;mov&lt;/span&gt; &lt;span class="nb"&gt;r0&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="c"&gt;; 5 seconds (5 * 20)&lt;/span&gt;
&lt;span class="nl"&gt;call&lt;/span&gt; &lt;span class="nf"&gt;secondsDelay&lt;/span&gt; &lt;span class="c"&gt;; delaying&lt;/span&gt;
&lt;span class="nl"&gt;setb&lt;/span&gt; &lt;span class="nb"&gt;p1.&lt;/span&gt;&lt;span class="err"&gt;5&lt;/span&gt; &lt;span class="c"&gt;; stop motor 2&lt;/span&gt;
&lt;span class="nl"&gt;jnb&lt;/span&gt; &lt;span class="nb"&gt;p2.&lt;/span&gt;&lt;span class="err"&gt;7,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="c"&gt;; loop until a is set&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Cleaning
&lt;/h2&gt;

&lt;p&gt;First, we open the water pump and check until that the first 2 containers are full to close it. Then we start both motors, wait 3 seconds, then stop them. This process is repeated one more time, then we stop everything.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight armasm"&gt;&lt;code&gt;&lt;span class="nl"&gt;mov&lt;/span&gt; &lt;span class="nb"&gt;r7&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="c"&gt;;  2 cycle&lt;/span&gt;
&lt;span class="nl"&gt;clean&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;
&lt;span class="nl"&gt;clr&lt;/span&gt; &lt;span class="nb"&gt;p1.&lt;/span&gt;&lt;span class="err"&gt;7&lt;/span&gt; &lt;span class="c"&gt;; open pump&lt;/span&gt;
&lt;span class="nl"&gt;checkEmpty&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;
&lt;span class="nl"&gt;mov&lt;/span&gt; &lt;span class="nb"&gt;acc&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;p2&lt;/span&gt; &lt;span class="c"&gt;; load port 2&lt;/span&gt;
&lt;span class="nl"&gt;anl&lt;/span&gt; &lt;span class="nb"&gt;acc&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="nv"&gt;fh&lt;/span&gt; &lt;span class="c"&gt;; isolate sensor 1 &amp;amp; 2&lt;/span&gt;
&lt;span class="nl"&gt;xrl&lt;/span&gt; &lt;span class="nb"&gt;acc&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="nv"&gt;fh&lt;/span&gt; &lt;span class="c"&gt;; clear if all is set&lt;/span&gt;
&lt;span class="nl"&gt;jnz&lt;/span&gt; &lt;span class="nf"&gt;checkEmpty&lt;/span&gt;&lt;span class="c"&gt;; loop until full&lt;/span&gt;
&lt;span class="nl"&gt;setb&lt;/span&gt; &lt;span class="nb"&gt;p1.&lt;/span&gt;&lt;span class="err"&gt;7&lt;/span&gt; &lt;span class="c"&gt;; close pump&lt;/span&gt;
&lt;span class="nl"&gt;clr&lt;/span&gt; &lt;span class="nb"&gt;p1.&lt;/span&gt;&lt;span class="err"&gt;4&lt;/span&gt;&lt;span class="c"&gt;; start motor 1&lt;/span&gt;
&lt;span class="nl"&gt;clr&lt;/span&gt; &lt;span class="nb"&gt;p1.&lt;/span&gt;&lt;span class="err"&gt;5&lt;/span&gt; &lt;span class="c"&gt;; start motor 2&lt;/span&gt;
&lt;span class="nl"&gt;mov&lt;/span&gt; &lt;span class="nb"&gt;r0&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="c"&gt;; 3 seconds (3 * 20)&lt;/span&gt;
&lt;span class="nl"&gt;call&lt;/span&gt; &lt;span class="nf"&gt;secondsDelay&lt;/span&gt;
&lt;span class="nl"&gt;setb&lt;/span&gt; &lt;span class="nb"&gt;p1.&lt;/span&gt;&lt;span class="err"&gt;4&lt;/span&gt; &lt;span class="c"&gt;; stop motor 1&lt;/span&gt;
&lt;span class="nl"&gt;setb&lt;/span&gt; &lt;span class="nb"&gt;p1.&lt;/span&gt;&lt;span class="err"&gt;4&lt;/span&gt; &lt;span class="c"&gt;; stop motor 2&lt;/span&gt;
&lt;span class="nl"&gt;djnz&lt;/span&gt; &lt;span class="nb"&gt;r7&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;clean&lt;/span&gt;
&lt;span class="nl"&gt;jmp&lt;/span&gt; &lt;span class="nb"&gt;finish&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Timer
&lt;/h2&gt;

&lt;p&gt;We decided to use the timer imperatively instead of via interrupts. We implemented it via a procedure. As the timer uses a 16 bits counter, we can only set it for a duration up to 65 536 us. We decided to go with 50 ms in and loop it to attain the expected duration. The number of loops is set in the &lt;code&gt;r0&lt;/code&gt; register (1 byte) before calling the timer subroutine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight armasm"&gt;&lt;code&gt;&lt;span class="c"&gt;; Setup Timer&lt;/span&gt;
&lt;span class="nl"&gt;fiftyMsDelay&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;
&lt;span class="nl"&gt;clr&lt;/span&gt; &lt;span class="nb"&gt;tr1&lt;/span&gt; &lt;span class="c"&gt;; stop timer&lt;/span&gt;
&lt;span class="c"&gt;; set up a 50 000 seconds delay by loading 65 536 - 50 000 = 15 536 (3CB0)&lt;/span&gt;
&lt;span class="nl"&gt;mov&lt;/span&gt; &lt;span class="nb"&gt;th1&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="nv"&gt;ch&lt;/span&gt;
&lt;span class="nl"&gt;mov&lt;/span&gt; &lt;span class="nb"&gt;tl1&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="nv"&gt;b0h&lt;/span&gt;
&lt;span class="nl"&gt;setb&lt;/span&gt; &lt;span class="nb"&gt;tr1&lt;/span&gt; &lt;span class="c"&gt;; start timer&lt;/span&gt;
&lt;span class="nl"&gt;jnb&lt;/span&gt; &lt;span class="nb"&gt;tf1&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="c"&gt;; loop until timer oveflow&lt;/span&gt;
&lt;span class="nl"&gt;clr&lt;/span&gt; &lt;span class="nb"&gt;tf1&lt;/span&gt; &lt;span class="c"&gt;; clear overflow&lt;/span&gt;
&lt;span class="nl"&gt;clr&lt;/span&gt; &lt;span class="nb"&gt;tr1&lt;/span&gt; &lt;span class="c"&gt;; stop timer&lt;/span&gt;
&lt;span class="nl"&gt;ret&lt;/span&gt;
&lt;span class="nl"&gt;secondsDelay&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;
&lt;span class="c"&gt;; save processor state&lt;/span&gt;
&lt;span class="nl"&gt;push&lt;/span&gt; &lt;span class="nb"&gt;psw&lt;/span&gt;
&lt;span class="nl"&gt;loopTimer&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;
&lt;span class="nl"&gt;call&lt;/span&gt; &lt;span class="nf"&gt;fiftyMsDelay&lt;/span&gt; &lt;span class="c"&gt;; 50 ms&lt;/span&gt;
&lt;span class="nl"&gt;djnz&lt;/span&gt; &lt;span class="nb"&gt;r0&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;loopTimer&lt;/span&gt; &lt;span class="c"&gt;; loop until r0 equal zero&lt;/span&gt;
&lt;span class="c"&gt;; restore processor state&lt;/span&gt;
&lt;span class="nl"&gt;pop&lt;/span&gt; &lt;span class="nb"&gt;psw&lt;/span&gt;
&lt;span class="nl"&gt;ret&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Improvements
&lt;/h1&gt;

&lt;p&gt;Our code is very simple and makes the milling process a very straightforward one. But in the real world, there are often edge cases that the programs would have to handle. Like whether the motor is actually running or pausing the milling process.&lt;/p&gt;

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

&lt;p&gt;Writing this was very stressful, but enjoyable as we have not had much experience writing assembly code. But we learn a lot during the implementation and it had become easier over time. Assembly is actually very simple, but also tedious to write. Next step will probably be porting this to Arduino. &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>microcontroller</category>
      <category>assembly</category>
      <category>8051</category>
    </item>
  </channel>
</rss>
