<?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: gortron</title>
    <description>The latest articles on DEV Community by gortron (@gortron).</description>
    <link>https://dev.to/gortron</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%2F283244%2F44cc8d8f-a091-4d40-8080-7f664459c492.jpeg</url>
      <title>DEV Community: gortron</title>
      <link>https://dev.to/gortron</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gortron"/>
    <language>en</language>
    <item>
      <title>Animating text over images in JS</title>
      <dc:creator>gortron</dc:creator>
      <pubDate>Sun, 21 Jun 2020 19:20:30 +0000</pubDate>
      <link>https://dev.to/gortron/animating-text-over-images-in-js-2g4g</link>
      <guid>https://dev.to/gortron/animating-text-over-images-in-js-2g4g</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;I've been building a personal blog site for my writing and photography, and it's been a great excuse to experiment with CSS that I don't always get a chance to use.&lt;/p&gt;

&lt;p&gt;In this post, I'll walk through how to animate text over a background image. In my portfolio, I use it to animate over image previews of posts. It'll end up looking something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wviQ3fPt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p4u71simwlpdpuj3psyo.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wviQ3fPt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p4u71simwlpdpuj3psyo.gif" alt="animation example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Solution
&lt;/h1&gt;

&lt;p&gt;My solution uses React and Emotion, but the principal of how I animate over my image should be viable for any other front-end framework.&lt;/p&gt;

&lt;p&gt;My approach was to create a container for the background image, and then lay a container on top of the background image. The top container is translucent until it is hovered over, and then it and the text it contains become opaque.&lt;/p&gt;

&lt;p&gt;The html structure (here, written in jsx) looks 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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;BackgroundImg&lt;/span&gt; &lt;span class="nx"&gt;alt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;alt_text&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Hover&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;AnimatedTitle&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;title_text&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/AnimatedTitle&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;AnimatedParagraph&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;paragraph_text&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/AnimatedParagraph&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Hover&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/BackgroundImg&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Using the &lt;code&gt;@emotion/styled&lt;/code&gt; library, I made 3 CSS-in-JS styled components to represent these 3 blocks. Code with in-line comments below.&lt;/p&gt;

&lt;p&gt;Note that &lt;code&gt;BackgroundImage&lt;/code&gt; here is a wrapper component from &lt;code&gt;Gatsby.js&lt;/code&gt; that does some nice image-load optimization functions in the background, but for your purposes you could use a &lt;code&gt;div&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Styling and Animation for Desktop&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BackgroundImg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;BackgroundImage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;`
  width: 33vw;
  height: 33vw;
  margin: 2vw 2vw 2vw 2vw;
`&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Hover&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="s2"&gt;`
  height: 100%;
  width: 100%;

  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;

  /* The component is not opaque, but opacity transitions out for 300ms */
  background-color: #efece4;
  opacity: 0;
  transition: opacity 300ms ease-in-out;

  /* When the user hovers over the component, it becomes opaque over 300ms */
  &amp;amp;:hover {
    opacity: 0.95;
    transition: opacity 300ms ease-in-out;
  }

  /* All children (animated text) move to default position when component is hovered over */
  &amp;amp;:hover &amp;gt; * {
    transform: translate3d(0, 0, 0);
  }
`&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;AnimatedTitle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="s2"&gt;`
  /* Transform to 50px down on load, but user doesn't see this because div this text is in is translucent  */
  transform: translate3d(0, 50px, 0);
  transition: transform 300ms ease;
  text-align: center;
  margin-bottom: 10px;
`&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;AnimatedParagraph&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="s2"&gt;`
  transform: translate3d(0, 50px, 0);
  transition: transform 300ms ease;
  text-align: center;
  margin-bottom: 0;
  font-size: 12px;
  font-style: italic;
`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



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

&lt;p&gt;This of course only scratches the surface of the transformations and animations CSS is capable of. Highly recommend the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitions"&gt;MDN spec&lt;/a&gt; to anyone who wants a more thorough overview of CSS transforms and transitions. I hope this post has been helpful for anyone trying to solve a similar problem. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Using Emotion for CSS-in-JavaScript</title>
      <dc:creator>gortron</dc:creator>
      <pubDate>Sun, 14 Jun 2020 17:15:31 +0000</pubDate>
      <link>https://dev.to/gortron/using-emotion-for-css-in-javascript-1gc5</link>
      <guid>https://dev.to/gortron/using-emotion-for-css-in-javascript-1gc5</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;You're already familiar with CSS, and likely with CSS-supersets like &lt;code&gt;sass&lt;/code&gt; and &lt;code&gt;less&lt;/code&gt;, which add much-needed utility and convenience to CSS. But some frontend developers are turning to CSS-in-JS, an increasingly common pattern for styling web applications. &lt;/p&gt;

&lt;p&gt;It's easy to understand why this approach is gaining traction. Imagine you've been tasked to make a button component with React, and you've been given some specs for its styling. You have two options:&lt;/p&gt;

&lt;h2&gt;
  
  
  1) Separate JS and CSS
&lt;/h2&gt;

&lt;p&gt;Write the Button.js React component and give it a class name like &lt;code&gt;&amp;lt;Button className="styled"&amp;gt;&lt;/code&gt;, then write the component-specific css in a separate file.&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;// Button.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&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;react&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;Button&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;button&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;styled&lt;/span&gt;&lt;span class="dl"&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/* sass or less which allows for logic statements */&lt;/span&gt;
&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="nc"&gt;.styled&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;32px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;hotpink&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;24px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;4px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="err"&gt;&amp;amp;:hover&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But, if another engineer needs to debug the component or change styling in the future, they need to work in both the JS and CSS files. This is a negligible issue for a simple component, but a more onerous task for a brand re-work. &lt;/p&gt;

&lt;h2&gt;
  
  
  2) CSS-in-JS
&lt;/h2&gt;

