<?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: Adrian Unger</title>
    <description>The latest articles on DEV Community by Adrian Unger (@staydecent).</description>
    <link>https://dev.to/staydecent</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%2F128797%2F1a1de570-2db2-4731-b54a-2ee179a7a401.jpeg</url>
      <title>DEV Community: Adrian Unger</title>
      <link>https://dev.to/staydecent</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/staydecent"/>
    <language>en</language>
    <item>
      <title>Why Redux over a more basic Global Store</title>
      <dc:creator>Adrian Unger</dc:creator>
      <pubDate>Thu, 14 Nov 2019 17:16:44 +0000</pubDate>
      <link>https://dev.to/staydecent/why-redux-over-a-more-basic-global-store-3lh2</link>
      <guid>https://dev.to/staydecent/why-redux-over-a-more-basic-global-store-3lh2</guid>
      <description>&lt;p&gt;Lately there's been some "discussion" about the boilerplate around using Redux for application state.&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1191745686980694017-8" src="https://platform.twitter.com/embed/Tweet.html?id=1191745686980694017"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1191745686980694017-8');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1191745686980694017&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;p&gt;It's easy to look at Redux and think it's just an over-complicated global state. You could strip away much of the API and just implement an immutable object state, even mimicking the React state API of just &lt;code&gt;setState&lt;/code&gt; and &lt;code&gt;getState&lt;/code&gt;. &lt;/p&gt;




&lt;p&gt;&lt;em&gt;For a Global Store that supports &lt;code&gt;getState&lt;/code&gt;, &lt;code&gt;setState&lt;/code&gt; and Redux compatible actions and reducers, I created &lt;a href="https://github.com/staydecent/atom" rel="noopener noreferrer"&gt;atom&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;




&lt;p&gt;And, this will get you pretty far. Then, throw in a few helpers to map or select parts of the state object and use them as props in your Components, and you actually have a solution that's very friendly for rapid prototyping. Without the need to create specific Actions and Reducers, you reduce boilerplate, and can just start modifying state from your components:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;globalStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;globalState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newTodo&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 creating prototypes or MVPs, where speed is a priority, a simple global store is a good solution. So, why would you bother with the added complexity of Redux, Actions, and Reducers?&lt;/p&gt;

&lt;h2&gt;
  
  
  Traceable state changes
&lt;/h2&gt;

&lt;p&gt;The origin of state change is easier to follow since the change is triggered by an Action. An action is basically just a descriptor of what should happen. ie. &lt;code&gt;ADD_TODO&lt;/code&gt;, &lt;code&gt;UPDATE_TODO&lt;/code&gt;, &lt;code&gt;REMOVE_TODO&lt;/code&gt;. This becomes more important when you have Components at various points in your React tree that can change the same data in your store.&lt;/p&gt;

&lt;p&gt;You can also observe the order in which Actions were triggered and make sure the changes are happening in a sequence that makes sense for your app. This can be thought about in terms of data-integrity as well as the User Experience.&lt;/p&gt;

&lt;p&gt;With Redux Devtools, this is a really pleasant experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Refactoring
&lt;/h2&gt;

&lt;p&gt;Because state changes are triggered by Actions, you can easily search your project for all instances of a specific Action being triggered. Perhaps, later on in the life of your project, you need to include some additional data for a specific type of state change. It's much easier to update a single Reducer to ensure that data now exists, and then search for all trigger instances for that Action.&lt;/p&gt;

&lt;p&gt;With a global &lt;code&gt;setState&lt;/code&gt; solution, it's much harder to search for the triggers for a specific change to state, ie.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&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="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;idToRemove&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;If you don't follow the exact formatting in every case where you remove a todo, you will not be able to perform a quick, project-wide search and replace.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding balance
&lt;/h2&gt;

&lt;p&gt;The sentiment from that above embedded tweet isn't wrong. I've worked on several React/Redux projects where there were many dozens of files trying to group even more numerous action definitions, and reducers, and — oh my — middleware.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So how can you keep the number of Actions and Reducers in your project under control?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, you need a generalized way to affect state change. &lt;a href="https://github.com/staydecent/wasmuth/blob/master/packages/node_modules/%40wasmuth/path-reducer/path-reducer.js" rel="noopener noreferrer"&gt;Here's the &lt;code&gt;pathReducer&lt;/code&gt;&lt;/a&gt; I use on every project.&lt;/p&gt;

&lt;p&gt;If a specific state change only occurs in one or two places (components) you should be fine to rely on the generalized state change. Once you have state changes that partially overlap, defining a specific action and reducer can help with any potential debugging (see above about Traceability).&lt;/p&gt;

