<?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: Beth G</title>
    <description>The latest articles on DEV Community by Beth G (@_peculiarism).</description>
    <link>https://dev.to/_peculiarism</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%2F145578%2F45ec030e-f1fd-40a0-881a-e03691ba8fd0.jpg</url>
      <title>DEV Community: Beth G</title>
      <link>https://dev.to/_peculiarism</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/_peculiarism"/>
    <language>en</language>
    <item>
      <title>Updating multiple DOM nodes using React Hooks</title>
      <dc:creator>Beth G</dc:creator>
      <pubDate>Sun, 24 May 2020 22:51:52 +0000</pubDate>
      <link>https://dev.to/_peculiarism/updating-multiple-dom-nodes-using-react-hooks-ip0</link>
      <guid>https://dev.to/_peculiarism/updating-multiple-dom-nodes-using-react-hooks-ip0</guid>
      <description>&lt;p&gt;(&lt;strong&gt;Disclaimer&lt;/strong&gt;: I'm not a UX designer, or an IXD specialist, so this article is based on a fun experiment, and my specific use case)&lt;/p&gt;

&lt;p&gt;TL;DR: You can use &lt;code&gt;useLayoutEffect()&lt;/code&gt; to add styling to multiple components if you're crazy like me!&lt;/p&gt;

&lt;p&gt;So far during lockdown (or isolation, quarantine, #stayathome, social distancing, whatever floats your boat) I have succeeded in the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Making banana bread&lt;/li&gt;
&lt;li&gt;Gutting my room (goodbye shoes from 2007)&lt;/li&gt;
&lt;li&gt;Finding a new taste for gardening&lt;/li&gt;
&lt;li&gt;Spending far too much money buying (and preparing to customize) an Ergodox EZ (the excitement is real)&lt;/li&gt;
&lt;li&gt;And getting around to building my own website.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I am completely against the idea that everyone should use this time to improve their technical skills, or have five side projects they should be working on; as I very quickly realised, we are working during a crisis, not just working from home. &lt;em&gt;That&lt;/em&gt; being said, I have genuinely wanted (and felt in the right headspace) to expand my knowledge, and as a full stack engineer specialising in front end development, I obviously gravitated towards a new framework, this time around being &lt;a href="https://www.gatsbyjs.org/"&gt;GatsbyJS&lt;/a&gt;. I have been keen to look at &lt;a href="https://nextjs.org/"&gt;Next.js&lt;/a&gt;, but for the purpose of a small portfolio website, I felt like Gatsby offered a quick (and fast) setup, with a less steep learning curve. Maybe for 2.0.&lt;/p&gt;

&lt;p&gt;Like any portfolio website I wanted to incorporate navigation of some kind, but I had commissioned an illustration for my main landing page; adding a top nav felt quite...clunky. The incredible &lt;a href="https://www.instagram.com/_dollgirls"&gt;Kai Kwong&lt;/a&gt; created the design, so I had the brainwave to grab one of the SVGs, add a spin to it, and boom, we have some cute, theme-compatible, &lt;code&gt;:hover&lt;/code&gt; navigation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/Pm9kEyv0myPRiIPBwc/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/Pm9kEyv0myPRiIPBwc/giphy.gif" alt="There's a gif because why not"&gt;&lt;/a&gt;Hurry up and get to the point, Beth&lt;/p&gt;

&lt;p&gt;Cute right?&lt;/p&gt;

&lt;p&gt;But what about mobile?&lt;/p&gt;

&lt;p&gt;🤦‍♀️&lt;/p&gt;

&lt;p&gt;Although beautiful on a desktop, my new navigation just didn't have the same result on mobile, particularly with the addition of &lt;code&gt;:hover&lt;/code&gt; behaviour. I reluctantly started working with a left nav, and then had &lt;strong&gt;another&lt;/strong&gt; brainwave (my Saturday nights have been wild I am telling you): why not both? Why not have a left nav which triggered the star SVG animation when on desktop, and on mobile became the prime mode of navigation? Going against EVERYTHING I knew about UX, but I liked it, so screw it!&lt;/p&gt;

&lt;p&gt;I had dabbled in React Hooks, but as the product I developed (professionally, the one I work on that makes me money) was still at v16.4, the opportunities to experiment with the various hooks was in my own personal time. The problem I now faced couldn't be resolved with CSS (I had an unordered list for a left nav, with the star SVG navigation being at different depths within the DOM), and I was initially leaning towards &lt;code&gt;useEffect()&lt;/code&gt; (it is a side effect after all), but as I was manipulating the DOM my researching (ahem, Googling) lead me to &lt;code&gt;useLayoutEffect()&lt;/code&gt;. As every blog post on the topic states:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The signature is identical to useEffect, but it fires synchronously after all DOM mutations.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What this basically means is that unlike the asynchronous behaviour of &lt;code&gt;useEffect()&lt;/code&gt;, &lt;code&gt;useLayoutEffect()&lt;/code&gt; is blocking, and runs prior to browser painting. You generally don't want to use it due to it's blocking nature, but as I wanted the &lt;code&gt;:hover&lt;/code&gt; interactions to be as smooth as possible, and as the function wouldn't be long running, performance hits would be minimal. To illustrate the difference in response time I've included two snippets of the design I was going with (albeit slightly tweaked), the first is using the &lt;code&gt;useEffect()&lt;/code&gt; hook:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/eglenfield/embed/WNQWeOE?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;And the second uses the &lt;code&gt;useLayoutEffect()&lt;/code&gt; hook:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/eglenfield/embed/jObJjYR?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Although milliseconds in difference, to the human eye any slight delay is noticeable, as well as the more noticeable flicker when changing selection. As this is a CodePen I kept it to a one-way binding between the left nav and the individual divs, but in a React project you could have as many components linked in this way (I have no idea why but each to their own) as you like, and a two-way binding could be formed. &lt;/p&gt;

&lt;p&gt;Feel free to nab the code for your desire of strange and wonderful navigation patterns (like myself), I will likely be posting more as the website develops, so stay tuned! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/mR3dXKpI6P8CA/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/mR3dXKpI6P8CA/giphy.gif" alt="That's all folks"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>hooks</category>
      <category>uselayouteffect</category>
      <category>gatsby</category>
    </item>
  </channel>
</rss>
