<?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>Forem: Chris Colborne</title>
    <description>The latest articles on Forem by Chris Colborne (@zorfling).</description>
    <link>https://forem.com/zorfling</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%2F51035%2Fc457fe05-f215-4dec-8d39-dbac7e5f9236.jpeg</url>
      <title>Forem: Chris Colborne</title>
      <link>https://forem.com/zorfling</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/zorfling"/>
    <language>en</language>
    <item>
      <title>Object Destructuring 101</title>
      <dc:creator>Chris Colborne</dc:creator>
      <pubDate>Sun, 08 Nov 2020 00:00:00 +0000</pubDate>
      <link>https://forem.com/zorfling/object-destructuring-101-2elb</link>
      <guid>https://forem.com/zorfling/object-destructuring-101-2elb</guid>
      <description>&lt;h2&gt;
  
  
  aka what on Earth does &lt;code&gt;doIt({ x: x2 = getX() } = {})&lt;/code&gt; mean?!
&lt;/h2&gt;

&lt;p&gt;You’re getting used to React. You’ve got components and arrow functions down pat. But then you run into this beast:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;doRender&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;state&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="nx"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getOptions&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;overrides&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// ... }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wait, what, how? Brackets and colons and equals, oh my! This little snippet breaks your brain. What was this developer thinking? Were they just being too clever, or what?!&lt;/p&gt;

&lt;p&gt;Although it certainly is bracket soup, there &lt;strong&gt;is&lt;/strong&gt; a method to the madness.&lt;/p&gt;

&lt;p&gt;This is a few different levels of object destructuring, layered on top of each other, inside a function call. Let’s break it down bit by bit, so that you can read it like a pro.&lt;/p&gt;

&lt;h2&gt;
  
  
  Level 1: Basic Destructuring
&lt;/h2&gt;

&lt;p&gt;First up, let’s start with the basics. Object destructuring is just a way of extracting certain keys directly from an object. It’s used quite heavily in React and other modern JavaScript frameworks. In fact you probably already use it. It looks like this in its basic form.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Chris&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;chris@example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Brisbane&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// extracts 'Brisbane' and assigns it to a variable `city`&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;city&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myObject&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Easy right? Let’s keep going.&lt;/p&gt;

&lt;h2&gt;
  
  
  Level 2: Renaming Destructuring
&lt;/h2&gt;

&lt;p&gt;So next level, what if we already had a variable &lt;code&gt;city&lt;/code&gt;? Let’s rename it as we extract it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Chris&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;chris@example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Brisbane&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// oops we already have city in scope&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sydney&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

&lt;span class="c1"&gt;// extracts 'Brisbane' and assigns it to a variable `myCity`&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;myCity&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myObject&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two from two, got it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Level 3: Multi-Level Destructuring
&lt;/h2&gt;

&lt;p&gt;Next up let’s tackle multi-level destructuring. That’s when the variable you want to destructure is actually nested inside another key. Let’s try and get at &lt;code&gt;city&lt;/code&gt; and &lt;code&gt;state&lt;/code&gt; in this nested object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Chris&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;chris@example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Brisbane&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;QLD&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// now city variable is 'Brisbane' and state variable is 'QLD'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;city&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="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myObject&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice a trick here - &lt;code&gt;address&lt;/code&gt; isn’t actually destructured, it’s just used to get at its children. If you wanted the full address as well, you could either destructure the address first, then destructure address into city and state, or destructure twice.&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="c1"&gt;// destructure `address` then `city` from `address`&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;address&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myObject&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;city&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// destructure `address` itself, then `city` from within it&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;address&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myObject&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great, we’re starting to look like that initial snippet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Level 4: Destructuring Defaults
&lt;/h2&gt;

&lt;p&gt;Next level is destructuring defaults. Up until now, we’ve been assuming the data is there. But what happens if a particular key &lt;em&gt;might&lt;/em&gt; be there, or &lt;em&gt;might&lt;/em&gt; not? That’s where defaults come into play.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Chris&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;chris@example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="c1"&gt;// city is missing for this one&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// `city` in this case will be `undefined`&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myObject&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

