<?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: William Ghelfi</title>
    <description>The latest articles on Forem by William Ghelfi (@trumbitta).</description>
    <link>https://forem.com/trumbitta</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%2F285968%2Fbde8d558-2add-4006-8c4a-4ef94c081385.jpeg</url>
      <title>Forem: William Ghelfi</title>
      <link>https://forem.com/trumbitta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/trumbitta"/>
    <language>en</language>
    <item>
      <title>How to make both npm and yarn use private registries</title>
      <dc:creator>William Ghelfi</dc:creator>
      <pubDate>Wed, 04 Nov 2020 14:46:06 +0000</pubDate>
      <link>https://forem.com/trumbitta/how-to-make-both-npm-and-yarn-use-private-registries-2n6b</link>
      <guid>https://forem.com/trumbitta/how-to-make-both-npm-and-yarn-use-private-registries-2n6b</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm login --registry &amp;lt;YOUR_PRIVATE_REGISTRY_URL&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn config delete registry
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then don't trust the autogenerated &lt;code&gt;~/.npmrc&lt;/code&gt; configuration and modify it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;always-auth=true
registry=&amp;lt;YOUR_PRIVATE_REGISTRY_URL&amp;gt;
_authToken=NpmToken.&amp;lt;YOUR_TOKEN&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

</description>
      <category>webdev</category>
      <category>node</category>
      <category>yarn</category>
      <category>npm</category>
    </item>
    <item>
      <title>A lesson about Continuous Deployment - 10 years in the making</title>
      <dc:creator>William Ghelfi</dc:creator>
      <pubDate>Thu, 22 Oct 2020 08:48:36 +0000</pubDate>
      <link>https://forem.com/trumbitta/a-lesson-about-continuous-deployment-10-years-in-the-making-4205</link>
      <guid>https://forem.com/trumbitta/a-lesson-about-continuous-deployment-10-years-in-the-making-4205</guid>
      <description>&lt;p&gt;~10 years ago &lt;a href="https://twitter.com/JoshuaKerievsky"&gt;Joshua Kerievsky&lt;/a&gt; taught me a lesson that only yesterday I finally came to understand.&lt;/p&gt;

&lt;p&gt;Or at least this is how I now understand his feedback.&lt;/p&gt;

&lt;h1&gt;
  
  
  The lesson
&lt;/h1&gt;

&lt;p&gt;You must do Continuous Deployment / Delivery. If you are not doing it, you are competing in the wrong league.&lt;/p&gt;

&lt;p&gt;If you're not doing CD because the thing you are working on just isn't ready for it, you must be following a plan with the goal of being finally able to start doing CD.&lt;/p&gt;

&lt;p&gt;If you are doing CD, your software is leaner, more flexible, well organized. Your processes are smooth and possibly automated. You have the minimum requirements for taking your chances in A-league.&lt;/p&gt;

&lt;h1&gt;
  
  
  The anecdote
&lt;/h1&gt;

&lt;p&gt;~2010 I was working at a company whose main product was a Java web framework with an Open Source core and some paid-for plugins.&lt;/p&gt;

&lt;p&gt;I was the main frontend developer and also the DevOps guy.&lt;/p&gt;

&lt;p&gt;We demoed the product to a potential investor, and Joshua followed up with an interview as a technical advisor to the investor.&lt;/p&gt;

&lt;p&gt;This is how I remember it:&lt;/p&gt;

&lt;p&gt;Joshua: So how do you guys distribute the product? Are you doing Continuous Deployment?&lt;/p&gt;

&lt;p&gt;William: Well, the product is actually more of a lib, and it most certainly isn't a SaaS, so we c&lt;/p&gt;

&lt;p&gt;Joshua: You should do CD. If you are not doing CD you are competing in the wrong league.&lt;/p&gt;

&lt;p&gt;W: Let me rephrase, the product is&lt;/p&gt;

&lt;p&gt;J: I got it the the first time :) but if you are not doing CD I think it's better - and he turned towards the investor - if we talk again when you are doing it.&lt;/p&gt;

&lt;p&gt;Shortly after, hands were shaken, goodbyes were given, and off we went.&lt;/p&gt;

&lt;p&gt;On our way out of the building, we brainstormed about the feedback we got, and we all agreed we didn't understand how it was even possible to continuously deploy a &lt;strong&gt;lib&lt;/strong&gt;. Maybe he did mean Delivery instead of Deployment? But surely no one would ever abandon GitFlow! But then how...&lt;/p&gt;

