<?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: Ghost</title>
    <description>The latest articles on DEV Community by Ghost (@ghost).</description>
    <link>https://dev.to/ghost</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%2F34981%2F84f5bb25-3656-46b3-bb37-6690a62cc6da.jpg</url>
      <title>DEV Community: Ghost</title>
      <link>https://dev.to/ghost</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ghost"/>
    <language>en</language>
    <item>
      <title>Aphantasia, without mental imaginary</title>
      <dc:creator>Ghost</dc:creator>
      <pubDate>Tue, 25 Feb 2020 21:10:54 +0000</pubDate>
      <link>https://dev.to/ghost/without-mental-imaginary-2c7n</link>
      <guid>https://dev.to/ghost/without-mental-imaginary-2c7n</guid>
      <description>&lt;p&gt;I'm over 30 and I just learned that my mind doesn't work like most.&lt;/p&gt;

&lt;p&gt;This post will have a lot of "I" on it, will be very subjective but that is the very core of the thing.&lt;/p&gt;

&lt;p&gt;My whole life phrases like "picture in your mind", "visualize" and even "happy places", where figurative speech. Have you felt like everybody but you got a memo, that everyone are playing by some rulebook you never got?, apparently most people can actually see pictures in their head, when people imagine being on a beach, can actually see, hear and/or oven smell it, with different levels of detail and control.&lt;/p&gt;

&lt;p&gt;You may think, so what?, but if you are like me and you haven't realized yet, you should be getting some really weird feelings and maybe some things are starting to make sense somehow. All I can see with my eyes closed is black, just my eyelids from inside, I can't see pictures, 3D objects, not even a simple circle, not a sound, not a smell and not emotional connection with memories; I can recall past event of course, but just as if someone else where telling me.&lt;/p&gt;

&lt;p&gt;I never knew until now because I never had problems with spatial tasks, I actually excel on some of them, when I need to know how would a 3D object be rotated I just know, but I can't "see" it, no image forms in my mind, and until very recently I thought that it was the same of everyone. Talking with my mother yesterday, she explained me how she can close her eyes and "see" herself in a lake and even her feet under the water, full color, Ray Tracing activated and even with smells!, after some DDG searching (I refuse to make a verb out of the G word), I find out that is not her superpower, is my underpower?, apparently there are not many studies about it, aphantasia is now called and from 2% to 5% have it, most of us don't know it and some realize much older than me, some never find out.&lt;/p&gt;

&lt;p&gt;But wait a minute, so if I'm like that I'm somehow inferior of lesser?, hell no, I've noticed and is maybe related, that I can't hold grudges, probably because there are no emotions related to memories, they are just facts to me and is no specially interesting to delve in them, in fact I've never spend much time in the past (well in fact all my life is in my past, but you get what I mean) and thinking about the future is just about facts for me, cold planning; kinda zen out of the box; no constant and distracting multimedia feed in my head, just my own inner voice (not heard but still there) an interesting background narration and even group conversations with "simulated" people in my head.&lt;/p&gt;

&lt;p&gt;Maybe that's why some of us need or prefer more graphical information, we need those diagrams and graphs made for us, our minds can't draw them, or at least doesn't share the video feed with us. Is like the visual process is running in the background. I dream in multimedia but some can't even do that, some can "see" but not "hear", some can do all of them and in different degrees.&lt;/p&gt;

&lt;p&gt;I'm just finding this out, maybe some of you are realizing it now, maybe if you ask around you'll find others, it may help you understand yourself better and think how to work with others.Maybe you felt inadequate and it was just that you have been using the wrong "tools" or feeling dumb because techniques used by others are not a good fit for you. Aphantasia has his pros and cons just like being an Aspie, don't let other to convince otherwise. We are weird, deeply weird, and that is not fine, is better than fine, is awesome, nothing normal ever caused awe, haven't it?.&lt;/p&gt;

</description>
      <category>todayilearned</category>
      <category>todayisearched</category>
    </item>
    <item>
      <title>Rust: Project structure example step by step</title>
      <dc:creator>Ghost</dc:creator>
      <pubDate>Sat, 18 Jan 2020 23:47:55 +0000</pubDate>
      <link>https://dev.to/ghost/rust-project-structure-example-step-by-step-3ee</link>
      <guid>https://dev.to/ghost/rust-project-structure-example-step-by-step-3ee</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;The recent, hopefully temporal, death of Actix-web forced me to recheck some code of a pet project of mine that was not cleanly decoupled, some of my Diesel was mixed with my Actix, so I will share the process and stages of decomposition in Rust.&lt;/p&gt;