&lt;span class="c1"&gt;// let's add a default&lt;/span&gt;
&lt;span class="c1"&gt;// now `city` will be 'Sydney' since it's not set in `myObject`&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sydney&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myObject&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;myObject2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;city2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Brisbane&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="c1"&gt;// but `city2` here will be 'Brisbane' since it was set in `myObject2`&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;city2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sydney&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myObject2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we try to do multi-level destructuring (or more generally try to destructure something that might be undefined), that’s where we might run into problems. Take this example, we try and get the &lt;code&gt;city&lt;/code&gt; from the &lt;code&gt;address&lt;/code&gt;, but there is no &lt;code&gt;address&lt;/code&gt; in &lt;code&gt;myObject&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Chris&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;chris@example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="c1"&gt;// sometimes we have address, but not this time&lt;/span&gt;
  &lt;span class="c1"&gt;// address: {&lt;/span&gt;
  &lt;span class="c1"&gt;// city: 'Brisbane',&lt;/span&gt;
  &lt;span class="c1"&gt;// }&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// bah bow - cannot read property 'city' of undefined&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myObject&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

&lt;span class="c1"&gt;// so let's fix that with a default empty object&lt;/span&gt;
&lt;span class="c1"&gt;// now we're looking for `city` in an empty object, &lt;/span&gt;
&lt;span class="c1"&gt;// which won't fail - `city` will be undefined&lt;/span&gt;
&lt;span class="c1"&gt;// but won't blow up&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myObject&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Full Circle
&lt;/h2&gt;

&lt;p&gt;So now we’re back to our original brain breaker. We can see now that all we’ve got is some multi-level destructuring with defaults.&lt;/p&gt;

&lt;p&gt;Still not convinced? Ok, we’ll step through it bit by bit to make sure it sinks in:&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="c1"&gt;// function call&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;doRender&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;

  &lt;span class="c1"&gt;// first parameter called `ui`&lt;/span&gt;
  &lt;span class="nx"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

  &lt;span class="c1"&gt;// destructure the second parameter&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// extract and rename `state` to variable `initialState`&lt;/span&gt;
    &lt;span class="na"&gt;state&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="c1"&gt;// extract `options` to a variable, and if it's unset, &lt;/span&gt;
    &lt;span class="c1"&gt;// default to the result of `getOptions()`&lt;/span&gt;
    &lt;span class="nx"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getOptions&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;overrides&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="c1"&gt;// finally, default the second parameter to empty object, as it is optional&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// ... }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hopefully, this has helped you see that even the most confusing looking destructuring is made up of these 4 levels. Parse them one by one, and you’ll be reading and writing code like this in no time.&lt;/p&gt;

</description>
      <category>react</category>
      <category>syntax</category>
      <category>javascript</category>
      <category>es6</category>
    </item>
    <item>
      <title>Which Headless CMS should I use for a new Gatsby site?</title>
      <dc:creator>Chris Colborne</dc:creator>
      <pubDate>Sun, 19 Apr 2020 00:00:00 +0000</pubDate>
      <link>https://forem.com/zorfling/which-headless-cms-should-i-use-for-a-new-gatsby-site-4ink</link>
      <guid>https://forem.com/zorfling/which-headless-cms-should-i-use-for-a-new-gatsby-site-4ink</guid>
      <description>&lt;p&gt;Gatsby is an awesome static site generator, but you need to get your data from somewhere. What are your options for headless CMSs?&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Gatsby?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.gatsbyjs.org/"&gt;Gatsby&lt;/a&gt; is a free and open source framework based on React that helps developers build blazing fast websites and apps. It basically bashes together React, GraphQL and the so called JAM Stack to make some serious awesomeness.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the JAM Stack?
&lt;/h2&gt;

&lt;p&gt;The JAM Stack is a name standing for a modern way of building sites/apps with JavaScript, APIs and Markup. Commonly it involves server side generated code from Markdown and/or API endpoints.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a headless CMS?
&lt;/h2&gt;