&lt;p&gt;One of the cofounders, who had been knowing Joshua for years, then told me: "I know, I don't understand it either. But if he said it, some day we most certainly will."&lt;/p&gt;

&lt;h1&gt;
  
  
  "Some day" was yesterday
&lt;/h1&gt;

&lt;p&gt;Yesterday I started a proposal to abandon GitFlow for some form of trunk-based development at my current company.&lt;/p&gt;

&lt;p&gt;A couple hours later, I stumbled upon &lt;a href="https://medium.com/@JoshuaKerievsky/your-sacred-software-scroll-experiencing-technical-debt-d0562bb93a5e"&gt;this post by Joshua&lt;/a&gt; about technical debt.&lt;/p&gt;

&lt;p&gt;The topic is slightly unrelated, but seeing who the author was made me think about that day of years ago and it finally clicked.&lt;/p&gt;

&lt;p&gt;In the current status of the software development industry, you must be doing Continuous Development / Delivery, &lt;strong&gt;and if you don't, you must be willing to do it&lt;/strong&gt;. Meaning that you must be following some well-thought plan that will finally enable you to do it.&lt;/p&gt;

&lt;p&gt;Or you will be competing in some B-league. And it's fine, but you should at least be aware of the circumstances you find yourself in.&lt;/p&gt;

</description>
      <category>agile</category>
      <category>productivity</category>
    </item>
    <item>
      <title>4 ways of fixing a "select" form field bad practice</title>
      <dc:creator>William Ghelfi</dc:creator>
      <pubDate>Tue, 01 Sep 2020 08:35:43 +0000</pubDate>
      <link>https://forem.com/trumbitta/4-ways-of-fixing-a-select-form-field-bad-practice-2k46</link>
      <guid>https://forem.com/trumbitta/4-ways-of-fixing-a-select-form-field-bad-practice-2k46</guid>
      <description>&lt;h1&gt;
  
  
  The firestarter
&lt;/h1&gt;

&lt;p&gt;Yesterday, a younger colleague of mine and extremely talented designer, shared his latest mockup on our Basecamp.&lt;/p&gt;

&lt;p&gt;As always, it was beatiful and well balanced... until my heart suddenly started to bleed while my inner Caesar was screaming: &lt;em&gt;"Et tu, Brute?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This is what I tweeted shortly after:&lt;/p&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--N_h7svGX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/1273866999995805697/kWbxnOCA_normal.jpg" alt="William Ghelfi profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        William Ghelfi
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        &lt;a class="mentioned-user" href="https://dev.to/trumbitta"&gt;@trumbitta&lt;/a&gt;

      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ir1kO05j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-f95605061196010f91e64806688390eb1a4dbc9e913682e043eb8b1e06ca484f.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      Having a "Choose" on top of a select form field, is like having a wood panel with a "Choose" sticker on top of your socks inside a drawer.
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      08:50 AM - 22 May 2014
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=469399930738835456" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fFnoeFxk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-reply-action-238fe0a37991706a6880ed13941c3efd6b371e4aefe288fe8e0db85250708bc4.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=469399930738835456" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k6dcrOn8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-retweet-action-632c83532a4e7de573c5c08dbb090ee18b348b13e2793175fea914827bc42046.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/like?tweet_id=469399930738835456" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SRQc9lOp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-like-action-1ea89f4b87c7d37465b0eb78d51fcb7fe6c03a089805d7ea014ba71365be5171.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


&lt;h1&gt;
  
  
  Challenge accepted
&lt;/h1&gt;

&lt;p&gt;Over the years, I often found myself having to explain to interns and young colleagues why this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;select&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Choose one...&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"foo"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;First choice&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"bar"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Second choice&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is in fact always a bad practice, and how to avoid it in a bunch of different situations.&lt;/p&gt;

&lt;p&gt;This time I'm trying to explain my opinion on the topic once and for all, really just hoping to end up having a handy link to point people to when needed.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why "Choose one..." is bad
&lt;/h1&gt;

&lt;p&gt;Let's examine the following HTML code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"icecream"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Choose one...&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"banana"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Banana&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"cream"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Cream&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"smurf"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Smurf&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The problem is, that first option doesn't have any purpose which is suitable for an option.&lt;/p&gt;

&lt;p&gt;That first "Choose one..." option is there just to make you choose the actual option you wanted to choose in the first place!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UJLcdWoI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6r1c8n1thhv91g6keuiq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UJLcdWoI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6r1c8n1thhv91g6keuiq.jpg" alt="Madness" width="510" height="247"&gt;&lt;/a&gt;&lt;/p&gt;
This is madness, and I promise you no Spartan ain't going to kick me down a well for writing it.