&lt;p&gt;This is thoroughly explained in The Book, but I'll try to make it more concise and simple, I'll skip many details in favor of clarity and of course the code itself is not useful, its only purpose is to show as simple as I could, the process step by step. I hope is helpful.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I also uploaded a repo in github (&lt;a href="https://github.com/robertorojasr/rust-split-example"&gt;https://github.com/robertorojasr/rust-split-example&lt;/a&gt;), if you clone it, each commit is another step in the process. And unlike this example, it runs (doesn't do much but runs)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This evolution should look like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Single file with everything on it.&lt;/li&gt;
&lt;li&gt;Move part of your code to a different module. (this step is missing in the repo, I forgot, sue me), please don't sue me.&lt;/li&gt;
&lt;li&gt;Move the module to a different file.&lt;/li&gt;
&lt;li&gt;Turn the module in a single file into a folder acting as a module with multiple files as sub-modules.&lt;/li&gt;
&lt;li&gt;Split your crate in a library and an executable that lives in the same directory tree.&lt;/li&gt;
&lt;li&gt;Move the executable and library to different crates (workspaces) with their own directory tree.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When our project gets bigger and more complex this strategies will help to keep the overall structure of your code manageable, clear and decoupled.&lt;/p&gt;

&lt;h2&gt;
  
  
  0. Starting point
&lt;/h2&gt;

&lt;p&gt;So you make you "Hello World" and everything is cool, you replace that println and start coding, soon you notice your lovely main.rs is getting bigger, filled with structs, functions and traits, some of them are link to each other but some are not, like Neo watching the Matrix you start to see some macro-structure and you tell to yourself, "myself, is time to tide this mess", of course you could design all before you start, (you should actually), but you want avoid overengineering and as this post shows (at least hopes to) is not necessary to take the big guns from the start.&lt;/p&gt;

&lt;p&gt;Lets say this is your current masterpiece, impressive, I know, I did it all by myself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c"&gt;// src/main.rs&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  1. A module
&lt;/h2&gt;

&lt;p&gt;So how do you start?, the first thing would be make modules to encapsulate some code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c"&gt;// src/main.rs&lt;/span&gt;

&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;something&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&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;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;but now there is a problem, even tho &lt;em&gt;A&lt;/em&gt; is defined in the same file, is no longer in the same module, so &lt;em&gt;main()&lt;/em&gt; have no idea &lt;em&gt;something&lt;/em&gt; is in there, so we have to import &lt;em&gt;something&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c"&gt;// src/main.rs &lt;/span&gt;

&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;something&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&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;use&lt;/span&gt; &lt;span class="nn"&gt;crate&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;something&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;// &amp;lt;- this is new&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now &lt;em&gt;main()&lt;/em&gt; knows that &lt;em&gt;something&lt;/em&gt; is here and that it can use everything public in there, but wait, there is nothing public in there, so we have to make public whatever we want to make visible to &lt;em&gt;main()&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c"&gt;// src/main.rs &lt;/span&gt;

&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;something&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c"&gt;// &amp;lt;- this is new&lt;/span&gt;
        &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c"&gt;// &amp;lt;- this is new&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c"&gt;// &amp;lt;- this is new&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;crate&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;something&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now &lt;em&gt;main()&lt;/em&gt; can see everything inside, of course you don't have to make all public, just what you need, and spare me the OOP argument that you shouldn't expose data, is just an example here, and OOP is not all there is BTW (a bit of sass here).&lt;/p&gt;

&lt;h2&gt;
  
  
  2. A module in other file
&lt;/h2&gt;

&lt;p&gt;But that doesn't solve the fact that your IDE gets laggy with your still huge file, in fact you just added more stuff!, to solve this you should start using Vim... I joking... (Am I?)&lt;/p&gt;

&lt;p&gt;So now you want to move that module outside, to other file, to live free and alone. &lt;/p&gt;

&lt;p&gt;So you keep the definition and the import in &lt;strong&gt;main.rs&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c"&gt;// src/main.rs&lt;/span&gt;

&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;something&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c"&gt;// the content of the module was here&lt;/span&gt;

&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;crate&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;something&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;and put the module content in your freshly made new file in the same folder as &lt;strong&gt;main.rs&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c"&gt;// src/something.rs &lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&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;When you added the module something in &lt;strong&gt;main.rs&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c"&gt;// src/main.rs&lt;/span&gt;

&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;something&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c"&gt;// the rest of it&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Rust automagically looks for it inside the file, if doesn't find it, looks for a file with the module name in the same folder (in this case src/) and if still doesn't find it looks for a folder with the module name and a file &lt;strong&gt;mod.rs&lt;/strong&gt; inside, there it looks for the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. A module in a folder with many submodules
&lt;/h2&gt;

&lt;p&gt;As mentioned in the last paragraph, we can split even more our module,&lt;br&gt;
all we have to do is make a folder &lt;strong&gt;something&lt;/strong&gt; so we get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/
 |_ main.rs
 |_ something.rs
 |_ something/

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



&lt;p&gt;Now, we could just rename &lt;strong&gt;something.rs&lt;/strong&gt; as &lt;strong&gt;mod.rs&lt;/strong&gt; and move it inside &lt;strong&gt;something/&lt;/strong&gt; but what's the point in that, we want to split things!, so we are gonna give &lt;em&gt;A&lt;/em&gt; and &lt;em&gt;B&lt;/em&gt;, (please don't name your stuff like that outside examples like this) their own modules. So we'll have this tree&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/
 |_ main.rs
 |_ something/
     |_ mod.rs
     |_ a.rs
     |_ b.rs

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



&lt;p&gt;But what happen with &lt;strong&gt;something.rs&lt;/strong&gt;, well my friend, you split it, &lt;em&gt;A&lt;/em&gt; goes to &lt;strong&gt;a.rs&lt;/strong&gt; and you can guess where &lt;em&gt;B&lt;/em&gt; went.&lt;/p&gt;

&lt;p&gt;Now &lt;em&gt;A&lt;/em&gt; and &lt;em&gt;B&lt;/em&gt; are in their own modules so we modify the imports accordingly&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c"&gt;// src/main.rs&lt;/span&gt;

&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;crate&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;something&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;a&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;// &amp;lt;- this is new&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;crate&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;something&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;b&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;// &amp;lt;- this is new&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But &lt;strong&gt;mod.rs&lt;/strong&gt; has now the responsibility to call their children, as I told before, when Rust check for &lt;strong&gt;something.rs&lt;/strong&gt; and doesn't find it will check for the folder &lt;strong&gt;something&lt;/strong&gt; and then inside look for a file named &lt;strong&gt;mod.rs&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c"&gt;// src/something/mod.rs&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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



&lt;p&gt;You may notice that this is the same thing we did with main at first. You can keep nesting modules as long as you like, just like that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c"&gt;// src/something/a.rs&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&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;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c"&gt;// src/something/b.rs&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&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;Fine and dandy, but now what? what if my project is a huge beast, what if I could reuse some of the code, let's say. You had a nice webapp with Diesel ORM and Actix-web on top, what is some day, let say Actix creator leaves and it's future is uncertain, what then huh?, what then?!&lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;Well, you could see your webapp as a library that deals with the DB, lets say with Diesel and a separate consumer in an executable with Actix-web or other framework that is not apparently dead, but maybe comes back ...&lt;/p&gt;

&lt;p&gt;You could also make a CLI UI for example that will use the same DB related codebase. Lets do it!&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Rust library with an executable file
&lt;/h2&gt;

&lt;p&gt;As you may have read and skip to more fun stuff, Rust recognize 2 kinds of crates (the official name of what I've been calling project, just because I'm a rebel) libraries and executables, you probably know the difference but for completion let put it simple, an executable is something you use directly and a library is something that is used by an executable.&lt;/p&gt;

&lt;p&gt;What we want in this case is to put our &lt;em&gt;main()&lt;/em&gt; in another file as an executable and leave all of our structs in a library for future reuse in other executables.&lt;/p&gt;

&lt;p&gt;Rust tries really hard to make things easy (because it feels guilty for all the suffering with burrows and lifetimes) so to make a crate a lib you just rename the file &lt;strong&gt;main.rs&lt;/strong&gt; to &lt;strong&gt;lib.rs&lt;/strong&gt;, voilá, now is a library, but lets make something useful with it, to do that, we'll make a new folder &lt;strong&gt;bin/&lt;/strong&gt; and we will copy our existing &lt;strong&gt;main.rs&lt;/strong&gt;, (now renamed &lt;strong&gt;lib.rs&lt;/strong&gt;) on it with some fancy and descriptive name, leaving you with this new tree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/
 |_ lib.rs // &amp;lt;- just a renamed main.rs
 |_ bin/ // &amp;lt;- this folder is new
 |_  |_ framework_that_broke_my_heart.rs // &amp;lt;- a copy of our ex-main.rs
 |_ something/
     |_ mod.rs
     |_ a.rs
     |_ b.rs   

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



&lt;p&gt;so, everything inside &lt;strong&gt;something/&lt;/strong&gt; will be untouched now, first we'll work in &lt;strong&gt;framework_that_broke_my_heart.rs&lt;/strong&gt;. The first thing will notice is that everything inside &lt;strong&gt;bin/&lt;/strong&gt; is in a bubble universe, even tho is inside our crate is not part of it anymore, think it like us and society (not you, well adjusted programmers..ugh), so we have to call our newly created library (you know, when we renamed &lt;strong&gt;main.rs&lt;/strong&gt; to &lt;strong&gt;lib.rs&lt;/strong&gt;) just as we where calling any library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c"&gt;// src/bin/framework_that_broke_my_heart.rs&lt;/span&gt;