&lt;p&gt;In this new JAM Stack world, your CMS backend and your frontend site are split. A headless CMS is simply a CMS which instead of pumping out HTML and CSS views, produces an API. There are many options from open source, to paid SaaS options, to trusty old WordPress.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are the options?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Open source options
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://www.netlifycms.org/"&gt;Netlify CMS&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Netlify is one of the most common hosting providers for the JAM stack. They’ve built an open source SPA which you can install as an npm module on your site, to provide a CMS experience over GitHub and Markdown, complete with editorial workflow using pull requests.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.gatsbyjs.org/starters/netlify-templates/gatsby-starter-netlify-cms/"&gt;Try it out&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://strapi.io/"&gt;Strapi&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Strapi is an open source, self hosted headless CMS written in Node. It provides both REST and GraphQL endpoints, with a customisable admin portal and API.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/strapi/strapi-starter-gatsby-blog"&gt;Try it out&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://www.keystonejs.com/"&gt;Keystone&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Keystone is a scalable, extensible and open-source platform to build NodeJS applications. It has first-class GraphQL support, and a great Admin UI.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.gatsbyjs.org/docs/third-party-graphql/"&gt;Try it out&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://www.wordpress.org"&gt;WordPress&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Even though WordPress is obviously a full CMS and blogging platform, it can also be used as a headless CMS by using its &lt;a href="https://developer.wordpress.org/rest-api/"&gt;REST API&lt;/a&gt; or using the &lt;a href="https://www.wpgraphql.com/"&gt;WPGraphQL plugin&lt;/a&gt; to provide a GraphQL endpoint. One thing to note is that you may have more or less success depending on how many plugins you use.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.gatsbyjs.org/starters/GatsbyCentral/gatsby-starter-wordpress/"&gt;Try it out&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Software as a Service (SaaS)
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://sanity.io/"&gt;Sanity&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Sanity is one of the new breed of SaaS headless CMSs. It consists of an open source CMS admin UI which you install as an npm package and host. This allows you to easily extend for your own workflow with their extension ecosystem. The admin then connects to Sanity for storing the data, and for authentication.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/sanity-io/gatsby-source-sanity"&gt;Try it out&lt;/a&gt; |&lt;a href="https://www.sanity.io/pricing"&gt;Pricing&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://www.contentful.com/"&gt;Contentful&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Contentful is one of the larger SaaS headless CMSs, and the oldest on our list here, being launched back in 2014. Clearly pitching themselves as the Enterprise solution, Contentful has the most clout, but also the price point to match. One thing to watch is that they may not have the features and extensibility common in the current generation of SaaS headless CMSs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.gatsbyjs.org/starters/contentful-userland/gatsby-contentful-starter/"&gt;Try it out&lt;/a&gt; |&lt;a href="https://www.contentful.com/pricing/"&gt;Pricing&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://prismic.io/"&gt;Prismic&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Prismic is another of the new breed of SaaS headless CMSs. It provides an excellent hosted admin with the ability to easily add custom types, and publishes a REST and GraphQL API.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/prismicio/gatsby-blog"&gt;Try it out&lt;/a&gt; |&lt;a href="https://prismic.io/pricing"&gt;Pricing&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://www.forestry.io/"&gt;Forestry&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Forestry has been described as Netlify CMS on steroids. Like Netlify CMS, it connects to GitHub to store markdown files which is great for portability. Unlike Netlify CMS however, Forestry also provides a hosted CMS admin UI, providing a more polished, managed solution. However you can also deploy the editor to your own site with &lt;a href="https://forestry.io/docs/editing/remote-admin/"&gt;Forestry Remote Admin&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/forestryio/gatsby-starter-blog"&gt;Try it out&lt;/a&gt; |&lt;a href="https://www.forestry.io/pricing/"&gt;Pricing&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Which one should you use?
&lt;/h2&gt;

&lt;p&gt;Like everything, it depends! 🤷‍♂️&lt;/p&gt;

&lt;p&gt;A lot of it comes down to how you want to store your data (git / hosted service) and the editing experience.&lt;/p&gt;

&lt;p&gt;If it’s a commercial site, you’ve got the money, or just really want something turnkey, then try one of the SaaS options. They all have free plans to start, so sign up and try them out to see what fits.&lt;/p&gt;

&lt;p&gt;If it’s a simple blog, Netlify CMS will do great. (in fact I’ll be using it for the new version of my blog!)&lt;/p&gt;