&lt;p&gt;It doesn't make any sense to have "Choose one..." as an option.&lt;/p&gt;

&lt;p&gt;Making you want to choose an &lt;code&gt;&amp;lt;option&amp;gt;&lt;/code&gt;, is the job of a &lt;code&gt;&amp;lt;label&amp;gt;&lt;/code&gt;. Not of another &lt;code&gt;&amp;lt;option&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  And how to fix it
&lt;/h1&gt;

&lt;h2&gt;
  
  
  The basics
&lt;/h2&gt;

&lt;p&gt;The first rule of the &lt;em&gt;Fight The "Choose one..." Madness Club&lt;/em&gt; is you always talk about it and you also use a &lt;code&gt;&amp;lt;label&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"icecream"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Choose an icecream:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&amp;lt;br&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"icecream"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"icecream"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"banana"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Banana&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"cream"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Cream&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"smurf"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Smurf&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second rule being: if you really, really have to use the word &lt;em&gt;"Choose"&lt;/em&gt;, use it inside the &lt;code&gt;&amp;lt;label&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now that we know the basics, let's see them in action in a couple different scenarios and let's also see how you can choose proper first options for each one of those scenarios.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. A filter-like &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt;, mandatory choice
&lt;/h2&gt;

&lt;p&gt;When the &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt; is used to filter a list of possible items, you nearly always have to include a first &lt;em&gt;show them all&lt;/em&gt; option.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"superheroes"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Show superheroes:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&amp;lt;br&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"superheroes"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"superheroes"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"all"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;All&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"flying"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Flying&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"super-force"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Super-force&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"super-sight"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Super-sight&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Notes:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;The value of the &lt;em&gt;show them all&lt;/em&gt; &lt;code&gt;&amp;lt;option&amp;gt;&lt;/code&gt; could be empty.
It depends on your backend implementation and coding style.&lt;/li&gt;
&lt;li&gt;Since in this example the choice is mandatory, having the &lt;em&gt;show them all&lt;/em&gt; option as the first one perfectly fits the requirement.&lt;/li&gt;
&lt;li&gt;A &lt;em&gt;don't actually filter anything&lt;/em&gt; option is often a good default one.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  2. A filter-like &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt;, not mandatory choice
&lt;/h2&gt;

&lt;p&gt;Ok, I cheated. The first example is valid in both cases.&lt;/p&gt;

&lt;p&gt;It doesn't really matter if the choice is mandatory or not: when you have a filter-like &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt;, you can always avoid using the dreadful "Choose one..." first option. Just stick with the winning &lt;code&gt;&amp;lt;label&amp;gt;&lt;/code&gt; + &lt;em&gt;show them all&lt;/em&gt; first option combo.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. A simple choose-one &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt;, mandatory choice
&lt;/h2&gt;

&lt;p&gt;When the &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt; is used inside a form to make you choose one out of some possible options, things can get tricky for the inexperienced &lt;em&gt;Padawan&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Good for us I'm writing this very blog post!&lt;/p&gt;

&lt;p&gt;Consider this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"opponent"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Choose your opponent:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&amp;lt;br&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"opponent"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"opponent"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"godzilla"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Godzilla&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"optimus_prime"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Optimus Prime&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"shaq"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Shaq&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"batman"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Batman&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"chuck_norris"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Chuck Norris&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"indiana_jones"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Indiana Jones&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If Godzilla is the default opponent you want poor human fighters go against every time, your job here is done.&lt;/p&gt;

&lt;p&gt;But you could also be a bit less evil and let those warriors try themselves against Indiana Jones by default – beware that gun, though!&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/4DzcOCyHDqc"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Nothing easier than that. Just add a &lt;code&gt;selected="selected"&lt;/code&gt; attribute (or just &lt;code&gt;selected&lt;/code&gt;) to the desired &lt;code&gt;&amp;lt;option&amp;gt;&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"opponent"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Choose your opponent:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&amp;lt;br&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"opponent"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"opponent"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"godzilla"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Godzilla&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"optimus_prime"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Optimus Prime&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"shaq"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Shaq&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"batman"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Batman&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"chuck_norris"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Chuck Norris&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"indiana_jones"&lt;/span&gt; &lt;span class="na"&gt;selected=&lt;/span&gt;&lt;span class="s"&gt;"selected"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Indiana Jones&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Et voilà!&lt;/em&gt; Indy is the first opponent.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. A simple choose-one &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt;, not mandatory choice
&lt;/h2&gt;