&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="n"&gt;crate&lt;/span&gt; &lt;span class="n"&gt;this_example&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;// oh right, I never named&lt;/span&gt;
&lt;span class="c"&gt;// this crate, is the name you give in Cargo.toml&lt;/span&gt;
&lt;span class="c"&gt;// under [package] in tha *name* field&lt;/span&gt;
&lt;span class="c"&gt;// (don't use dashes on it)&lt;/span&gt;

&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;crate&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;something&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;a&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;crate&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;something&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;b&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;and in &lt;strong&gt;lib.rs&lt;/strong&gt; ex-main.rs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c"&gt;// src/lib.rs&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;something&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;// &amp;lt;- this is all, is like telling Rust&lt;/span&gt;
&lt;span class="c"&gt;// copy/paste everything inside `something` inside a `mod` here&lt;/span&gt;

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



&lt;p&gt;Everything inside something is untouched.&lt;/p&gt;

&lt;p&gt;We are close to the finish line, by now you may have wondered about the situation of that poor fellow whose heart was broken by the early demise of his favorite framework, and telling yourself, how the F* did he put all his Actix-web code inside a tiny little file, that file must be huge, a huge mess, but the whole idea was to split things and he just made everything worse!, well my fellow, this is when the next point comes.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Workspaces
&lt;/h2&gt;

&lt;p&gt;This turns you crate in smaller crates inside a big crate-ish umbrella, you could just split your code in 2 crates by now, after all, we claimed that the executable in the later step was already not part of the original crate and that's right. Spliting entirely the code in 2 separate crates is a valid choice, but many of the dependencies are common to both: the executable and the library; and it would be annoying to rebuild 2 times, test 2 times, etc. If both the library(es) and executable(s) are related to each other, you may want to treat them as 1 thing for building/testing/running purposes, you may also keep them both in the same repo.&lt;/p&gt;

&lt;p&gt;This one is gonna get a bit more complicated but not much.&lt;/p&gt;

&lt;p&gt;We are gonna make 2 crates inside our original one and glue them together. &lt;/p&gt;

&lt;p&gt;So we had this beauty:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./
 |_Cargo.toml
 |_Cargo.lock
 |_target/
 | |_ ... // we don't care about this, is made in the building process
 |
 |_src/
   |_ lib.rs
   |_ bin/
   |_  |_ framework_that_broke_my_heart.rs
   |_ something/
       |_ mod.rs
       |_ a.rs
       |_ b.rs   

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



&lt;p&gt;Sitting in ./ just next to the "Cargos" we just make two new crates.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo init --lib db_stuff
$ cargo init ftbmh // framework_that_broke_my_heart, too long,
// too lazy, again, this is an example, name your thing with
// common sense, don't be // funny in a real project, the fun
// will last about 10min, the pain much // more than that.

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



&lt;p&gt;The argument --lib the only think it does, is instead to make a &lt;strong&gt;main.rs&lt;/strong&gt; makes a &lt;strong&gt;lib.rs&lt;/strong&gt;, by default makes a executable.&lt;/p&gt;

&lt;p&gt;So we will get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./
 |_Cargo.toml
 |_Cargo.lock
 |_target/
 | |_ ...
 |
 |_src/
 |  |_ lib.rs
 |  |_ bin/
 |  |_  |_ framework_that_broke_my_heart.rs
 |  |_ something/
 |      |_ mod.rs
 |      |_ a.rs
 |      |_ b.rs
 |
 | // ^ that's the old part
 |
 |_db_stuff/    // this whole folder is new
 |   |_Cargo.toml
 |   |_src/
 |      |_ lib.rs // &amp;lt;- that's all the --lib does
 |_ftbmh/      // this whole folder is new
     |_Cargo.toml
     |_src/
        |_ main.rs

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



&lt;p&gt;Now, from the original &lt;strong&gt;Cargo.toml&lt;/strong&gt; we will move the parts as necessary to the new &lt;strong&gt;Cargo.toml&lt;/strong&gt;, for example the dependencies to whoever needs them.&lt;/p&gt;

&lt;p&gt;When you are done with that, just clean you good old &lt;strong&gt;Cargo.toml&lt;/strong&gt; and just put this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[workspace]
members = ["db_stuff", "ftbmh"]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That's it, now the original &lt;strong&gt;Cargo.toml&lt;/strong&gt; doesn't have a [package] or [dependencies] section; the original crate is now a shell.&lt;/p&gt;

&lt;p&gt;When you made the 2 crates inside (&lt;em&gt;dn_stuff&lt;/em&gt; and &lt;em&gt;ftbmh&lt;/em&gt;) cargo saw that you where inside an existing crate with its own git repo so didn't made one for them, your old repo is still good and healthy.&lt;/p&gt;

&lt;p&gt;Now remember that you splited your code and probably one part depends on others, in this case &lt;em&gt;ftbmh&lt;/em&gt; depends on &lt;em&gt;db_stuff&lt;/em&gt; so we have to add that dependency in the &lt;em&gt;ftbmh&lt;/em&gt; &lt;strong&gt;Cargo.toml&lt;/strong&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ftbmh/Cargo.tom

[package]
// your stuff

[dependencies]
// your dependencies
db_stuff = { path = "../db_stuff" } // as you may know the `..`
// in the path refers to the mother folder of the current one

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



&lt;p&gt;&lt;strong&gt;ftbmh/main.rs&lt;/strong&gt; already was outside the original crate as you may remember from the last step, so it's all done there and the crate db_stuff was used as an external crate already so everything is the same there too.&lt;/p&gt;

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

&lt;p&gt;We are done. What started as a simple single file project is now a complex crate with 2 workspaces, of course those could be 3 or 100, just rinse and repeat and the same for modules that are the namespaces of Rust.&lt;/p&gt;

&lt;p&gt;As you can see, if your code is properly decoupled, the whole process is very unobtrusive, of course is good to have the design planned from the start, but sometimes projects grow more than we thought, they get more complex and is good to have ways to easily adapt it without making a mess, is also good because you don't need to over engineer your solution afraid of a future operations like the one exposed here. You can grow your code organically.&lt;/p&gt;

&lt;p&gt;I hope this help someone, there is nothing new here, but I found that it was too disperse in the documentation and books. There is a lot more of it, I didn't talk much about the public/private and what is visible for whom by default, that is very well explained in both &lt;strong&gt;The Book&lt;/strong&gt; and the amazing &lt;strong&gt;O'Reilly "Programming Rust"&lt;/strong&gt;, I just tried to make a scaffold to make easy hanging the details later.&lt;/p&gt;