&lt;p&gt;If it’s a bigger site / application, give a more full powered solution a go such as Keystone or Strapi.&lt;/p&gt;

&lt;p&gt;And if you need to work with clients, you might be stuck with WordPress, but at least you can have some shiny front end tech and they get the benefits of a performant, secure site.&lt;/p&gt;

</description>
      <category>gatsby</category>
      <category>react</category>
      <category>wordpress</category>
      <category>blog</category>
    </item>
    <item>
      <title>Can I use React Helmet to add social card meta tags for Twitter and Facebook?</title>
      <dc:creator>Chris Colborne</dc:creator>
      <pubDate>Tue, 18 Feb 2020 08:30:00 +0000</pubDate>
      <link>https://forem.com/zorfling/can-i-use-react-helmet-to-add-social-card-meta-tags-for-twitter-and-facebook-4a36</link>
      <guid>https://forem.com/zorfling/can-i-use-react-helmet-to-add-social-card-meta-tags-for-twitter-and-facebook-4a36</guid>
      <description>&lt;p&gt;So you’ve built an awesome React powered site. It’s shiny, and cool, and does everything you need… until your client/boss/best mate’s dad asks about social sharing meta tags. ‘Hey mate, I’ve heard you can put some tags in and I’ll get the cool card when I share my pages on Twitter and Facebook?’ Uhhh sure, just chuck them in &lt;a href="https://github.com/nfl/react-helmet"&gt;React Helmet&lt;/a&gt; and you’ll be sweet right? But hang on, it’s not working!&lt;/p&gt;

&lt;p&gt;So what’s the deal, can you add social share meta tags with React Helmet?&lt;/p&gt;

&lt;p&gt;Absolutely… with a caveat.&lt;/p&gt;

&lt;p&gt;See, the problem is that the various social networks don’t run your JavaScript and so won’t see your dynamically updated meta tags as they’re added on page load.&lt;/p&gt;

&lt;p&gt;So the answer is that you need a way to either statically generate, or server-side render your React app.&lt;/p&gt;

&lt;p&gt;Whilst not exhaustive, here are a couple of ways you can do that.&lt;/p&gt;

&lt;h2&gt;
  
  
  React Snapshot
&lt;/h2&gt;

&lt;p&gt;Probably the easiest if you’ve just got a Create React App (CRA) app, is to use &lt;a href="https://github.com/geelen/react-snapshot"&gt;React Snapshot&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;React Snapshot takes your CRA app, follows any internal links it can find and generates a static site.&lt;/p&gt;

&lt;p&gt;Just a simple npm install, update 3 lines and you’ve got a statically generated site.&lt;/p&gt;

&lt;h2&gt;
  
  
  static-site-generator-webpack-plugin
&lt;/h2&gt;

&lt;p&gt;If you’ve got a custom Webpack setup, you can use the &lt;a href="https://github.com/markdalgleish/static-site-generator-webpack-plugin"&gt;static-site-generator-webpack-plugin&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This plugin formed the basis of Gatsby (mentioned next) before version 2.&lt;/p&gt;

&lt;h2&gt;
  
  
  Gatsby
&lt;/h2&gt;

&lt;p&gt;As primarily a static site generator, &lt;a href="https://www.gatsbyjs.org/"&gt;Gatsby&lt;/a&gt; can do this out of the box. If you use the &lt;a href="https://github.com/gatsbyjs/gatsby-starter-blog"&gt;blog starter&lt;/a&gt;, you can use the &lt;a href="https://github.com/gatsbyjs/gatsby-starter-blog/blob/master/src/components/seo.js"&gt;SEO component&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even if you’re not using the blog starter, you can use the SEO component for inspiration and implement it in your site.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next.js
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://nextjs.org/"&gt;Next.js&lt;/a&gt; has a feature called &lt;a href="https://nextjs.org/docs/advanced-features/static-html-export"&gt;Static HTML Export&lt;/a&gt; which can statically render your site.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ next build &amp;amp;&amp;amp; next export&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And if you’re not using &lt;code&gt;getInitialProps&lt;/code&gt;, you might not even need to use &lt;code&gt;next export&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next.js has a feature called &lt;a href="https://nextjs.org/docs/advanced-features/automatic-static-optimization"&gt;Automatic Static Optimization&lt;/a&gt; which will automatically prerender &lt;code&gt;.html&lt;/code&gt; files for those it can.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing your social cards
&lt;/h2&gt;