&lt;p&gt;Now what if the choice is not mandatory, what if I can skip it and walk away without fighting vs anyone?&lt;/p&gt;

&lt;p&gt;You add a &lt;em&gt;Monty Brewster&lt;/em&gt; option and call it a day.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---8Jzu__K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/sj82j7iwwvbdo7j0gcwl.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---8Jzu__K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/sj82j7iwwvbdo7j0gcwl.jpg" alt="None of the above" width="636" height="356"&gt;&lt;/a&gt;&lt;/p&gt;
Sometimes a choice can be a non-choice – Confucius.



&lt;p&gt;Wait, what's a &lt;em&gt;Monty Brewster&lt;/em&gt; option?&lt;/p&gt;

&lt;p&gt;The following is a simple choose-one &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt; with a not mandatory choice, which uses a &lt;em&gt;Monty Brewster&lt;/em&gt; option instead of that bad bad bad "Choose one..." option.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"opponent"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Choose your opponent:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&amp;lt;br&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"opponent-2"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"opponent-2"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;None&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"godzilla"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Godzilla&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"optimus_prime"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Optimus Prime&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"shaq"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Shaq&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"batman"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Batman&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"chuck_norris"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Chuck Norris&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"indiana_jones"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Indiana Jones&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Notes:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Just like before, the value of the &lt;em&gt;Monty Brewster&lt;/em&gt; &lt;code&gt;&amp;lt;option&amp;gt;&lt;/code&gt; can be anything you want. It depends on your backend implementation and coding style.&lt;/li&gt;
&lt;li&gt;If you want to be super-sure that any browser will always render your desired option as the default one, just add the &lt;code&gt;selected="selected"&lt;/code&gt; attribute.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Closing thoughts
&lt;/h2&gt;

&lt;p&gt;As I hope I have demonstrated, it is always possible to avoid using the "Choose one..." first option in a &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Using it is cheap, lazy, confusing to newcomers, and now you have this blog post with examples on how to implement the right thing.&lt;/p&gt;

&lt;p&gt;What do you think about it?&lt;br&gt;
Did you find a use case I forgot to cover?&lt;/p&gt;

&lt;p&gt;Let me know on &lt;a href="https://twitter.com/trumbitta"&gt;Twitter&lt;/a&gt;, or leave a comment!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Originally posted here: &lt;a href="https://www.williamghelfi.com/blog/2014-05-24-select-form-field-bad-practices-and-how-to-fix-it/"&gt;https://www.williamghelfi.com/blog/2014-05-24-select-form-field-bad-practices-and-how-to-fix-it/&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>html</category>
      <category>webdev</category>
      <category>ux</category>
    </item>
    <item>
      <title>React's default way of appending itself to a DOM tree considered harmful</title>
      <dc:creator>William Ghelfi</dc:creator>
      <pubDate>Mon, 24 Aug 2020 22:08:18 +0000</pubDate>
      <link>https://forem.com/trumbitta/react-s-default-way-of-appending-itself-to-a-dom-tree-considered-harmful-36d</link>
      <guid>https://forem.com/trumbitta/react-s-default-way-of-appending-itself-to-a-dom-tree-considered-harmful-36d</guid>
      <description>&lt;h1&gt;
  
  
  For the impatient
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8mbA4Fky--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/695v5wxh3mifq7abn75l.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8mbA4Fky--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/695v5wxh3mifq7abn75l.jpg" alt="Alt Text" width="880" height="587"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Don't do this
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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;rootElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&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;App&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;rootElement&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Do this instead
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role-react-root&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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;rootElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;[data-role-react-root]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;

&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&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;App&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;rootElement&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  What am I talking about?
&lt;/h1&gt;

&lt;p&gt;If you ever dabbled in React or followed even &lt;a href="https://reactjs.org/docs/add-react-to-a-website.html#add-react-in-one-minute"&gt;the most basic of its tutorials&lt;/a&gt;, chances are you'll find the following snippet quite familiar:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- here reduced to the bare minimum for simplicity's sake --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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;rootElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&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;App&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;rootElement&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This &lt;code&gt;&amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;&lt;/code&gt; plus the accompanying JavaScript snippet, or one of their variants, are everywhere: tutorials, official docs, tools like Create React App and Nx, online samples... everywhere.&lt;/p&gt;

&lt;p&gt;Here's why this is bad to the point of being harmful.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why is it harmful
&lt;/h1&gt;

&lt;p&gt;In a nutshell:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ids and classes are meant to be used for styling purposes:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"some-class"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Even better, don't use ids for styling purposes; use them only for navigation:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/somepage#pricing"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Pricing&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;