&lt;p&gt;Write the component &lt;em&gt;and&lt;/em&gt; styling logic in one file.&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;// Button.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&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;react&lt;/span&gt;&lt;span class="dl"&gt;'&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;css&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;emotion&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;Button&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;button&lt;/span&gt; &lt;span class="nx"&gt;css&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;css&lt;/span&gt;&lt;span class="s2"&gt;`
    padding: 32px;
    background-color: hotpink;
    font-size: 24px;
    border-radius: 4px;
    &amp;amp;:hover {
      color: white;
    }`&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I haven't touched on other major benefits of CSS-in-JS, such as the ease with which you can pass styling arguments as props to your components, e.g. &lt;code&gt;&amp;lt;Button big pink&amp;gt;&lt;/code&gt;, or making styled components e.g. &lt;code&gt;StyledButton = button.styled{...}&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;As an aside, Emotion is a popular library (see also: styled-components) for CSS-in-JS, and well worth a look. (I borrowed their styling for this blog post!)&lt;/p&gt;

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

&lt;p&gt;These two schools exhibit a philosophical question: is styling something "other" than the object that is styled, or is styling an inextricable property of the thing that is styled? In my opinion, it's the latter. So, to my taste, CSS-in-JS makes more organizational sense to me. (A poor analogy: imagine having to look away from a house to figure out what color it is.)&lt;/p&gt;

&lt;p&gt;Beyond that, choosing between separating JS and CSS and using CSS-in-JS is a tradeoff between strict adherence to separation of concerns, and developer convenience/experience. &lt;/p&gt;

&lt;p&gt;This to me seems like a broader trend of &lt;em&gt;everything&lt;/em&gt;-in-JS. JS seems to continue to evolve into do-anything language. Consider the popularity of Node.js, and the recent announcement of TensorFlow.js.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>An Overview of Server-side Rendering</title>
      <dc:creator>gortron</dc:creator>
      <pubDate>Mon, 08 Jun 2020 05:12:01 +0000</pubDate>
      <link>https://dev.to/gortron/an-overview-of-server-side-rendering-3pad</link>
      <guid>https://dev.to/gortron/an-overview-of-server-side-rendering-3pad</guid>
      <description>&lt;p&gt;Server-side rendering has been around for awhile, but is undergoing a Renaissance as new tools like Gatsby and Next.js gain traction. So what is server-side rendering, and how is different from a client-side rendered web app? What is the advantage to server-side rendering?&lt;/p&gt;

&lt;p&gt;Consider a web application, in which a Node server hosted by AWS serves a React application (compiled into HTML/CSS/JS). When a client-user sends a request to that web app's URL, the web app's server responds with the HTML/CSS/JS needed to load the front end of the web app. The client loads the JS, and if it's a web app of any complexity it will then fetch any resources necessary for the app to run. Consider that, in client-side rendering, a user will typically wait for one request-response cycle, wait for the JS in the response to load, and then wait for &lt;em&gt;another&lt;/em&gt; request-response cycle. &lt;/p&gt;

&lt;p&gt;Much has been written about &lt;a href="https://www.section.io/blog/page-load-time-bounce-rate"&gt;user's reactions to page load times&lt;/a&gt;, but note that milliseconds count.&lt;/p&gt;

&lt;p&gt;Fetching not only slows down the user experience, but presents challenges for SEO. When Google indexes your web app's URL, it doesn't wait for fetching to complete. It indexes the html that is sent back from the web app's server. &lt;/p&gt;

&lt;p&gt;Server-side rendering is an alternative model to client-side rendering. The goal of server-side rendering is to offload as much of the work from the client to the server as possible. You can do this by rendering at run time (Next.js) or rendering at build time (Gatsby).&lt;/p&gt;

&lt;p&gt;For example, by rendering at run time, you can make it the server's responsibility to fetch data for the web application. So when a user sends a request the app's URL, that will trigger the server to fetch data for the app, and once the fetch is resolved it will send all the HTML/CSS/JS the client needs in the response; the client won't need to fetch again. Depending on where your server is and where the storage for the data is, this can make a significant difference.&lt;/p&gt;

&lt;p&gt;By rendering at build time, however, your server is configured to fetch any data the app needs &lt;em&gt;before&lt;/em&gt; a client sends a response, and compose that data into static HTML/CSS/JS. Then, when a client sends a request the web app's URL, the server responds with the static HTML/CSS/JS it's already built. &lt;/p&gt;

&lt;p&gt;Server-side rendering is ideal for web applications that don't involve significant dynamic interaction with the user, where you could ostensibly do one fetch to get all the data you need for the web app to function (blogs, smaller shops).&lt;/p&gt;

&lt;p&gt;I hope this post has help clarify what server-side rendering is, and why it can be a powerful methodology. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>React: making a store from createContext and useReducer</title>
      <dc:creator>gortron</dc:creator>
      <pubDate>Mon, 01 Jun 2020 01:54:08 +0000</pubDate>
      <link>https://dev.to/gortron/react-making-a-store-from-createcontext-and-usereducer-35bj</link>
      <guid>https://dev.to/gortron/react-making-a-store-from-createcontext-and-usereducer-35bj</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;For all but the simplest applications or static sites, React developers will use state to manage data. Determining the optimal location for state is part of the job of a React developer. Is this data relevant only to this component? This container? Or the entire application? Tools like Redux are popular solutions for managing 'global' state for JS frameworks like React.&lt;/p&gt;

&lt;p&gt;There are advantages to using Redux (including its documentation, developer tools, and user base), but in this post I'll share an approach that uses existing React tools to make a state container. Specifically, this approach uses createContext and useReducer to make a StateProvider. &lt;/p&gt;

&lt;h1&gt;
  
  
  Approach
&lt;/h1&gt;

&lt;p&gt;First, we'll write our reducer and store in a &lt;code&gt;store.js&lt;/code&gt;. Note that the switch for your actions will look different from mine, but the pattern should look similar.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useReducer&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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;mobile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;projects&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialState&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;Provider&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;store&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;StateProvider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&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;dispatch&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useReducer&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;action&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;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;addToCart&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="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="na"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Unhandled action type: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&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="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;initialState&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;Provider&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&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;dispatch&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;children&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;/Provider&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;StateProvider&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next we'll wrap our application with our StateProvider, to subscribe our Store to that App's view. In your &lt;code&gt;index.js&lt;/code&gt;, your code could look 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&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;lt;&lt;/span&gt;&lt;span class="nx"&gt;StateProvider&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/StateProvider&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Lastly, we'll need a way to dispatch actions from the view to the reducer in the store. Your App container might look 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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useContext&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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&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;dispatch&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;store&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;itemAdded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;item&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;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;addToCart&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;item&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;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;I like this pattern for managing global state in React applications, especially simple ones, principally because it reduces the amount of syntactic sugar from &lt;code&gt;mapStateToProps&lt;/code&gt;, &lt;code&gt;mapDispatchToDrops&lt;/code&gt;, and &lt;code&gt;export default connect(...)&lt;/code&gt;. I hope this post was helpful to you!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>React custom hooks: a clean approach to fetch</title>
      <dc:creator>gortron</dc:creator>
      <pubDate>Mon, 25 May 2020 00:51:38 +0000</pubDate>
      <link>https://dev.to/gortron/react-custom-hooks-a-clean-approach-to-fetch-5efi</link>
      <guid>https://dev.to/gortron/react-custom-hooks-a-clean-approach-to-fetch-5efi</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Custom hooks are not a React feature, per se; but implementing custom hooks follows the same design pattern of React's common hooks like &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useEffect&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;As the React documentation specifies, a &lt;a href="https://reactjs.org/docs/hooks-custom.html"&gt;custom hook&lt;/a&gt; is _a JavaScript function whose name starts with ”use” and that may call other Hooks. &lt;/p&gt;

&lt;p&gt;If you're familiar with React, you may be familiar with the following pattern, where a component &lt;code&gt;fetch&lt;/code&gt;es a some data from a backend service, e.g.&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;const&lt;/span&gt; &lt;span class="nx"&gt;myComponent&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;posts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setPosts&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getPosts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&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="s2"&gt;my_backend_url&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;setPosts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;getPosts&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ul&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;posts&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;post&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;PostPreview&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;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slug&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ul&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;This common pattern can be abstracted into a custom hook, which cleans up the component code.&lt;/p&gt;

&lt;h1&gt;
  
  
  Code
&lt;/h1&gt;

&lt;p&gt;Consider the following approach to getting/setting posts. This code would exist in something like &lt;code&gt;./src/hooks/use-posts.js&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;usePosts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&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="s2"&gt;my_backend_url&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now our component could be re-written to be:&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;const&lt;/span&gt; &lt;span class="nx"&gt;myComponent&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="nx"&gt;posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;usePosts&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;ul&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;posts&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;post&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;PostPreview&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;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slug&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ul&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;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;There are many considerations when you're refactoring functional code to improve it. Among those considerations are: &lt;em&gt;is this easier to follow?&lt;/em&gt;, &lt;em&gt;does this separate concerns?&lt;/em&gt;, and &lt;em&gt;does this follow the language's design patterns?&lt;/em&gt; The custom hook pattern for React is affirmative to all of those considerations. I hope you find this post useful. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Environment Variables with pm2: The Right Way</title>
      <dc:creator>gortron</dc:creator>
      <pubDate>Mon, 18 May 2020 01:35:29 +0000</pubDate>
      <link>https://dev.to/gortron/environment-variables-with-pm2-the-right-way-42ij</link>
      <guid>https://dev.to/gortron/environment-variables-with-pm2-the-right-way-42ij</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;This post is meant to support anyone who is setting up environment variables for a Node.js daemon that they will manage with pm2. Specifically, this post will show you how to handle &lt;code&gt;NODE_ENV&lt;/code&gt; for development and production settings for pm2. &lt;/p&gt;

&lt;p&gt;There are multiple ways to handle environment variables for a pm2 daemon.&lt;br&gt;
1) Some deployment tools, like Heroku or Netlify, let you set environment variables.&lt;br&gt;
2) You can set them in your CLI. &lt;br&gt;
3) You can use a pm2 &lt;code&gt;ecosystem.yml&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;There are trade-offs between all of these approaches. The first option forces you to navigate a deployment tool's interface to figure out any bugs relating to environment variables. &lt;/p&gt;

&lt;p&gt;The second approach makes it easier to debug. You might implement it with something like this, in the &lt;code&gt;scripts&lt;/code&gt; section of your &lt;code&gt;package.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"NODE_ENV=production &amp;amp;&amp;amp; pm2 start app.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"NODE_ENV=development &amp;amp;&amp;amp; pm2 start app.js"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;While this approach is viable, to my tastes it feels "hacky". It does the immediate job of setting one environment variable, but what if you wanted to set more environment variables? Maybe you would write a &lt;code&gt;set_env_var.sh&lt;/code&gt; script and invoke it in your &lt;code&gt;package.json&lt;/code&gt; script, but then you would have to go to that shell script to debug any issues.&lt;/p&gt;

&lt;p&gt;pm2 provides a preferred way to handle environment variables: an ecosystem file.&lt;/p&gt;

&lt;h1&gt;
  
  
  Solution
&lt;/h1&gt;

&lt;p&gt;Compare this &lt;code&gt;ecosystem.yml&lt;/code&gt; file below with the &lt;code&gt;package.json&lt;/code&gt; snippet above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;apps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;./app.js&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;app"&lt;/span&gt;
    &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;NODE_ENV&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;development&lt;/span&gt;
    &lt;span class="na"&gt;env_production&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;NODE_ENV&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;production&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It would be far easier to add environment variables, here. And, better still, you're clearly communicating your intent to your collaborators: this file is where we handle environment variables for our pm2 daemon. &lt;/p&gt;

&lt;p&gt;You complete the solution by adding something like the following to your &lt;code&gt;package.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"pm2 restart ecosystem.yml --env production"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"pm2 restart ecosystem.yml --env development"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Similar to the first &lt;code&gt;package.json&lt;/code&gt; scripts, these scripts will start (or restart an existing) pm2 process, and update the environment for that process to include the variables you declared in your &lt;code&gt;ecosystem.yml&lt;/code&gt; file. &lt;/p&gt;

&lt;p&gt;This is a useful pattern, when setting up dev ops for a pm2 micro service. Consider the following snippet from &lt;code&gt;app.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;TOKEN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NODE_ENV&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;production&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PROD_TOKEN&lt;/span&gt;
      &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DEV_TOKEN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you call &lt;code&gt;yarn start&lt;/code&gt;, you will re/start a pm2 process with a token designated for production; if you call &lt;code&gt;yarn dev&lt;/code&gt;, you will re/start a pm2 process with a token designated for development. &lt;/p&gt;

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

&lt;p&gt;I hope this guide was useful for anyone getting started with pm2, and thinking through how to implement environment variables for their daemon. An example application that uses this pattern can be found on my &lt;a href="https://github.com/gortron/cupbot"&gt;GitHub&lt;/a&gt;. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Deploying to AWS with Travis via ssh</title>
      <dc:creator>gortron</dc:creator>
      <pubDate>Sun, 10 May 2020 19:59:16 +0000</pubDate>
      <link>https://dev.to/gortron/deploying-to-aws-with-travis-via-ssh-315a</link>
      <guid>https://dev.to/gortron/deploying-to-aws-with-travis-via-ssh-315a</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The goal of this post is to provide a sample solution for setting up CICD from a GitHub repo to an AWS ec2 instance using Travis—I hope someone debugging this problem finds this useful. &lt;/p&gt;

&lt;p&gt;I recently worked on a small personal project for a Discord bot, which I deployed on an AWS ec2 instance. My workflow for deploying my GitHub repo to my ec2 instance wasn't that onerous for a personal project—ssh to my ec2 instance, cd to repo, git pull, and restart pm2. &lt;/p&gt;

&lt;p&gt;But it had a problem: any time a contributor made a pull request, &lt;em&gt;I&lt;/em&gt; had to deploy the updated master using the workflow above. That approach works fine for one person, but makes less sense for a group of contributors. I needed a deployment methodology that would essentially recreate the steps above, but would do so anytime a pull request was merged to master. That sounded to me like I needed lightweight CICD tooling.&lt;/p&gt;

&lt;p&gt;There are many tools to help set up CICD deployment to an AWS ec2 instance (Jenkins, Travis, CircleCI among them). I to setup a custom deployment on Travis, mainly because Travis is hosted and wouldn't require me to set up another ec2 and s3 instances. &lt;/p&gt;

&lt;p&gt;Below, I'll describe how I executed this problem in brief. My solution is an adaptation from &lt;a href="https://oncletom.io/2016/travis-ssh-deploy/"&gt;Thomas Parisot&lt;/a&gt;, so great thanks to him. He writes in greater depth about why his solution solves for security concerns with ssh'ing to ec2 from Travis, and I strongly consider anyone solving this problem to visit his blog as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Execution
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1) Set up an ssh key for Travis
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;If you don't already have the Travis CLI tooling, use &lt;code&gt;$ brew install travis&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Make sure you have a &lt;code&gt;.travis.yml&lt;/code&gt; file in the root directory of your project repo. &lt;/li&gt;
&lt;li&gt;While in your root directory of your project repo, execute the following:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ssh-keygen &lt;span class="nt"&gt;-t&lt;/span&gt; rsa &lt;span class="nt"&gt;-b&lt;/span&gt; 4096 &lt;span class="nt"&gt;-C&lt;/span&gt; &lt;span class="s1"&gt;'build@travis-ci.org'&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; ./deploy_rsa
&lt;span class="nv"&gt;$ &lt;/span&gt;travis encrypt-file deploy_rsa &lt;span class="nt"&gt;--add&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;ssh-copy-id &lt;span class="nt"&gt;-i&lt;/span&gt; deploy_rsa.pub ec2-user@&amp;lt;your-ec2-instance&amp;gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; deploy_rsa deploy_rsa.pub
&lt;span class="nv"&gt;$ &lt;/span&gt;git add deploy_rsa.enc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  2) Set up your Travis custom deployment
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The commands above will add most of what you need to your &lt;code&gt;.travis.yml&lt;/code&gt; file. We will make changes, though, so that the encrypted &lt;code&gt;deploy_rsa&lt;/code&gt; key gets added to &lt;code&gt;./tmp/&lt;/code&gt; so that it doesn't accidentally get deployed:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;before_deploy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;openssl aes-256-cbc -K $encrypted_&amp;lt;...&amp;gt;_key -iv $encrypted_&amp;lt;...&amp;gt;_iv -in deploy_rsa.enc -out /tmp/deploy_rsa -d&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;eval "$(ssh-agent -s)"&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;chmod 600 /tmp/deploy_rsa&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;ssh-add /tmp/deploy_rsa&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now that Travis is configures ssh in its &lt;code&gt;before_deploy&lt;/code&gt; lifecycle, we will tell it what to do in its &lt;code&gt;deploy&lt;/code&gt; lifecycle. We want it to ssh to our ec2 instance and execute a deployment script:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;deploy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;provider&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;script&lt;/span&gt;
  &lt;span class="na"&gt;skip_cleanup&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
  &lt;span class="na"&gt;script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ssh ec2-user@&amp;lt;your-ec2-instance&amp;gt; 'bash ./deploy.sh'&lt;/span&gt;
  &lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;branch&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;master&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  3) Write a deploy script for Travis to execute
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The final step is to add a deployment shell script in the root directory of your ec2 instance. After a successful build, travis will ssh to your ec2 instance and call &lt;code&gt;bash ./deploy.sh&lt;/code&gt;. So the last step is to add a &lt;code&gt;deploy.sh&lt;/code&gt; script in the root directory of your ec2 instance. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Your deployment script will look unique from mine, but I'll provide mine below as guidepost. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;As stated in my intro, I want mine to &lt;code&gt;cd&lt;/code&gt; to my previously deployed repo, &lt;code&gt;git pull&lt;/code&gt; to get the latest, &lt;code&gt;yarn&lt;/code&gt; to add any missing dependencies, and &lt;code&gt;pm2 restart all&lt;/code&gt; to re-start my Node server.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Changing directory to ~/cupbot"&lt;/span&gt;
&lt;span class="nb"&gt;cd &lt;/span&gt;cupbot
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Pulling from git"&lt;/span&gt;
git pull
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Yarn'ing"&lt;/span&gt;
yarn
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Restarting pm2 instances"&lt;/span&gt;
pm2 restart all
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope this was useful to anyone looking to deploy from a GitHub repo to an AWS ec2 instance. If you get stuck or need help, please don't hesitate to comment or reach out. The discord bot repo I deployed is available &lt;a href="https://github.com/gortron/cupbot"&gt;here&lt;/a&gt; as well.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>AWS EC2 Deployment Guide</title>
      <dc:creator>gortron</dc:creator>
      <pubDate>Sun, 03 May 2020 18:13:02 +0000</pubDate>
      <link>https://dev.to/gortron/aws-ec2-deployment-guide-ego</link>
      <guid>https://dev.to/gortron/aws-ec2-deployment-guide-ego</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I thought I would share my personal deployment checklist for AWS EC2. I remember the first time I deployed, I felt like there were a lot of &lt;em&gt;gotchas&lt;/em&gt;. I've put together this checklist, which helps me keep track of the different steps it takes to create a new EC2 instance you can ssh into, configuring its environment, and deploying on it. &lt;/p&gt;

&lt;p&gt;For first-timers deploying to AWS, I hope you find this helpful. &lt;/p&gt;

&lt;h2&gt;
  
  
  Steps
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Create a new EC2 instance.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Defaults / free tier are fine, except for Security Groups and Key Pair.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The included port 22 is for SSH.&lt;/li&gt;
&lt;li&gt;Open Port 80. Can use your laptop IP address or leave it as 0.0.0.0 which leaves it exposed to any IP.&lt;/li&gt;
&lt;li&gt;Add other ports as necessary. If your server "listens" on 3000, then add 3000. But you may want to consider using &lt;code&gt;nginx&lt;/code&gt; to only listen on port 80 but "route" relevant requests to 3000, 8000, etc.&lt;/li&gt;
&lt;li&gt;Make a new Key Pair (.pem) and download it. Move it to &lt;code&gt;~/.ssh&lt;/code&gt; . You can do that with &lt;code&gt;mv &amp;lt;drag file to terminal to get current path&amp;gt; ~/.ssh&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;strong&gt;SSH into your instance.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You need to configure the read/write authorizations on your ssh key. Then you need to add that key to the destination site and finally add the key to your keychain. Keychain is going to run through all possible ssh keys whenever you try to connect to a site.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;chmod 400 ~/.ssh/&amp;lt;your key&amp;gt;&lt;/code&gt;. If you need to debug this step, use &lt;code&gt;ls -lh &amp;lt;filepath&amp;gt;&lt;/code&gt; , which will tell you permissions. should just be &lt;code&gt;r&lt;/code&gt; at the front.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ssh -i identityFile ec2-user@&amp;lt;your host ip address&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ssh-add ~/.ssh/&amp;lt;your key&amp;gt;.pem&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;eval "$(ssh-agent -s)"&lt;/code&gt; - starts the ssh-agent&lt;/li&gt;
&lt;li&gt;Finally, &lt;code&gt;ssh ec2-user@&amp;lt;your host ip address&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Install necessary software.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You'll need git at a minimum. Rest is app-specific. This example is for nvm/node/yarn.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;sudo yum install git&lt;/code&gt;. &lt;code&gt;which git&lt;/code&gt; to make sure it's working.&lt;/li&gt;
&lt;li&gt;Install nvm: &lt;code&gt;curl -o- [https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh](https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh) | bash&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Install node: &lt;code&gt;nvm install node&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Install your version: &lt;code&gt;nvm install &amp;lt;your local version #&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Install yarn: &lt;code&gt;npm install -g yarn@&amp;lt;your version #&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Configure git.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Your local is already set up to ssh nicely with git. New EC2 instance isn't (yet).&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;ssh-keygen -t rsa -b 4096 -C "your_email@example.com"&lt;/code&gt; 
Use an empty passphrase when prompted.&lt;/li&gt;
&lt;li&gt;Add the key to your ssh-agent with &lt;code&gt;eval "$(ssh-agent -s)"&lt;/code&gt;  and &lt;code&gt;ssh-add ~/.ssh/id_rsa&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If you have problems with this, see step 2 above.&lt;/li&gt;
&lt;li&gt;You then need to copy the content of this newly generate &lt;code&gt;id_rsa&lt;/code&gt; file into your github security. use &lt;code&gt;pbcopy &amp;lt; ~/.ssh/id_rsa.pub&lt;/code&gt; or &lt;code&gt;vim ~/.ssh/id_rsa.pub&lt;/code&gt; to do this. &lt;/li&gt;
&lt;li&gt;If you need to debug, git has a guide &lt;a href="https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Set up the directory.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Finally, clone the repo.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;git clone &amp;lt;repo .git&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;yarn&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Any files not pushed to github, like &lt;code&gt;.env&lt;/code&gt;, need to be copied over. use &lt;code&gt;scp &amp;lt;drag .env file to terminal&amp;gt; ec2-user@&amp;lt;your host ip address&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;if you need this to run while you're not in ec2 terminal, use &lt;code&gt;pm2 start &amp;lt;app.js or runfilename.js&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;There are many ways to configure an EC2 instance and set it up, but I hope this barebones guide is useful to beginners debugging their deployment. I'll be writing a future post that demonstrates ways to set up testing, logging, and CICD on EC2.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>A Hitchhiker’s Guide to RegEx</title>
      <dc:creator>gortron</dc:creator>
      <pubDate>Sun, 26 Apr 2020 17:49:17 +0000</pubDate>
      <link>https://dev.to/gortron/a-hitchhiker-s-guide-to-regex-pn8</link>
      <guid>https://dev.to/gortron/a-hitchhiker-s-guide-to-regex-pn8</guid>
      <description>&lt;h2&gt;
  
  
  tl;dr
&lt;/h2&gt;

&lt;p&gt;At some level, most files and programs you interact with are strings. RegEx helps you find very specific string patterns. With a few simple regex building blocks, you can make remarkably complex search queries. &lt;em&gt;Read on for a deeper explanation and a beginner’s guide on how to get started with regex.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;So, you’re learning to be a &lt;a href="http://www.paulgraham.com/hp.html"&gt;hacker&lt;/a&gt;, huh? I bet you’ve run into some variant of this problem, then: go find something specific in a large set of data. I’m being deliberately vague, here. Information, and ways of searching it, comes in many formats. By now, you’ve probably got a handle on hashes, arrays, strings, and the like. And you’re getting comfortable with a few nifty enumerators (What’s your favorite? I like Ruby’s &lt;code&gt;.select&lt;/code&gt; method. More on that later.).&lt;/p&gt;

&lt;p&gt;But you’re here because someone has asked you to find a whacky, difficult-to-grasp data element and your normal tools aren’t cutting it. You Google’d the problem. Some (generous, saintly)random on StackOverflow solved a similar problem for someone else a few years ago. They said something about ‘regex’, and then a shared a line of code with an alarming number of /’s and \’s. And you might be thinking, okay, WTF is this? RegEx doesn’t make a great first impression, but trust me, you want to sit down on the couch next to RegEx at your friend’s party. It has .~&lt;em&gt;Special Powers&lt;/em&gt;~.&lt;/p&gt;

&lt;p&gt;RegEx is short for Regular Expression. A Regular Expression is a special string used for pattern-matching and searching. One of the best features of regex is its portability: it has a syntax you learn one, but use in many contexts. Most languages have an enumerator that helps you search for a pattern (like Ruby’s .find), but what makes regex really powerful is the level of control it gives you over that search pattern.&lt;/p&gt;

&lt;p&gt;A regular expression is comprised of characters. You know, like ASCII characters. Specifically, a regex is comprised of two types of characters: literal, and meta. Literal characters are ‘simple’ characters; letters, numbers, and punctuation. Metacharacters, like &lt;code&gt;{ } [ ] ( ) ^ $ . | * + ? \&lt;/code&gt;, are ‘complex’ characters. I’m oversimplifying the point, but essentially metacharacters are the characters that have special meaning in the context of regex. Metacharacters convey richer information than literal characters. &lt;/p&gt;

&lt;p&gt;Remember how I said regex was powerful? With just characters and metacharacters, you can search for any ASCII string, specifically, or strings with a more general search parameters.&lt;/p&gt;

&lt;p&gt;Let’s look at a simple example. We’ve got a filename, and we’re trying to parse information from that filename. The filename is &lt;code&gt;Talking Heads - Speaking in Tongues - This Must Be the Place (Naïve Melody).mp3&lt;/code&gt; How can we grab the Artist, Album, Song Name, and Filetype? With Ruby, maybe we could try to .split around the “-” characters, but that won’t help us out with the Filetype, where we want to split based on a “.”. Take a look at this regexp cookbook and see if any of these ingredients might help us:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;. is a wildcard. It represents *any* ASCII character.
\ let’s us search for metacharacters, such as .[{(\|$^
\d is for *any* number 0–9
\w is for *any* whitespace
[ ] let’s us match specific characters in a given slot
{ } let’s us catch repetition
* represents 0 or more of the characters after a character
+ represents 1 or more of the characters after a character
? we can use this to make character optional
( | ) let’s us search based on multiple parameters
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What looks helpful? It looks like we’re dealing with hyphens, whitespace, AND .’s. Let’s chain a few of these together into one regex.&lt;br&gt;
Let’s say &lt;code&gt;string = Talking Heads - Speaking in Tongues - This Must Be the Place (Naïve Melody).mp3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then &lt;code&gt;string.split(/\w - |\./)&lt;/code&gt; returns:&lt;br&gt;
&lt;code&gt;[“Talking Head”, “Speaking in Tongue”, “This Must Be the Place (Naïve Melody)”, “mp3”]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The easiest way to learn is to do. Use your preferred interactive coding environment to experiment with regex. Or, see the Further Reading section below for other resources for learning this powerful tool.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further Reading:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://regexone.com/"&gt;RegExOne&lt;/a&gt; — Interactive exercises that teach the fundamentals of regex. Highly recommended if you want to go from zero to hero.&lt;br&gt;
&lt;a href="https://en.wikipedia.org/wiki/Regular_expression"&gt;RegEx Wiki&lt;/a&gt; — history and reasoning behind regex.&lt;br&gt;
Your preferred language’s documentation on regex! (&lt;a href="https://ruby-doc.org/core-2.5.1/Regexp.html"&gt;Ruby&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions"&gt;JS&lt;/a&gt;)&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Making Your First Twitch Bot</title>
      <dc:creator>gortron</dc:creator>
      <pubDate>Sun, 19 Apr 2020 22:19:00 +0000</pubDate>
      <link>https://dev.to/gortron/making-your-first-twitch-bot-1l3m</link>
      <guid>https://dev.to/gortron/making-your-first-twitch-bot-1l3m</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;In this post, I'll walk through implementing a Twitch bot. Specifically, I'll be going through initial configuration to connect to Twitch with Node and the &lt;code&gt;tmi.js&lt;/code&gt; package, as I feel the documentation available makes this less straightforward than it should be. &lt;/p&gt;

&lt;p&gt;I'll be referencing code from &lt;a href="https://github.com/gortron/twitch-bot"&gt;this repo&lt;/a&gt;, which I put together for this post.&lt;/p&gt;

&lt;h1&gt;
  
  
  Implementation
&lt;/h1&gt;

&lt;p&gt;Start with the instructions and sample code (or pull from my GitHub repo, linked above) that Twitch provides &lt;a href="https://dev.twitch.tv/docs/irc"&gt;here&lt;/a&gt;. Once you've created an account for your bot, &lt;a href="https://twitchapps.com/tmi/"&gt;registered it&lt;/a&gt; for the &lt;code&gt;tmi.js&lt;/code&gt; packages' OAuth, and have a local clone of my/or GitHub's repo, then you're ready to set up your &lt;code&gt;.env&lt;/code&gt; file and &lt;code&gt;bot.js&lt;/code&gt; script to connect to Twitch.&lt;/p&gt;

&lt;p&gt;The code Twitch provides in their getting started guide asks you to put your OAuth credentials and other environment variables directly into the &lt;code&gt;bot.js&lt;/code&gt; script. This is an anti-pattern, and makes it easy to expose credentials you want to keep secret. &lt;/p&gt;

&lt;p&gt;Instead, we'll use the &lt;code&gt;dotenv.js&lt;/code&gt; package to manage environment variables. &lt;br&gt;
1) Add the dotenv package with &lt;code&gt;yarn add dotenv&lt;/code&gt;&lt;br&gt;
2) Add a &lt;code&gt;.env&lt;/code&gt; file to your project's root directory&lt;br&gt;
3) Add the following code to the &lt;code&gt;.env&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BOT_USERNAME=&amp;lt;YOUR BOT'S USERNAME&amp;gt;
OAUTH_TOKEN=&amp;lt;YOUR BOT'S TMI OAUTH TOKEN&amp;gt;
CHANNEL_NAME=&amp;lt;TARGET CHANNEL'S NAME&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Example syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BOT_USERNAME=mytwitchbot
OAUTH_TOKEN=oauth:fsjgslgd4jkdfgsds5sjskl
CHANNEL_NAME=mytwitchusername
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;4) Update code in &lt;code&gt;bot.js&lt;/code&gt; to reference the environment variables:&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;const&lt;/span&gt; &lt;span class="nx"&gt;opts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;identity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;BOT_USERNAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;OAUTH_TOKEN&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;channels&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;CHANNEL_NAME&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;opts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;channels&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Your set up is complete!  To run, call &lt;code&gt;node bot.js&lt;/code&gt; in your shell. &lt;/p&gt;

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

&lt;p&gt;In this post, you reviewed how to configure your environment variables to connect to Twitch. I hope it was helpful. Feel free to reach out to me if you have trouble connecting your own bot to Twitch. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>A Hitchhiker's Guide to MVC</title>
      <dc:creator>gortron</dc:creator>
      <pubDate>Mon, 13 Apr 2020 17:44:52 +0000</pubDate>
      <link>https://dev.to/gortron/a-hitchhiker-s-guide-to-mvc-1bm2</link>
      <guid>https://dev.to/gortron/a-hitchhiker-s-guide-to-mvc-1bm2</guid>
      <description>&lt;h2&gt;
  
  
  Background.
&lt;/h2&gt;

&lt;p&gt;MVC, or model-view-controller, is a prevalent software design pattern. It was pioneered by &lt;a href="https://en.wikipedia.org/wiki/Trygve_Reenskaug" rel="noopener noreferrer"&gt;Trygve Reenskaug&lt;/a&gt;, and &lt;a href="http://heim.ifi.uio.no/~trygver/2007/MVC_Originals.pdf" rel="noopener noreferrer"&gt;introduced in 1979&lt;/a&gt; after a trip to the Xerox Palo Alto Research Center. (Never heard of the &lt;a href="https://www.mac-history.net/computer-history/2012-03-22/apple-and-xerox-parc" rel="noopener noreferrer"&gt;Xerox PARC&lt;/a&gt;? You can learn more about its amazing story &lt;a href="https://www.youtube.com/watch?v=2u70CgBr-OI" rel="noopener noreferrer"&gt;here&lt;/a&gt;.) Why is MVC, a pattern which pre-dates the internet, still taught as an axiom of software design for web apps 40 years later? To answer that question, we first need to talk about &lt;em&gt;clean code.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Any developer will tell you that in many cases getting the code to run is a lot easier than making the code clean. While everyone has their own taste as to what clean code is, generally clean code does a good job of being organized and easy-to-read. Like many system, code demonstrates entropy; the code will trend towards an inherent state of disorder. It takes &lt;em&gt;hard work&lt;/em&gt; to keep code ordered and clean. MVC remains popular because it is indispensable in this effort. So what is it?&lt;/p&gt;




&lt;h2&gt;
  
  
  Concept.
&lt;/h2&gt;

&lt;p&gt;MVC stands for model-view-controller, and it is a framework for thinking about how human users can interact with programmed models (be they abstract or tangible). It's particularly useful in designing user-interfaces, including web apps. In a system designed around MVC, all responsibilities are split between the view, the controller, and the model. Let’s talk about the domain of a restaurant, and explore what these mean through analogy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fa%2Fa0%2FMVC-Process.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fa%2Fa0%2FMVC-Process.svg" title="MVC" alt="MVC"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  Fig. 1: A simplified view of the MVC framework.
&lt;/h6&gt;




&lt;h2&gt;
  
  
  Example.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;View&lt;/em&gt;&lt;/strong&gt; - Customers show up to a restaurant and sit down at a table, where they will &lt;em&gt;view&lt;/em&gt; possible meal options on a menu and eventually their resulting meal. In most restaurants, to get a meal, you don’t cook it your self at the table. You give the waiter your order.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Controller&lt;/em&gt;&lt;/strong&gt; - Waiters act as the go-between for customers and chefs. They take meal orders from customers, give the orders to the chefs in a structured format, and eventually return the resulting meal to the specific customer that ordered it. Have you ever eaten at a restaurant where a waiter seemed to have perfect &lt;em&gt;control&lt;/em&gt; of the situation? Where they seemed to know exactly where and when things needed to happen? The secret of any great waiter is their friendship with the chef.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Model&lt;/em&gt;&lt;/strong&gt; - A chef’s only job is to make meals. In the kitchen, they’ve got the two things they need to make a great meal: the cooking methods they’ve perfected, and the necessary ingredients on hand. A chef receives a customer’s order from a waiter, makes the customer’s meal, and gives the meal back to the waiter who puts it on the customer’s table. At a great restaurant, a chef is a &lt;em&gt;model&lt;/em&gt; to the craft of cooking.&lt;/p&gt;

&lt;p&gt;In terms of software, a view is responsible &lt;em&gt;only&lt;/em&gt; for rendering information to the user. A controller is responsible &lt;em&gt;only&lt;/em&gt; for directing various user requests to the model, and returning the model’s response. And a model is responsible &lt;em&gt;only&lt;/em&gt; for the logic and handling of data.&lt;/p&gt;

&lt;p&gt;A lot of popular web app frameworks, including &lt;strong&gt;Rails&lt;/strong&gt; and &lt;strong&gt;Express.js,&lt;/strong&gt; leverage MVC. Let's work with this example in Rails.&lt;/p&gt;




&lt;h2&gt;
  
  
  Quick Code.
&lt;/h2&gt;

&lt;p&gt;Rails is a &lt;em&gt;very&lt;/em&gt; opinionated webapp framework that strongly encourages developing within the MVC framework. Here's a high-level view of how Rails web apps are structured:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frecipe4developer.files.wordpress.com%2F2011%2F09%2Fimage.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frecipe4developer.files.wordpress.com%2F2011%2F09%2Fimage.png" title="Rails Architecture" alt="Rails Architecture"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  Fig 2. A Rails system diagram demonstrating an MVC pattern.
&lt;/h6&gt;

&lt;p&gt;But, one of the advantages of being opinionated is that Rails is &lt;em&gt;fast&lt;/em&gt;. One line in your terminal does a lot of the heavy, tedious lifting of building a webapp.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;rails new restaurant
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After you've started a new Rails app, you'll notice a directory in that project's root called app. Let's look what's inside there.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FnJKBWjJ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FnJKBWjJ.png" title="Rails Directory" alt="Rails Directory"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  Fig 3. The file structure of a new Rails app.
&lt;/h6&gt;

&lt;p&gt;Wild! Rails insists that we build in MVC. And why not? MVC is a popular design pattern for a reason. Let's go over some of the benefits of sticking to MVC.&lt;/p&gt;




&lt;h2&gt;
  
  
  Benefits.
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;As a convention, MVC sets expectations for the underlying code and makes it easier to collaborate&lt;/strong&gt;&lt;em&gt;.&lt;/em&gt; If you asked me to help you debug a web app you had designed around MVC, right out of the gate we would have a shared understanding and language of the high-level patterns of your program. This saves a lot of time and headache in collaboration.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MVC helps us separate concerns by assigning responsibilities across controllers, views, and models&lt;/strong&gt;&lt;em&gt;.&lt;/em&gt; Separation of Concerns is an essential design principle which developers use to build and maintain clean code. A modification to one part of our application will not effect the whole, and debugging will be more efficient. When we develop web apps around MVC, we stand on the shoulders of giants like Trygve Reenskaug who thought hard about separations of concerns about the past.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MVC makes it much easier to parallel develop and ship clean code faster&lt;/strong&gt;. Because it separates concerns, MVC allows for effective parallel development. Different engineers can work on the model, view, and controller concurrently.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;I hope this Hitchhiker’s Guide has helped you understand the MVC framework.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Can you write a JS function that memoizes Fibonacci?</title>
      <dc:creator>gortron</dc:creator>
      <pubDate>Mon, 06 Apr 2020 03:04:14 +0000</pubDate>
      <link>https://dev.to/gortron/can-you-write-a-js-function-that-memoizes-fibonacci-2gn0</link>
      <guid>https://dev.to/gortron/can-you-write-a-js-function-that-memoizes-fibonacci-2gn0</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;In this post, I'll walk through implementing the Fibonacci sequence in JavaScript, and then improving the approach by memoizing previous returns from the function. You can think of memoizing like implementing a 'cache' which stores information from previous calls to a function. In JavaScript, the best approach to take advantage of closures and implement an outer function, which creates the cache as a map, and an inner function, which inherits the scope of the outer function and can write/read from the cache. &lt;/p&gt;

&lt;h1&gt;
  
  
  1: Recursive Fibonacci
&lt;/h1&gt;

&lt;p&gt;Reviewing recursion is outside the scope of this post, but I wanted to share this approach with you as the memoize function will incorporate this code. This approach to fibonacci solves in O(log n) run time.&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;const&lt;/span&gt; &lt;span class="nx"&gt;fibonacci&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&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;h1&gt;
  
  
  2: Memoize Fibonacci
&lt;/h1&gt;

&lt;p&gt;Memoizing the recursive function above will make a tradeoff between speed and space. Implementing the cache will take O(n) space complexity, but the best case for the solution will become O(1) instead of O(log n).&lt;/p&gt;

&lt;p&gt;There are degrees of increasing utility (or, intelligence) to memoizing the fibonacci function.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Only cache answers for a given n, e.g. fib(20) would not benefit from previous calls of fib(21) or fib(19)&lt;/li&gt;
&lt;li&gt;Cache all solutions up to a given n, e.g. fib(20) should cache fib(1...20), but fib(21) doesn't take advantage of fib(20)&lt;/li&gt;
&lt;li&gt;Given an n greater than a previous n, leverage the solution for the previous greatest n, e.g. fib(21) should leverage fib(20)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The approach I implement below is the latter of the three. It 'caches' all previous solutions to the fibonacci function, including the recursive calls. So, memoizeFib(5) will cache the fibonacci number that corresponds to (1...5).&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;const&lt;/span&gt; &lt;span class="nx"&gt;memoizeFib&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cache&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;fibonacci&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;solution&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;solution&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;solution&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;fibonacci&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;You would call this code by creating an instance of the memoizeFib function (which creates the cache and returns the fibonacci function), and passing that instance an argument. Example:&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;const&lt;/span&gt; &lt;span class="nx"&gt;memoizedFib&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;memoizeFib&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;memoizedFib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// returns 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



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

&lt;p&gt;Closures and memoizing in JavaScript are not only used to improve performance and prevent repetitious, expensive work. They're used for data security and privacy. Consider that, for the above example, the cache for &lt;code&gt;memoizedFib&lt;/code&gt; is &lt;em&gt;not&lt;/em&gt; accessible outside the scope of &lt;code&gt;memoizedFib&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;If you want to play around with the code above, you can fork the solution I implemented (with tests) on CodePen &lt;a href="https://codepen.io/gortron/pen/OJVdrWo"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