&lt;p&gt;Once you’ve set up your social card meta tags, you can test your meta tags using the following tools.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://cards-dev.twitter.com/validator"&gt;Twitter Card Validator&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developers.facebook.com/tools/debug/"&gt;Facebook Sharing Debugger&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>gatsby</category>
      <category>javascript</category>
      <category>socialmedia</category>
    </item>
    <item>
      <title>Is it ever OK to set state directly? A setState cheat sheet</title>
      <dc:creator>Chris Colborne</dc:creator>
      <pubDate>Mon, 03 Feb 2020 00:00:00 +0000</pubDate>
      <link>https://forem.com/zorfling/is-it-ever-ok-to-set-state-directly-a-setstate-cheat-sheet-531a</link>
      <guid>https://forem.com/zorfling/is-it-ever-ok-to-set-state-directly-a-setstate-cheat-sheet-531a</guid>
      <description>&lt;p&gt;One of the parts of React that trips people up is understanding state and how to update it. A lot of this confusion comes from the fact that setState is asynchronous.&lt;/p&gt;

&lt;p&gt;As they write in the &lt;a href="https://reactjs.org/docs/react-component.html#setstate"&gt;React docs&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Think of setState() as a &lt;strong&gt;request&lt;/strong&gt; rather than an immediate command to update the component.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;(Note: I’ll be referring to setState, but it applies equally to the useState hook which we’ll cover later)&lt;/p&gt;

&lt;h2&gt;
  
  
  So if I want to update immediately, I should just update the state object directly… right?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;this.state.myValue = 'newValue';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;NO! If you do this, you’re opting out of React’s handling, and your component won’t re-render.&lt;/p&gt;

&lt;p&gt;The reason setState exists is to allow React to produce a more efficient and better experience for the end user. By batching the state updates, it can prevent needless re-renders and can allow React to do even more smart handling of different priority updates, aka &lt;a href="https://reactjs.org/docs/concurrent-mode-intro.html"&gt;concurrent mode&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  OK, OK, I’ll use setState. But what about the weird updater function syntax? Surely I don’t need that?
&lt;/h2&gt;

&lt;p&gt;Actually you do.&lt;/p&gt;

&lt;p&gt;setState (and useState) have two forms, the updater function and the stateChange object syntax.&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="c1"&gt;// Updater function&lt;/span&gt;
&lt;span class="c1"&gt;// pass a function to show what to update&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;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevState&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&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="c1"&gt;// stateChange object&lt;/span&gt;
&lt;span class="c1"&gt;// pass an object to show what to update&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;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;New Name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Which one should I use when?
&lt;/h2&gt;

&lt;p&gt;Here’s a quick rule of thumb.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If your next state depends on the current state, use the updater function. Otherwise you’re fine to pass an object in the stateChange syntax.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;updating a count (updater function)&lt;/li&gt;
&lt;li&gt;updating an object (updater function)&lt;/li&gt;
&lt;li&gt;toggling a boolean (updater function)&lt;/li&gt;
&lt;li&gt;setting a new string value (stateChange object)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What about hooks and useState?
&lt;/h2&gt;

&lt;p&gt;All the cases above apply equally to the useState hook.&lt;/p&gt;

&lt;p&gt;Where you would use the updater function in setState, use the updater function in your hook setter function.&lt;/p&gt;

&lt;p&gt;See the below examples in both setState and useState hook.&lt;/p&gt;

&lt;h2&gt;
  
  
  So how about that cheat sheet?
&lt;/h2&gt;

&lt;p&gt;Most situations can be boiled down to one of a few examples, shown below:&lt;/p&gt;

&lt;h3&gt;
  
  
  Updating a count
&lt;/h3&gt;

&lt;p&gt;Relies on previous state, use updater function&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="c1"&gt;// this.setState()&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;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevState&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&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="c1"&gt;// useState hook&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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevCount&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prevCount&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Updating an object
&lt;/h3&gt;