&lt;span class="c"&gt;&amp;lt;!-- more code --&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;section&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"pricing"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- awesome pricing table --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/section&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;When you need to add special meaning to HTML, use data attributes:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-special&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Such special, much data&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This separation of concerns (&lt;strong&gt;styling&lt;/strong&gt; via &lt;strong&gt;classes&lt;/strong&gt;, &lt;strong&gt;navigation&lt;/strong&gt; via &lt;strong&gt;ids&lt;/strong&gt;, &lt;strong&gt;special meaning&lt;/strong&gt; via &lt;strong&gt;data attributes&lt;/strong&gt;) helps avoiding some harmful situations you most certainly &lt;strong&gt;will&lt;/strong&gt; find yourself in at some point in your career if you keep using &lt;code&gt;&amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;&lt;/code&gt; as a React root element.&lt;/p&gt;

&lt;h2&gt;
  
  
  Harmful situation #1
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Sue creates a new React app.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Bob hops on the project. The design by Alan has a furry pink border for the main container, meaning Bob needs a selector and lo and behold &lt;code&gt;#root&lt;/code&gt; is already there.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nf"&gt;#root&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2rem&lt;/span&gt; &lt;span class="n"&gt;furry&lt;/span&gt; &lt;span class="no"&gt;pink&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;ul&gt;
&lt;li&gt;John the CEO has a vision: furry borders are so 2005! We need to get rid of it!&lt;/li&gt;
&lt;li&gt;John the CEO calls Alan at 5:00 in the morning on a sunday and asks him to change the border ASAP, before his meeting with prospect investors.&lt;/li&gt;
&lt;li&gt;Alan, a designer with good enough CSS skills to get rid of a border, but no JavaScript nor React skill whatsoever, finds the offending CSS rule and deletes it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then, in a rare flash of love for his job, Alan the designer searches the codebase for places where &lt;code&gt;id="root"&lt;/code&gt; is used.&lt;br&gt;&lt;br&gt;
Alan finds our friend &lt;code&gt;&amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
Alan thinks he just removed the one and only reason for it to exist, and deletes the whole line.&lt;/p&gt;

&lt;p&gt;A sip of coffee, a deploy to production, and the job is done just before the important meeting.&lt;/p&gt;

&lt;p&gt;Chaos ensues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Harmful situation #2
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Sue creates a new React app.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Bob starts working on the project, but leaves the company with a ton of work still to be done.&lt;/li&gt;
&lt;li&gt;John the CEO decides to be smart and hires a bunch of junior developers from outer space.&lt;/li&gt;
&lt;li&gt;Xyz, one of the junior devs from outer space, works on a new strategic feature and being the junior that she is, she uses &lt;code&gt;document.getElementById('root')&lt;/code&gt; as the base for the strategic feature.&lt;/li&gt;
&lt;li&gt;Something happens on Twitter and suddenly "root" is not a good word anymore. Sue changes the HTML:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"react-base"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Sue also changes the React initialization code accordingly:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&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;App&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-base&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;ul&gt;
&lt;li&gt;The strategic feature doesn't work anymore.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Chaos ensues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;Both of these harmful situations happened to me multiple times over the last 20 years.&lt;/p&gt;

&lt;p&gt;At first, I was Alan or Xyz, more recently I was Bob or Sue.&lt;/p&gt;

&lt;p&gt;We had the knowledge to avoid it. Something we learnt during the first months of our careers.&lt;/p&gt;

&lt;p&gt;Something some old dude taught us and we forgot.&lt;/p&gt;

&lt;h1&gt;
  
  
  What's the best practice
&lt;/h1&gt;

&lt;p&gt;When working with HTML, CSS, and JavaScript:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For styling purposes, use only classes and element selectors.&lt;/li&gt;
&lt;li&gt;Use ids only for anchors and navigation.&lt;/li&gt;
&lt;li&gt;When you need to get a DOM element via JavaScript, use data attributes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Or, on the other hand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you see a class, assume it's for styling only.&lt;/li&gt;
&lt;li&gt;If you see an id, assume it's for navigation only.&lt;/li&gt;
&lt;li&gt;If you see a data attribute, assume it's used via JavaScript somehow, somewhere.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Separation of concerns.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Works wonders, if you remember to respect it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Image credits
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Cover photo by &lt;a href="https://unsplash.com/@vl_cherkasenko?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Vladyslav Cherkasenko&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/hazard?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Photo by &lt;a href="https://unsplash.com/@jamietempleton?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Jamie Templeton&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/instructions?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

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