&lt;p&gt;Then, once a project becomes more mature (re: Launched and with a userbase!), you may want to set time aside to write specific actions and reducers to replace those generalized state changes. Because of the improved traceability and refactoring, your project can become easier to maintain.&lt;/p&gt;

&lt;h2&gt;
  
  
  Anything else?
&lt;/h2&gt;

&lt;p&gt;Did I miss anything? From my experience, the above two points are what make Redux and the concept of Actions and Reducers stand out.&lt;/p&gt;

&lt;p&gt;I still believe a simpler global &lt;code&gt;setState&lt;/code&gt; can really help get an MVP out the door quickly. But, eventually, as an app and codebase grow, you'll likely benefit from the traceability and easy refactoring that comes with specific Actions and Reducers.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>redux</category>
    </item>
    <item>
      <title>The Difference Between Static and Bound Methods</title>
      <dc:creator>Adrian Unger</dc:creator>
      <pubDate>Mon, 14 Jan 2019 18:32:55 +0000</pubDate>
      <link>https://dev.to/staydecent/the-difference-between-static-and-bound-methods-28bk</link>
      <guid>https://dev.to/staydecent/the-difference-between-static-and-bound-methods-28bk</guid>
      <description>&lt;p&gt;Recently, at &lt;a href="http://inputlogic.ca" rel="noopener noreferrer"&gt;work&lt;/a&gt;, we were coming up with our &lt;a href="https://github.com/inputlogic/styleguides/blob/master/react.md" rel="noopener noreferrer"&gt;React Style Guide&lt;/a&gt; (based on &lt;a href="https://github.com/airbnb/javascript/tree/master/react" rel="noopener noreferrer"&gt;AirBnBs&lt;/a&gt;). One of the "rules" is to avoid binding methods within the render method (as this creates a new function each render), and a teammate mentioned, "why not just use static arrow functions?"&lt;/p&gt;

&lt;p&gt;While both solutions were practically equivalent, I worried about the technical differences. Static methods are defined on the Class &lt;em&gt;not&lt;/em&gt; on each prototype instance. &lt;/p&gt;

&lt;h2&gt;
  
  
  Wait, so how is the arrow function properly binding the correct &lt;code&gt;this&lt;/code&gt; to the static?!
&lt;/h2&gt;

&lt;p&gt;This magic had me worried. So I pulled up a &lt;a href="https://babeljs.io/repl" rel="noopener noreferrer"&gt;Babel REPL&lt;/a&gt; and wrote two classes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Static&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;onClickDiv&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="c1"&gt;// do stuff&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;render &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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onClickDiv&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bound&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onClickDiv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onClickDiv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;onClickDiv &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// do stuff&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;render &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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onClickDiv&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The transpiled Bound class was as you would expect, but the Static class resulted in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Static&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;_temp&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;_temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onClickDiv&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="c1"&gt;// do stuff&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;_temp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;render&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;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&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;onClick&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onClickDiv&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;Oh! So maybe these are technically equivilant! But defining a Class method is still a different syntax than defining a function on a &lt;code&gt;this&lt;/code&gt; reference. So I dropped the transpiled code into a &lt;a href="https://developer.mozilla.org/en-US/docs/Tools/Scratchpad" rel="noopener noreferrer"&gt;FireFox scratchpad&lt;/a&gt;.&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/http%3A%2F%2Fstaydecent.ca%2Fassets%2Fmedia%2Fclass-prototypes.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/http%3A%2F%2Fstaydecent.ca%2Fassets%2Fmedia%2Fclass-prototypes.png" alt="Screenshot of the Firefox Console showing the Static and Bound prototypes."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The only difference seems to be a bound class method exists on the prototype whereas the static arrow function does not.&lt;/p&gt;

&lt;p&gt;But, how or why would this matter? In my experience, it usually doesn't, unless you want to mock a class method in your tests (by overriding the definition on the prototype), or — &lt;a href="https://reactjs.org/docs/composition-vs-inheritance.html" rel="noopener noreferrer"&gt;god forbid!&lt;/a&gt; — you plan on extending the class.&lt;/p&gt;

&lt;p&gt;In the end, because of the techincal difference, we aren't enforcing static methods over bound methods in our style guide, rather both are fine, as long as you're aware of the implications.&lt;/p&gt;

&lt;p&gt;Have I missed any other differences between the two? I'd love to know if there's other details to consider.&lt;/p&gt;

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