&lt;p&gt;Relies on previous state, use updater function&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="c1"&gt;// this.setState()&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;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevState&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;config&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;prevState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;updatedValues&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="c1"&gt;// useState hook&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;config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setConfig&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;setConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevConfig&lt;/span&gt; &lt;span class="o"&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="nx"&gt;prevConfig&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;updatedValues&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Toggling a boolean
&lt;/h3&gt;

&lt;p&gt;Relies on previous state, use updater function&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="c1"&gt;// this.setState()&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;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevState&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;isEnabled&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isEnabled&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// useState hook&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;isEnabled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIsEnabled&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;setIsEnabled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevIsEnabled&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;prevIsEnabled&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Setting a new string value
&lt;/h3&gt;

&lt;p&gt;Doesn’t rely on previous state, use stateChange object&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="c1"&gt;// this.setState()&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;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;stringValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;doesn't rely on previous state&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// useState hook&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;stringValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setStringValue&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a default string&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;setStringValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;doesn't rely on the previous state&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  To wrap up
&lt;/h2&gt;

&lt;p&gt;Component state is one of the main concepts you need to understand in React. Remember these simple rules and you’ll never be confused by setState again!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Never change state directly: always use setState or the useState setter function.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;If your next state depends on the current state, use the updater function.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>react</category>
      <category>setstate</category>
      <category>javascript</category>
      <category>hooks</category>
    </item>
    <item>
      <title>Avoid npm build errors by tracking your Node version with nvm</title>
      <dc:creator>Chris Colborne</dc:creator>
      <pubDate>Tue, 21 Jan 2020 08:15:00 +0000</pubDate>
      <link>https://forem.com/zorfling/avoid-npm-build-errors-by-tracking-your-node-version-with-nvm-36a</link>
      <guid>https://forem.com/zorfling/avoid-npm-build-errors-by-tracking-your-node-version-with-nvm-36a</guid>
      <description>&lt;p&gt;You know the feeling. You get a spare moment jump in and do some updates on an old app. But you try to get it running and the dreaded npm errors come up.&lt;/p&gt;

&lt;p&gt;Oftentimes, the issue is that you’ve updated Node since you last ran it. Many issues with dependencies and npm can been easily solved by switching Node versions.&lt;/p&gt;

&lt;p&gt;Easy, so just go back to the version of node you used when you originally developed it. But how do you know which version to jump to? Just guess and check?&lt;/p&gt;

&lt;p&gt;Perhaps you can leave a note in your README saying “This works in Node version X.” And sure, that’s certainly one way of solving it.&lt;/p&gt;

&lt;p&gt;But using &lt;code&gt;nvm&lt;/code&gt;, we can track the version, as well as automate the switch back, and that’s what we’ll talk about today.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;nvm&lt;/code&gt; is &lt;a href="https://nvm.sh"&gt;Node Version Manager&lt;/a&gt;. It primarily allows you to switch between isolated Node environments for different versions. If you’ve used rvm/rbenv in Ruby, it’s similar to that.&lt;/p&gt;

&lt;p&gt;For our purposes today however, the killer feature is the &lt;code&gt;.nvmrc&lt;/code&gt; file. &lt;code&gt;.nvmrc&lt;/code&gt; is a file you commit with your project which contains a single line with the version of Node to use for this project.&lt;/p&gt;

&lt;p&gt;In a project with an &lt;code&gt;.nvmrc&lt;/code&gt; file, you can type &lt;code&gt;nvm use&lt;/code&gt; and it will automatically switch back to the version you’ve stated. If you’re really keen, you can (and I’d say you should) also set up your shell to &lt;a href="https://github.com/nvm-sh/nvm/blob/master/README.md#deeper-shell-integration"&gt;automatically &lt;code&gt;nvm use&lt;/code&gt;&lt;/a&gt; when it encounters an &lt;code&gt;.nvmrc&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;So start using &lt;code&gt;nvm&lt;/code&gt; and &lt;code&gt;.nvmrc&lt;/code&gt; in your projects and next time you’re returning to an old project, you’re just an &lt;code&gt;nvm use&lt;/code&gt; away from being back up and running quick smart.&lt;/p&gt;

</description>
      <category>npm</category>
      <category>nvm</category>
      <category>node</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