&lt;p&gt;Any corrections and suggestion, feel free to let me know specially if something is weirdly written, English is not my native language, this is my best for now.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>actix</category>
    </item>
    <item>
      <title>PSA about systemd</title>
      <dc:creator>Ghost</dc:creator>
      <pubDate>Tue, 24 Dec 2019 03:16:15 +0000</pubDate>
      <link>https://dev.to/ghost/psa-about-systemd-155c</link>
      <guid>https://dev.to/ghost/psa-about-systemd-155c</guid>
      <description>&lt;p&gt;Hello everyone,&lt;/p&gt;

&lt;p&gt;This is my first post and I hope is a useful one.&lt;/p&gt;

&lt;p&gt;For those new to Linux, you may heard about Debian voting on the init system, if you haven't, good for you, you may skip the rest, have a nice day :) .&lt;/p&gt;

&lt;p&gt;If you already know what systemd is, you can also can skip the rest and have a nice day too.&lt;/p&gt;

&lt;p&gt;How am I doing so far? I already lost 80% of the readers.&lt;/p&gt;

&lt;p&gt;I think this post is for the rest of you, you just got your first install or have been blissfuly unaware of all this init system debacle.&lt;/p&gt;

&lt;p&gt;TLDR: the whole thing doesn't matter until it does and that will probably be never, so stay blissfuly ignorant of the matter.&lt;/p&gt;

&lt;p&gt;For the last 1% of the readers, good for you. Those who are interested in the inner workings of the system have a wonderful journey ahead of you, but beware, there are some unworthy battles out there and I'm afraid this is one of those. These init system discussion lost it's technical value long ago and now is almost a religious one. If you want to learn something about it, avoid all things "Vs" or "best" or "comparisons", just go to the individual descriptions and technical inner workings.&lt;/p&gt;

&lt;p&gt;The Linux community can be a wonderfully passionated bunch and as such, can be vicious sometimes, we have to take the bad with the good I guess. So my advice is, don't get involved, these discussion at this point has very little nutritional value.&lt;/p&gt;

&lt;p&gt;Have a nice day.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>news</category>
    </item>
    <item>
      <title>State and Variable in React Hooks</title>
      <dc:creator>Ghost</dc:creator>
      <pubDate>Thu, 12 Sep 2019 07:48:01 +0000</pubDate>
      <link>https://dev.to/ghost/state-and-variable-in-react-hooks-1adl</link>
      <guid>https://dev.to/ghost/state-and-variable-in-react-hooks-1adl</guid>
      <description>&lt;p&gt;I am pretty new to react-hooks and I have yet to discover its real super powers. I want to ask if I am breaking anything if I do the sample code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;somevariable&lt;/span&gt;&lt;span class="o"&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;SomeComponent&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setState&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="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;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://insert-api/state&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="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&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="c1"&gt;//update `somevariable` whenever the state changes&lt;/span&gt;
   &lt;span class="nx"&gt;somevariable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;something&lt;/span&gt;
  &lt;span class="p"&gt;},[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;


 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;clickMe&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;somevariable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="c1"&gt;//do something to somevariable&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;button&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;clickMe&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;Click&lt;/span&gt; &lt;span class="nx"&gt;Me&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&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;Instead of putting &lt;code&gt;somevariable&lt;/code&gt;in the state I put it outside to make it kind of global. I did this because when I put &lt;code&gt;somevariable&lt;/code&gt; in its own state and set its value in the useEffect it results to endless loop but this one does not, so this is like my workaround. A lot of my functions rely on &lt;code&gt;somevariable&lt;/code&gt; that's why it's important for me to know if this approach is alright or not. &lt;/p&gt;

&lt;p&gt;PS. I don't know if it's alright to ask this here so please tell me if it's not and I will take it down. Thanks.&lt;/p&gt;

</description>
      <category>reacthooks</category>
      <category>question</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>The Power of Rules</title>
      <dc:creator>Ghost</dc:creator>
      <pubDate>Mon, 19 Aug 2019 11:38:57 +0000</pubDate>
      <link>https://dev.to/ghost/the-power-of-rules-fbe</link>
      <guid>https://dev.to/ghost/the-power-of-rules-fbe</guid>
      <description>&lt;p&gt;Every organization has their own rules they follow for better workflow. In terms of web development, what are some of the rules you have in your organization? Do you have a boilerplate or some style guide? Or do you set your own rules that you apply in your organization? How powerful are the rules you set that make your development as flawless as it can be? Leave a comment below. &lt;/p&gt;

</description>
      <category>discuss</category>
      <category>javascript</category>
      <category>boilerplate</category>
    </item>
    <item>
      <title>From JavaScript to Swift - Building a List in SwiftUI</title>
      <dc:creator>Ghost</dc:creator>
      <pubDate>Thu, 04 Jul 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/ghost/from-javascript-to-swift-building-a-list-in-swiftui-34bj</link>
      <guid>https://dev.to/ghost/from-javascript-to-swift-building-a-list-in-swiftui-34bj</guid>
      <description>&lt;p&gt;So you want to build a list with Apple's new declarative UI framework. Maybe you're used to building for the web like I am, and you think, "Let's mock up a data structure and iterate over it to make a list." Pretty straightforward, or so you thought. In JavaScript, you might do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Mock data structure&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;racers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Valentino Rossi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;team&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Yamaha&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="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Marc Márquez&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;team&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Repsol Honda&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="c1"&gt;// In React&lt;/span&gt;
&lt;span class="nx"&gt;racers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;racer&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&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;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;racer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&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;racer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;rides&lt;/span&gt; &lt;span class="kd"&gt;with&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;racer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;team&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="c1"&gt;// In ES6&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt; &lt;span class="o"&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;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.container&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;racers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;racer&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;item&lt;/span&gt; &lt;span class="o"&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;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;racer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;racer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; rides with &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;racer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&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;I thought I could do the same thing with SwiftUI. Define an array of dictionaries and iterate over them using something like SwiftUI's &lt;code&gt;ForEach&lt;/code&gt; or &lt;code&gt;List&lt;/code&gt; views. In UIKit, &lt;code&gt;List&lt;/code&gt; would be roughly equal to &lt;code&gt;UITableView&lt;/code&gt;, and from my experience with &lt;code&gt;UITableView&lt;/code&gt;, the table wants everything to be set up in a very particular fashion. So approaching cautiously, will &lt;code&gt;List&lt;/code&gt; require us to do some extra stuff, or can we just chuck some data in and the world will be well and good? Turns out, there's a bit more setup. This won't work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;SwiftUI&lt;/span&gt;

&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;RacerList&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;View&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

      &lt;span class="c1"&gt;// Mock data structure&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;racers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Valentino Rossi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s"&gt;"team"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Yamaha"&lt;/span&gt;
        &lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Marc Márquez"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s"&gt;"team"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Repsol Honda"&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;some&lt;/span&gt; &lt;span class="kt"&gt;View&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;racers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;racer&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;racer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;team&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;racer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;team&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kt"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; rides with &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;team&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&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="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 compiler throws this error: &lt;code&gt;Unable to infer complex closure return type; add explicit type to disambiguate&lt;/code&gt; , essentially boiling down to: "Hey, I don't understand what type you're returning." But didn't we say the &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;team&lt;/code&gt; optionals are strings when we unwrapped them?&lt;/p&gt;

&lt;p&gt;As it turns out, the problem isn't with the SwiftUI view code, it's with the data structure. &lt;strong&gt;Swift is a strongly typed, protocol-oriented language&lt;/strong&gt; (bold for my own sake). The data you pass into a &lt;code&gt;List&lt;/code&gt; needs to conform to the &lt;code&gt;Identifiable&lt;/code&gt; protocol so that it knows how to reference each item.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I like to think about protocols like this: protocols are to class inheritance as GraphQL is to REST. In regular class inheritance in most object-oriented languages, if you want to extend a class, you get all the methods and properties and other stuff from that class in your new sub-class. With protocols, you split up that baggage into different "slices" that you can tell your new classes to conform to. Much like GraphQL, where you tell the backend exactly what you want and what you don't want, protocols allow you to define exactly what you want your sub classes (or structs) to get.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can implement the &lt;code&gt;Identifiable&lt;/code&gt; protocol like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;SwiftUI&lt;/span&gt;

&lt;span class="c1"&gt;// Mock data structure&lt;/span&gt;
&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;Racer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Identifiable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;team&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;RacerList&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;View&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;some&lt;/span&gt; &lt;span class="kt"&gt;View&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Racer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Valentino Rossi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;team&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Yamaha"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Racer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Marc Márquez"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;team&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Repsol Honda"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;racers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;racers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;racer&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
            &lt;span class="kt"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;racer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; rides with &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;racer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;team&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Yay, it works! 🙌 Now if we're to start refactoring this a bit, we can take the data structure, or &lt;em&gt;model&lt;/em&gt; as it's known in the iOS world, and put it in a separate directory with all our models. Then, anywhere in our app we define a capital R &lt;code&gt;Racer&lt;/code&gt;, the compiler knows we are referencing our model, and hence has detailed information about how it conforms to &lt;code&gt;Identifiable&lt;/code&gt; and the type of each property.&lt;/p&gt;

&lt;p&gt;Done! This has been another round trip in what may become a loosely associated series of articles about how to do things in Swift coming from JavaScript land. Thanks for reading!&lt;/p&gt;

&lt;p&gt;More resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.apple.com/tutorials/swiftui/building-lists-and-navigation"&gt;SwiftUI List Tutorial by Apple&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Protocols.html"&gt;Protocols — The Swift Programming Language (Swift 5.1)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.apple.com/documentation/swiftui/identifiable"&gt;Identifiable - SwiftUI | Apple Developer Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.swiftbysundell.com/posts/different-flavors-of-view-models-in-swift"&gt;Different flavors of view models in Swift — Swift by Sundell&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>swift</category>
    </item>
    <item>
      <title>Stop Judging Books By Their Covers</title>
      <dc:creator>Ghost</dc:creator>
      <pubDate>Fri, 28 Jun 2019 15:38:22 +0000</pubDate>
      <link>https://dev.to/ghost/never-judge-books-by-their-covers-read-them-first-3c1a</link>
      <guid>https://dev.to/ghost/never-judge-books-by-their-covers-read-them-first-3c1a</guid>
      <description>&lt;p&gt;Many of us (myself included) are guilty of passing judgement on great books because of their jaded or pale or lackluster appearance or accompanying artistic material. Something which all too often prevents us from tapping into the insights which great authors or great content producers provide to us.&lt;/p&gt;

&lt;p&gt;In the digital age, the equivalent effect is our perception of stock photos, the images which accompany the articles that publications like the ones found on Medium produce.&lt;/p&gt;

&lt;p&gt;How often have we decided to pass up the opportunity to read something interesting because of a badly selected stock photo, or scrolled through a Medium article hurryingly as we pass judgement on the content of the article.&lt;/p&gt;

&lt;p&gt;But do we ever stop to think, to think about the awesome content we are missing when we make value judgements about articles or books, or podcasts, or even people. I’d argue that the answer to that question is nay.&lt;/p&gt;

&lt;p&gt;Why did I choose to write an entire post on this topic? The answer can be found in the pages of a book I recently purchased by Henry Allison, on the topic of the Transcendental Aesthetic.&lt;/p&gt;

&lt;p&gt;Allison, goes to painstaking lengths (through the use of complex metaphysical arguments) to attempt to prove to its reader that the reasoning which German Philosopher, Kant used in his various intertextual critiques had something quite important to say on the topic, and the reasoning as to why human beings make value judgements about abstract concepts such as beauty and appeal.&lt;/p&gt;

&lt;p&gt;In that sense, beauty is very much relative. What I find beautiful, you may not find beautiful and what I hold dear to my heart as something which retains aesthetic value, you may view through a lens of normality. &lt;/p&gt;

&lt;p&gt;This may include books, and their covers. That will always be the case unfortunately, so I guess this article is just a polite means of drawing our attention to the beauty within the pages of your next read.&lt;/p&gt;

&lt;p&gt;Happy reading!&lt;/p&gt;

</description>
      <category>todayilearned</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Am I using too many Coding sites?</title>
      <dc:creator>Ghost</dc:creator>
      <pubDate>Thu, 20 Jun 2019 00:00:22 +0000</pubDate>
      <link>https://dev.to/ghost/am-i-using-too-many-coding-sites-372p</link>
      <guid>https://dev.to/ghost/am-i-using-too-many-coding-sites-372p</guid>
      <description>&lt;p&gt;Hi guys,&lt;/p&gt;

&lt;p&gt;After some advice. Wanted to start coding for awhile now with the aim to change careers too.  And I also intend on starting a second degree in the autumn via distance learning. What I am worried about is that I keep flittering about with different websites that will teach me to “code”?&lt;/p&gt;

&lt;p&gt;Basically got a list of;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SoloLearn App.&lt;/li&gt;
&lt;li&gt;Udemy.com Courses&lt;/li&gt;
&lt;li&gt;Dev.to&lt;/li&gt;
&lt;li&gt;CodeAcademy&lt;/li&gt;
&lt;li&gt;The Odin Project&lt;/li&gt;
&lt;li&gt;freeCodeCamp&lt;/li&gt;
&lt;li&gt;w3schools.com&lt;/li&gt;
&lt;li&gt;Scrimba&lt;/li&gt;
&lt;li&gt;Pluralsight&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Which would you all recommend? I want to focus. But I also won’t the best one that will help me get a job in the industry.  &lt;/p&gt;

</description>
      <category>help</category>
      <category>codenewbie</category>
      <category>wheretostart</category>
    </item>
    <item>
      <title>Managing colors with extensions in Swift</title>
      <dc:creator>Ghost</dc:creator>
      <pubDate>Sun, 12 May 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/ghost/managing-colors-with-extensions-in-swift-10ci</link>
      <guid>https://dev.to/ghost/managing-colors-with-extensions-in-swift-10ci</guid>
      <description>&lt;p&gt;As a full-time developer on the web and part-time dabbler in the world of iOS development, I recently tried to create some color variables to use throughout a project in a single location. In most web development projects, you might put these inside a &lt;code&gt;utils&lt;/code&gt; folder and &lt;code&gt;import&lt;/code&gt; them wherever they are needed:&lt;/p&gt;

&lt;p&gt;Define color variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// utils/colors.js&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dark&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#151951&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// Default for all text&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Use the variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// components/container.js&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;dark&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../utils/colors&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Container&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="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="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;`color: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;dark&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="o"&gt;&amp;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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Container&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But in iOS development with Swift, there’s a problem with this. Unlike on the web, you don’t explicitly declare what you are importing and exporting in each file. Instead, methods of classes just magically become available throughout the project. While convenient, how will others reading your code know where the methods are coming from?&lt;/p&gt;

&lt;p&gt;We could &lt;a href="https://medium.com/@KaushElsewhere/better-way-to-manage-swift-extensions-in-ios-project-78dc34221bc8"&gt;use a Swift protocol&lt;/a&gt; and namespace all the utils in the same class, or do something a bit simpler which I prefer: prepend an &lt;code&gt;_&lt;/code&gt; before all extensions so we know they’re custom utility functions. The same example above would then look like this, using &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Extensions.html"&gt;extensions in Swift&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The usage of hex values for colors in iOS development requires some extra computation with &lt;code&gt;UIColor&lt;/code&gt; because Apple doesn’t accept hex values directly. See dozens of different implementations of this &lt;a href="https://stackoverflow.com/questions/24263007/how-to-use-hex-color-values"&gt;on StackOverflow&lt;/a&gt;. The following approach is borrowed from &lt;a href="https://www.youtube.com/watch?v=HPhqO0D1tG4"&gt;Jared Davidson&lt;/a&gt;, and the emoji logging idea from &lt;a href="https://medium.com/swift-programming/swift-prettify-your-print-statements-pt-1-64832bb7fafa"&gt;Andyy Hope’s Pretty in Print series&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Define color variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Colors.swift&lt;/span&gt;

&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;UIKit&lt;/span&gt;

&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;UIColor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// Defined colors&lt;/span&gt;
  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;_dark&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIColor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"#151951"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;hex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;hex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;UIColor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// Clean string&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;hexString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hex&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trimmingCharacters&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;in&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;whitespacesAndNewlines&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;uppercased&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;// Remove hash&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;hexString&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasPrefix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;hexString&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;at&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;hexString&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startIndex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Log warning if hex value is not 6 characters&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;hexString&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"⚠️ Hex value #&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;hexString&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; is not 6 characters.
      Please enter a value with 6 characters."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;UIColor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;white&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;rgb&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UInt32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="kt"&gt;Scanner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;string&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;hexString&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scanHexInt32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;rgb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;UIColor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nv"&gt;red&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CGFloat&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;rgb&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xFF0000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;255.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nv"&gt;green&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CGFloat&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;rgb&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0x00FF00&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;255.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nv"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CGFloat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rgb&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0x0000FF&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;255.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nv"&gt;alpha&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;1.0&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Use the color variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// CustomLabel.swift&lt;/span&gt;

&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;UIKit&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;CustomLabel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UILabel&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;required&lt;/span&gt; &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;?(&lt;/span&gt;&lt;span class="n"&gt;coder&lt;/span&gt; &lt;span class="nv"&gt;aDecoder&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;NSCoder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;coder&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;aDecoder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;textColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIColor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_dark&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;As I’m rapidly realizing, things you can do simply on the web with JavaScript don’t often come for free in iOS with Swift. This may be due to the relative newness of the language, particular design choices made by the architects, or a mix of both.&lt;/p&gt;

&lt;p&gt;While I know it’s not exactly helpful to apply web thinking to a native iOS context, I find it useful to compare and contrast implementations as I’m learning to write more idiomatic Swift. Hopefully, I’ll come out a better software developer at the end of the day.&lt;/p&gt;

&lt;p&gt;Cheers! 🍻&lt;/p&gt;

</description>
      <category>swift</category>
      <category>javascript</category>
    </item>
    <item>
      <title>We're all internet hoarders</title>
      <dc:creator>Ghost</dc:creator>
      <pubDate>Thu, 09 May 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/ghost/we-re-all-internet-hoarders-1i15</link>
      <guid>https://dev.to/ghost/we-re-all-internet-hoarders-1i15</guid>
      <description>&lt;p&gt;Over the past few years I’ve moved around a lot, living in Los Angeles, Seoul, Beijing and now Singapore. Over the course of these moves I pared down my belongings, so now everything fits in just two large pieces of luggage.&lt;/p&gt;

&lt;p&gt;But on the internet, this is so much more difficult to do. Most of us have more than a few suitcases worth of accounts and user data floating around out there. More so, even if we want to toss out some of these things, we usually can’t do it quickly or easily. For each account,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Log in (~30 seconds)&lt;/li&gt;
&lt;li&gt;Reset password? (~1-2 minutes)&lt;/li&gt;
&lt;li&gt;Export user data? (~1-2 minutes)&lt;/li&gt;
&lt;li&gt;Delete account (~1-2 minutes)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s 5 minutes per account. Now multiply that by all your accounts, of which I have, maybe 20 or 25. Now imagine you get stuck in a loop trying to reset a password or retrieve an old email address in a janky auth flow (Microsoft, looking at you).&lt;/p&gt;

&lt;p&gt;🤯 If only we could just &lt;code&gt;rm -rf accounts&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But the saga continues. Besides closing down old accounts, what about changing all your passwords to unique, secure ones and storing them in a password manager? I’ve been down that road already. Estimate many more days or weeks.&lt;/p&gt;

&lt;p&gt;The point is, It’s really hard to have just two suitcases worth of internet stuff because it lives in so many guarded places. I’m a fan of new projects emerging from this line of thinking like &lt;a href="https://solid.mit.edu"&gt;Solid&lt;/a&gt;, but it may be some time before every user is savvy enough to have something like their own personal “pod” or cloud.&lt;/p&gt;

&lt;p&gt;That being said, I’ve been thinking about a few actionable targets we can observe as developers that can help new apps avoid adding to this unwelcome trend:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do not require authentication, or if absolutely necessary, require only an email address.&lt;/li&gt;
&lt;li&gt;Store data locally or in a cloud already owned by the users.&lt;/li&gt;
&lt;li&gt;If data is stored remotely, make user data export and deletion as readily accessible as possible.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If all goes well, I plan on implementing these ideas in the side project I’m working on now. As for the rest of the apps out there that we depend on daily, the only way is to painstakingly reduce our dependency until cleaner alternatives are built.&lt;/p&gt;

&lt;p&gt;Cheers! 🍻&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>Does contributing to open-source companies increase the chance of a job there?</title>
      <dc:creator>Ghost</dc:creator>
      <pubDate>Thu, 03 Jan 2019 20:50:58 +0000</pubDate>
      <link>https://dev.to/ghost/does-contributing-to-open-source-companies-increase-the-chance-of-a-job-there-l0h</link>
      <guid>https://dev.to/ghost/does-contributing-to-open-source-companies-increase-the-chance-of-a-job-there-l0h</guid>
      <description>&lt;p&gt;Hello I'm coming from &lt;a href="https://news.ycombinator.com/item?id=18818597"&gt;HN&lt;/a&gt;&lt;br&gt;
As the title says, Does contributing to open-source companies such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Gitlab&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ghost&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Discourse&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Edx&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Google(Angular, Flutter, Tenserflow)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Facebook(React, React Native, Jest)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Increases (Dramatically) Chances of getting a job at said companies? to a point where said companies reach out to the contributor?&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>jobs</category>
    </item>
    <item>
      <title>Theme your Expo app with Redux and React Navigation</title>
      <dc:creator>Ghost</dc:creator>
      <pubDate>Sun, 23 Dec 2018 13:15:22 +0000</pubDate>
      <link>https://dev.to/ghost/theme-your-expo-app-with-redux-and-react-navigation-3p61</link>
      <guid>https://dev.to/ghost/theme-your-expo-app-with-redux-and-react-navigation-3p61</guid>
      <description>&lt;p&gt;Recently whilst developing a React Native app (with Expo), I built a simple tab navigator using React Navigation library. I wanted to theme the app so that the header would change colour depending on which page you’re on. For example on the primary page it would be red and on the secondary page, when you change tabs, the header would become blue.&lt;/p&gt;

&lt;p&gt;I ended up being able to do it using Redux. In this article, I will show you how you can theme your Expo app using Redux with React Navigation.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;P.S&lt;/em&gt;. This article is written in British English however, the code is written using American English to make it more consistent (colour vs color).&lt;/p&gt;

&lt;h4&gt;
  
  
  React Navigation
&lt;/h4&gt;

&lt;p&gt;Is a library that helps you simplify app navigation. The main reason for using this library is because it’s written purely in JavaScript so no native code (Swift/Android) is required to make it work. Also, it’s the &lt;a href="https://docs.expo.io/versions/latest/guides/routing-and-navigation"&gt;recommended navigation library&lt;/a&gt; for Expo.&lt;/p&gt;

&lt;h4&gt;
  
  
  Redux
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://redux.js.org/"&gt;Redux&lt;/a&gt; is used to manage global state across a React app, it can actually be used with both React and React Native. Of course, in this example, we will be using Redux with a React Native app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tM-VgMiB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A-5TV7URjutGKbjbeu-tiPg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tM-VgMiB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2A-5TV7URjutGKbjbeu-tiPg.png" alt=""&gt;&lt;/a&gt;Redux Store, from &lt;a href="https://css-tricks.com/learning-react-redux"&gt;&lt;/a&gt;&lt;a href="https://css-tricks.com/learning-react-redux"&gt;https://css-tricks.com/learning-react-redux&lt;/a&gt;/&lt;/p&gt;

&lt;p&gt;The diagram above explains pretty well why you may need to use Redux. Passing state can be tricky in more complicated React Native apps, with lots of components and sub-components. Using redux we can change the state in one component and Redux will update the state in all the other components.&lt;/p&gt;

&lt;p&gt;There are many great tutorials about Redux, I particularly liked this &lt;a href="https://www.youtube.com/watch?v=KcC8KZ_Ga2M"&gt;one&lt;/a&gt;. Here is brief summary how Redux will work with this app.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Redux sets the initial (state) colour to red&lt;/li&gt;
&lt;li&gt;Change tabs from main to the secondary page&lt;/li&gt;
&lt;li&gt;This dispatches an action to the Redux store (action is called toggleTheme)&lt;/li&gt;
&lt;li&gt;The store then calls a reducer&lt;/li&gt;
&lt;li&gt;The reducer (also called toggleTheme) updates the old state to a new state, it changes the colour from red to blue&lt;/li&gt;
&lt;li&gt;Redux updates the state in all the components&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is a GIF that might help clear up how it work.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r_OOHF7Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2AMtR6XLdEyZF-0-7X" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r_OOHF7Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2AMtR6XLdEyZF-0-7X" alt="Redux Flow, from http://slides.com/jenyaterpil/redux-from-twitter-hype-to-production#"&gt;&lt;/a&gt;Redux Flow, from &lt;a href="http://slides.com/jenyaterpil/redux-from-twitter-hype-to-production#"&gt;&lt;/a&gt;&lt;a href="http://slides.com/jenyaterpil/redux-from-twitter-hype-to-production#"&gt;http://slides.com/jenyaterpil/redux-from-twitter-hype-to-production#&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Prerequisite
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;An Expo/React Native app&lt;/li&gt;
&lt;li&gt;An Android emulator/device to test on&lt;/li&gt;
&lt;li&gt;Install the following dependencies
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"react-navigation": "^2.18.0",
"react-redux": "^5.1.1",
"redux": "^4.0.1",
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Solution
&lt;/h4&gt;

&lt;p&gt;Now to the interesting part of this article let’s take a look at the code.&lt;/p&gt;

&lt;h4&gt;
  
  
  Structure
&lt;/h4&gt;

&lt;p&gt;The project structure will look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;├── src
│ ├── actions
│ ├── components
│ ├── containers
│ ├── reducers
│ ├── screens
│ ├── store
| └── themes
├── package.json
├── App.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  themes
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BjhyWVoM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/528/1%2AFxf8Da_QXHpwMbVWLQoRxQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BjhyWVoM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/528/1%2AFxf8Da_QXHpwMbVWLQoRxQ.png" alt=""&gt;&lt;/a&gt;themes/index.js&lt;/p&gt;

&lt;p&gt;Here we define our two colours that will be used as themes, red and blue. I have given each colour a name because it makes the toggle logic easier to follow.&lt;/p&gt;

&lt;h4&gt;
  
  
  reducers
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IMsU1Ga3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/568/1%2AzWLmD-KERRnJxFnx842XNA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IMsU1Ga3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/568/1%2AzWLmD-KERRnJxFnx842XNA.png" alt=""&gt;&lt;/a&gt;reducers/Theme.js&lt;/p&gt;

&lt;p&gt;A reducer is a pure function which takes some state and returns a new state. In this example it gets passed the current theme colour and swaps it to the new theme colour.&lt;/p&gt;

&lt;p&gt;In this example if the action is TOGGLE_THEME, we get the colour name from the payload and using a switch statement we swap the colours over. So if the current colour is red we update the state (colorData) to be blue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KEU-SOgm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/476/1%2AOtWZtS43CUVQI1u9WqGfaA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KEU-SOgm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/476/1%2AOtWZtS43CUVQI1u9WqGfaA.png" alt=""&gt;&lt;/a&gt;reducers/index.js&lt;/p&gt;

&lt;p&gt;Here we combine all of our reducers, in this example, we are only using the one reducer but if we had multiple reducers, The combineReducers function to would be necessary to combine them together. In this example we can simply add new reducers to the function as and when we need them.&lt;/p&gt;

&lt;h4&gt;
  
  
  actions
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w7FBX0Zm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/508/1%2Aj6yKSOpCkwtP9rSHDsj7Cg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w7FBX0Zm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/508/1%2Aj6yKSOpCkwtP9rSHDsj7Cg.png" alt=""&gt;&lt;/a&gt;actions/actionTypes.js&lt;/p&gt;

&lt;p&gt;This file defines all the actions we can dispatch to our store. In this example, we only need one action to toggle our theme.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iXlCdvaH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/528/1%2AL8mg3PYJFkNWJ0uRfkJQZQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iXlCdvaH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/528/1%2AL8mg3PYJFkNWJ0uRfkJQZQ.png" alt=""&gt;&lt;/a&gt;actions/index.js&lt;/p&gt;

&lt;p&gt;In this file, we define our actions, so here we have a single action toggleTheme, which takes a theme as input and passes it as our payload, hence to access the name of the colour we use action.payload.name in our reducer.&lt;/p&gt;

&lt;h4&gt;
  
  
  store
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Dc35bxTA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/558/1%2AiPrFTnkPVpJEO5VZ-JZVhA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Dc35bxTA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/558/1%2AiPrFTnkPVpJEO5VZ-JZVhA.png" alt=""&gt;&lt;/a&gt;store/index.js&lt;/p&gt;

&lt;p&gt;The Redux store is used to store the current state for our app, we have to link our store with our reducers we can do this using the createStore function and import the reducers from reducers/index.js.&lt;/p&gt;

&lt;h4&gt;
  
  
  App.js
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KNDKkQ9c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/773/1%2A289XAR5oxLjw-xejBU6nsg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KNDKkQ9c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/773/1%2A289XAR5oxLjw-xejBU6nsg.png" alt=""&gt;&lt;/a&gt;App.js&lt;/p&gt;

&lt;p&gt;This acts as the main file for our app, to use Redux with our app we must wrap around Provider tags and set the store props to our store/index.js file. The  contains the logic for our two screens and the tab navigator.&lt;/p&gt;

&lt;h4&gt;
  
  
  components
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IprefMNm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/732/1%2ADS8nZV5MIeeqC6-hCMC-zA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IprefMNm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/732/1%2ADS8nZV5MIeeqC6-hCMC-zA.png" alt=""&gt;&lt;/a&gt;components/CustomTabNavigator.js&lt;/p&gt;

&lt;p&gt;Here is where we use react navigation to create our tab navigator. We define two screens called A and B, each screen has a different tab colour, red for A and blue for B. The main part of this file is the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tabBarOnPress: ({ defaultHandler }) =&amp;gt; {
 store.dispatch(toggleTheme(COLORS.red));
 defaultHandler();
},
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;On tab change (from A -&amp;gt; B), we detect the tab press and dispatch the toggleTheme action to the Redux store. So when we change tabs from A -&amp;gt; B the colour in the store will change from Red -&amp;gt; Blue and vice versa for B -&amp;gt; A.&lt;/p&gt;

&lt;p&gt;One other thing to note is the tab colour is set using the following function. The colour is passed by the tabBarOptions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const commonTabOptions = color =&amp;gt; ({
 activeTintColor: 'white',
 pressColor: '#fff',
 inactiveTintColor: '#ddd',
 style: {
 backgroundColor: color,
 },
});

tabBarOptions: commonTabOptions(COLORS.red.hexCode)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--a8mxllC---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/814/1%2A4IVlvFV68MYHxgnjsXQwzg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--a8mxllC---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/814/1%2A4IVlvFV68MYHxgnjsXQwzg.png" alt=""&gt;&lt;/a&gt;components/ToggleTheme.js&lt;/p&gt;

&lt;p&gt;This file does most of the heavy lifting for this app, this is where most of the logic sits. So, first of all, we have a button, which calls toggleTheme on a press of the and passes the current colour (state) as an argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Button
 title="Toggle Color"
 color={this.props.color.hexCode}
 onPress={() =&amp;gt; this.props.toggleTheme(this.props.color)}
/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The colour of the button is also using the current (state) colour, so in this case, it will start off as red, this.props.color. We can access the current state using this.props.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mapStateToProps = state =&amp;gt; ({
 color: state.Theme.colorData,
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This function is used to map the Redux store to the state of this component. We map state.Theme.colorData, where Theme is the name of our reducer and colorData is the item name. In this example, we've mapped the current colorData to color props in this component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mapDispatchToProps = dispatch =&amp;gt; ({
 toggleTheme: color =&amp;gt; dispatch(toggleTheme(color)),
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This function maps a function to an action (and dispatches it), in this example when the toggleTheme function is called in the component it will dispatch the toggleTheme action to redux. So when the button is pressed it will dispatch an action to toggle the theme.&lt;/p&gt;

&lt;h4&gt;
  
  
  PageA
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Hl-CeGOS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/600/1%2A9zeTitnYwaevyPrT3sBRoQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Hl-CeGOS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/600/1%2A9zeTitnYwaevyPrT3sBRoQ.png" alt=""&gt;&lt;/a&gt;screens/PageA.js&lt;/p&gt;

&lt;p&gt;This is an example of one of our screens, it’s very simple just the page name and  component.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n4fcmW23--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/406/1%2AzL4nT7aYvzY36WCSf_Hd0A.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n4fcmW23--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/406/1%2AzL4nT7aYvzY36WCSf_Hd0A.gif" alt=""&gt;&lt;/a&gt;The app running on an Android emulator.&lt;/p&gt;

&lt;p&gt;Here is an example of the app running, as you can see there are two ways we can toggle the theme. We could use the button click, or change tabs.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Extra Task&lt;/em&gt;: If you want some practice, currently this won’t detect a swipe action to change tabs look at how you can do this. An example of it working can be found &lt;a href="https://gitlab.com/hmajid2301/stegappasaurus/blob/development/src/screens/Decoding.js"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Appendix
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/hmajid2301/medium/tree/master/10.%20Redux%20with%20React%20Navigation"&gt;Example source code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactnavigation.org/"&gt;React Navigation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://css-tricks.com/learning-react-redux/"&gt;Redux Animation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://slides.com/jenyaterpil/redux-from-twitter-hype-to-production#"&gt;Redux Diagram&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://redux.js.org/"&gt;Redux&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;GIFs created with &lt;a href="https://www.screentogif.com/"&gt;screentogif&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://carbon.now.sh"&gt;Code images made with carbon&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>redux</category>
      <category>reactnative</category>
      <category>reactnavigation</category>
      <category>expo</category>
    </item>
  </channel>
</rss>
