<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Dilshan Kelsen's RSS Feed]]></title><description><![CDATA[Tutorials about full-stack web development]]></description><link>https://dilshankelsen.com</link><generator>GatsbyJS</generator><lastBuildDate>Thu, 01 Feb 2024 09:02:57 GMT</lastBuildDate><item><title><![CDATA[Adding E2E Setup With PlayWright And Cucumber In TypeScript Project]]></title><description><![CDATA[In this post, I'll demonstrate how to set up an  end-to-end (E2E) testing framework  using  Cucumber and Playwright  in your TypeScript…]]></description><link>https://dilshankelsen.com/adding-e2e-setup-with-playwright-and-cucumber-in-typescript-project/</link><guid isPermaLink="false">https://dilshankelsen.com/adding-e2e-setup-with-playwright-and-cucumber-in-typescript-project/</guid><pubDate>Mon, 01 May 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In this post, I&amp;#x27;ll demonstrate how to set up an &lt;strong&gt;end-to-end (E2E) testing framework&lt;/strong&gt; using &lt;em&gt;Cucumber and Playwright&lt;/em&gt; in your TypeScript project.&lt;/p&gt;&lt;h2 id=&quot;installing-dependencies&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#installing-dependencies&quot; aria-label=&quot;installing dependencies permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Installing dependencies&lt;/h2&gt;&lt;p&gt;First, add the following libraries as &lt;em&gt;dev dependencies&lt;/em&gt; to your project.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;yarn&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; --dev @cucumber/cucumber @playwright/test &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# OR&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; --save-dev @cucumber/cucumber @playwright/test&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;setting-up-cucumber&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#setting-up-cucumber&quot; aria-label=&quot;setting up cucumber permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Setting up Cucumber&lt;/h2&gt;&lt;p&gt;It should be noted that the testing framework is operated &lt;strong&gt;entirely by Cucumber&lt;/strong&gt;, and Playwright is merely &lt;em&gt;plugged&lt;/em&gt; 🔌 into it. Theoretically, you can &lt;em&gt;switch it out&lt;/em&gt; for any browser automation tool such as &lt;strong&gt;Cypress or Selenium&lt;/strong&gt;.&lt;/p&gt;&lt;h3 id=&quot;features-folder&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#features-folder&quot; aria-label=&quot;features folder permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Features folder&lt;/h3&gt;&lt;p&gt;All your &lt;em&gt;E2E code&lt;/em&gt; will live in a folder named &lt;code&gt;features&lt;/code&gt; at the root of your project.&lt;/p&gt;&lt;h3 id=&quot;feature-file&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#feature-file&quot; aria-label=&quot;feature file permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Feature file&lt;/h3&gt;&lt;p&gt;Inside of your &lt;code&gt;features&lt;/code&gt; folder, you&amp;#x27;ll will have &lt;code&gt;.feature&lt;/code&gt; files which &lt;strong&gt;represent your Cucumber 🥒 files&lt;/strong&gt;. To test the entire setup, let&amp;#x27;s create an &lt;em&gt;example&lt;/em&gt; file.&lt;/p&gt;&lt;div class=&quot;gatsby-code-title code-title-custom&quot;&gt;features/greeting.feature&lt;/div&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;gherkin&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-gherkin&quot;&gt;GHERKIN&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-gherkin&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Feature: Greeting&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  Scenario: Say hello&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    When the greeter says hello&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    Then I should have heard &amp;quot;hello&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;support-file&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#support-file&quot; aria-label=&quot;support file permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Support file&lt;/h3&gt;&lt;p&gt;In the background, your feature files are &lt;em&gt;executed&lt;/em&gt; by TypeScript files located in a &lt;code&gt;steps&lt;/code&gt; folder. Their file names &lt;strong&gt;must correspond to the name of the feature file&lt;/strong&gt; they are executing.&lt;/p&gt;&lt;div class=&quot;gatsby-code-title code-title-custom&quot;&gt;features/steps/greeting.ts&lt;/div&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; When&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Then &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;@cucumber/cucumber&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;MyWorld&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  whatIHeard&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;When&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;the greeter says hello&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; MyWorld&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;whatIHeard &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;hello&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Then&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;I should have heard {string}&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; MyWorld&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  expectedResponse&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Make assertion here&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;configuration-file&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#configuration-file&quot; aria-label=&quot;configuration file permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Configuration file&lt;/h3&gt;&lt;p&gt;What we have so far is &lt;em&gt;enough&lt;/em&gt; to execute an E2E test. However, for that to happen we have to &lt;strong&gt;specify a host of parameters&lt;/strong&gt; in the CLI, such as the Typescript execution engine, the location of the step definitions, etc. We can &lt;em&gt;avoid repeating&lt;/em&gt; 🙏 these parameters on every test run by creating a &lt;code&gt;cucumber.mjs&lt;/code&gt; file &lt;strong&gt;at the root of your project&lt;/strong&gt; that stores theses parameters.&lt;/p&gt;&lt;div class=&quot;gatsby-code-title code-title-custom&quot;&gt;cucumber.mjs&lt;/div&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; configuration &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;requireModule&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;@swc-node/register&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;features/steps/**/*.ts&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Optional below&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;summary&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;progress-bar&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;formatOptions&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;snippetInterface&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;async-await&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; configuration&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;p&gt;Specify a TypeScript execution engine of your choice in &lt;code&gt;requireModule&lt;/code&gt;, such as &lt;code&gt;@swc-node&lt;/code&gt; or &lt;code&gt;ts-node&lt;/code&gt;. Oddly, &lt;code&gt;ts-node&lt;/code&gt; did not work for me. 🤔&lt;/p&gt;&lt;/div&gt;&lt;p&gt;I recommend &lt;em&gt;checking out&lt;/em&gt; the overview of all the &lt;a href=&quot;https://github.com/cucumber/cucumber-js/blob/6505e61abce385787767f270b6ce2077eb3d7c1c/docs/configuration.md#options&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;configuration options&lt;/a&gt; for further reference.&lt;/p&gt;&lt;h2 id=&quot;installing-playwright&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#installing-playwright&quot; aria-label=&quot;installing playwright permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Installing Playwright&lt;/h2&gt;&lt;p&gt;With the current setup in place, you can &lt;strong&gt;already execute&lt;/strong&gt; 😲 the test with &lt;code&gt;yarn/npm run cucumber-js&lt;/code&gt; with a pass. However, it&amp;#x27;s passing &lt;em&gt;because&lt;/em&gt; we aren&amp;#x27;t making any assertions, for which we will use Playwright.&lt;/p&gt;&lt;p&gt;Returning to the step file, make the following changes:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;diff&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-diff&quot;&gt;DIFF&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-diff&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token inserted-sign inserted prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted line&quot;&gt; import { expect } from &amp;quot;@playwright/test&amp;quot;;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token inserted-sign inserted line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;# [...]&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Then(&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token unchanged prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token unchanged line&quot;&gt; &amp;quot;I should have heard {string}&amp;quot;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token unchanged line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token unchanged prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token unchanged line&quot;&gt; function (this: MyWorld, expectedResponse: string) {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token unchanged line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted line&quot;&gt;   expect(this.whatIHeard).toEqual(expectedResponse);&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token inserted-sign inserted line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token unchanged prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token unchanged line&quot;&gt; }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token unchanged line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;);&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To check if the assertion &lt;em&gt;actually works&lt;/em&gt;, simply &lt;strong&gt;change the casing&lt;/strong&gt; of the string &lt;code&gt;&amp;quot;hello&amp;quot;&lt;/code&gt; and you will receive a failed ❌ test after running Cucumber.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;diff&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-diff&quot;&gt;DIFF&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-diff&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token deleted-sign deleted prefix deleted&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token deleted-sign deleted line&quot;&gt; this.whatIHeard = &amp;quot;hello&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token deleted-sign deleted line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted line&quot;&gt; this.whatIHeard = &amp;quot;Hello&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:29.500000000000004%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA50lEQVQY023M226DMAwG4LzMupI4EAiJTcmBQ6HQVl3v9v5vssDai0mTPv36ZVtmrp+GyxKnuR/nNg7Rz217w3Cn9qrNWZVRyObI7b9YLozLERWedOMUdTla4yH2qoq66nQZuTx9cPvrsPt8J8s4jtA80U0hWIGVwJQkrNnZDZKkRhJKskAGUO1nUiA7HGvITACMme0BW7C9pMSnCeAgKWWajxL9tk2vbcFNwa3IDOuGW4gLlU5jgNqLooXSycrvwru8er4JqgrFxrPH1/d9eT7i2i1Xf1lDXHUdOaAAEnIH9OrwBwf6AX9FSrAIthSPAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Failed Cucumber Test&quot; title=&quot;Failed Cucumber Test&quot; src=&quot;/static/772a8427643c4ca8d4f895ef7b4ade7f/5a190/image-1.png&quot; srcSet=&quot;/static/772a8427643c4ca8d4f895ef7b4ade7f/772e8/image-1.png 200w,/static/772a8427643c4ca8d4f895ef7b4ade7f/e17e5/image-1.png 400w,/static/772a8427643c4ca8d4f895ef7b4ade7f/5a190/image-1.png 800w,/static/772a8427643c4ca8d4f895ef7b4ade7f/eafa0/image-1.png 939w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;While you are now capable of running tests, you are nevertheless missing the proper setup to &lt;strong&gt;keep each test independent of each other&lt;/strong&gt;. Check out &lt;a href=&quot;/set-up-custom-world-for-cucumber-playwright-tests-in-typescript/&quot;&gt;my walk-through&lt;/a&gt; on how to do so.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Set Up A Custom World For Cucumber/Playwright Tests In TypeScript]]></title><description><![CDATA[It's important to ensure that each test you run is  independent of each other . Cucumber uses the  concept of a World , which is an isolated…]]></description><link>https://dilshankelsen.com/set-up-custom-world-for-cucumber-playwright-tests-in-typescript/</link><guid isPermaLink="false">https://dilshankelsen.com/set-up-custom-world-for-cucumber-playwright-tests-in-typescript/</guid><pubDate>Mon, 24 Apr 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;It&amp;#x27;s important to ensure that each test you run is &lt;strong&gt;independent of each other&lt;/strong&gt;. Cucumber uses the &lt;a href=&quot;https://github.com/cucumber/cucumber-js/blob/main/docs/support_files/world.md&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;concept of a World&lt;/a&gt;, which is an isolated scope for each scenario, &lt;em&gt;exposed as&lt;/em&gt; &lt;code&gt;this&lt;/code&gt; to each step.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; CustomWorld &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;./custom-world&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Given&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;my color is {string}&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; CustomWorld&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; color&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; pageUrl &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// You may either access a variable set in this unique World,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;color &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; color &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Or set a variable accessible to following steps.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// [...]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;GitHub user &lt;a href=&quot;https://github.com/Tallyb/cucumber-playwright&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;Tallyb&lt;/a&gt; has a good example project that implements a World setup. &lt;em&gt;No need to reinvent&lt;/em&gt; the wheel, yet &lt;strong&gt;improving&lt;/strong&gt; it wouldn&amp;#x27;t hurt. 😁&lt;/p&gt;&lt;h2 id=&quot;hooks&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#hooks&quot; aria-label=&quot;hooks permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Hooks&lt;/h2&gt;&lt;p&gt;There&amp;#x27;s a &lt;a href=&quot;https://github.com/Tallyb/cucumber-playwright&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;configuration&lt;/a&gt; file I &lt;em&gt;won&amp;#x27;t focus on&lt;/em&gt;, as its content is quite straightforward. Rather, we should pay attention to the &lt;a href=&quot;https://github.com/Tallyb/cucumber-playwright/blob/master/src/support/common-hooks.ts&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;hooks file&lt;/a&gt;. This file does the &lt;strong&gt;grunt work&lt;/strong&gt; of launching the test browser, &lt;em&gt;storing traces&lt;/em&gt; (similar to log files) after a test has failed, instantiating variables, etc.&lt;/p&gt;&lt;p&gt;The &lt;code&gt;Before&lt;/code&gt; hook is of particular to interest to us since this is where you would &lt;strong&gt;define any variables&lt;/strong&gt; that you wish to use throughout any scenario.&lt;/p&gt;&lt;div class=&quot;gatsby-code-title code-title-custom&quot;&gt;src/support/common-hooks.ts&lt;/div&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;Before&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; CustomWorldBeforeSetup&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// used to be `ICustomWorld`&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; pickle &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; ITestCaseHookParameter&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;startTime &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;pageUrl &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;https://localhost:3000&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// New variable&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// [...]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;However, TypeScript will complain 👎 if we define any &lt;em&gt;new&lt;/em&gt; variables in this hook. That&amp;#x27;s because it &lt;strong&gt;hasn&amp;#x27;t been defined&lt;/strong&gt; in the &lt;code&gt;CustomWorldBeforeSetup&lt;/code&gt; type. Note that I&amp;#x27;ve replaced the original &lt;code&gt;ICustomWorld&lt;/code&gt; with the aforementioned type. &lt;strong&gt;Read more&lt;/strong&gt; to find out why!&lt;/p&gt;&lt;h2 id=&quot;custom-world&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#custom-world&quot; aria-label=&quot;custom world permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Custom World&lt;/h2&gt;&lt;p&gt;The &amp;quot;World&amp;quot; is nothing more than a &lt;strong&gt;JavaScript class&lt;/strong&gt; that is instantiated on each test run. By default, only &lt;code&gt;debug&lt;/code&gt; is set. I separated the typing of the &amp;quot;Custom World&amp;quot; into &lt;em&gt;two&lt;/em&gt;:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;CustomWorldBeforeSetup&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;CustomWorld&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h3 id=&quot;before-setup&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#before-setup&quot; aria-label=&quot;before setup permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Before setup&lt;/h3&gt;&lt;p&gt;I renamed &lt;code&gt;ICustomWorld&lt;/code&gt; to &lt;code&gt;CustomWorldBeforeSetup&lt;/code&gt;. For each scenario you test, the &lt;code&gt;TestWorld&lt;/code&gt; class will be instantiated &lt;strong&gt;before any of the hooks run&lt;/strong&gt;. This means, unless you declare the variables &lt;em&gt;inside the class itself&lt;/em&gt;, as is the case with &lt;code&gt;debug&lt;/code&gt;, you will mark most properties as &lt;em&gt;optional&lt;/em&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-code-title code-title-custom&quot;&gt;src/support/custom-world.ts&lt;/div&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;/**&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt; * The &amp;quot;custom world&amp;quot; before the setup of a scenario.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt; */&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;CustomWorldBeforeSetup&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;World&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  debug&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Remaining properties are optional&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  feature&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; messages&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Pickle&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  context&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; BrowserContext&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  page&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Page&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  pageUrl&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// New property&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  testName&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  startTime&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Date&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  server&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; APIRequestContext&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  playwrightOptions&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; PlaywrightTestOptions&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;TestWorld&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;World&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;implements&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;CustomWorldBeforeSetup&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; IWorldOptions&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  debug &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setWorldConstructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;TestWorld&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;However, this interface &lt;strong&gt;doesn&amp;#x27;t accurately reflect&lt;/strong&gt; 🙅‍♀️ the custom world &lt;em&gt;after&lt;/em&gt; the entire setup has run. Furthermore, you would have to always &lt;em&gt;check if the variable is defined&lt;/em&gt; since it&amp;#x27;s marked as optional. Hence, why there&amp;#x27;s a &lt;strong&gt;second interface&lt;/strong&gt;.&lt;/p&gt;&lt;h3 id=&quot;after-setup&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#after-setup&quot; aria-label=&quot;after setup permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;After setup&lt;/h3&gt;&lt;p&gt;In this interface, we &lt;strong&gt;know for sure&lt;/strong&gt; that all the listed properties below are &lt;em&gt;defined&lt;/em&gt; since this represents &lt;code&gt;TestWorld&lt;/code&gt; &lt;em&gt;after&lt;/em&gt; the &lt;code&gt;Before&lt;/code&gt; hook has run.&lt;/p&gt;&lt;div class=&quot;gatsby-code-title code-title-custom&quot;&gt;src/support/custom-world.ts&lt;/div&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;/**&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt; * The &amp;quot;custom world&amp;quot; after the setup of a scenario, i.e. with all of its&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt; * properties set.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt; */&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;CustomWorld&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;CustomWorldBeforeSetup&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  feature&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; messages&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Pickle&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  context&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; BrowserContext&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  page&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Page&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  pageUrl&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// New property set in a hook&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  testName&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  startTime&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Date&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  server&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; APIRequestContext&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  color&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// New property set in a step&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Consequently, at any step, &lt;em&gt;assign&lt;/em&gt; &lt;code&gt;this&lt;/code&gt; the &lt;code&gt;CustomWorld&lt;/code&gt; interface, not &lt;code&gt;CustomWorldBeforeSetup&lt;/code&gt;. Lastly, notice 🔔 &lt;code&gt;color&lt;/code&gt; is an &lt;strong&gt;optional property&lt;/strong&gt; as it is set in a scenario, i.e. step, not in a hook as is the case with &lt;code&gt;pageUrl&lt;/code&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[JavaScript Modules Explained]]></title><description><![CDATA[Nowadays, it's natural to  split up  a JavaScript program into multiple files, i.e. modules. However, until the  ES6 revision in 201…]]></description><link>https://dilshankelsen.com/javascript-modules-explained/</link><guid isPermaLink="false">https://dilshankelsen.com/javascript-modules-explained/</guid><pubDate>Mon, 17 Apr 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Nowadays, it&amp;#x27;s natural to &lt;em&gt;split up&lt;/em&gt; a JavaScript program into multiple files, i.e. modules. However, until the &lt;strong&gt;ES6 revision in 2015&lt;/strong&gt;, there was no official support for this. Consequently, &lt;em&gt;unofficial standards&lt;/em&gt; emerged, such as &lt;strong&gt;CommonJS&lt;/strong&gt; in NodeJS. Furthermore, a different standard called &lt;a href=&quot;https://requirejs.org/docs/commonjs.html&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;RequireJS&lt;/a&gt; emerged in the browser since CommonJS wasn&amp;#x27;t designed with browsers in mind.&lt;/p&gt;&lt;p&gt;Consequently, you may have seen &lt;em&gt;import and export statements&lt;/em&gt; such as below, which belong to CommonJS.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; someModule &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;someModule&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;someFunc&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; someModule&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;doSomething&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;modules&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;exports&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; someFunc&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;es-modules&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#es-modules&quot; aria-label=&quot;es modules permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;ES Modules&lt;/h2&gt;&lt;p&gt;With the arrival of the &lt;em&gt;official&lt;/em&gt; ECMAScript module standard, or short &lt;strong&gt;ES Modules&lt;/strong&gt;, one could now accomplish the same thing in a more readable manner.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt;someModule&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;someModule&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;someFunc&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; someModule&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;doSomething&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; someFunc&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;nodejs&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#nodejs&quot; aria-label=&quot;nodejs permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;NodeJS&lt;/h2&gt;&lt;p&gt;By now, NodeJS has added &lt;em&gt;stable support&lt;/em&gt; for ES Modules. However, since CommonJS is the &lt;strong&gt;original module system&lt;/strong&gt; of NodeJS, there is still &lt;em&gt;widespread support&lt;/em&gt; for it in the ecosystem. Consequently, in order to not break support, NodeJS still uses it &lt;strong&gt;by default&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;If you wish to use &lt;strong&gt;ES Modules&lt;/strong&gt;, you can either use it in a &lt;code&gt;.mjs&lt;/code&gt; file, set it as the &lt;em&gt;project default&lt;/em&gt; with &lt;code&gt;&amp;quot;type&amp;quot;: &amp;quot;module&amp;quot;&lt;/code&gt; in &lt;code&gt;package.json&lt;/code&gt; or use &lt;code&gt;--input-type=module&lt;/code&gt; in the CLI. Likewise, NodeJS will treat &lt;code&gt;.cjs&lt;/code&gt; files as CommonJS modules.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Prevent CLS From Image Rendering On Client Side]]></title><description><![CDATA[Cumulative Layout Shift  (CLS) can negatively affect your  user experience  and  SEO results . The most common cause is improper image…]]></description><link>https://dilshankelsen.com/prevent-cls-from-image-rendering-on-client-side/</link><guid isPermaLink="false">https://dilshankelsen.com/prevent-cls-from-image-rendering-on-client-side/</guid><pubDate>Mon, 10 Apr 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;em&gt;Cumulative Layout Shift&lt;/em&gt; (CLS) can negatively affect your &lt;em&gt;user experience&lt;/em&gt; and &lt;strong&gt;SEO results&lt;/strong&gt;. The most common cause is improper image rendering on the client side. Fortunately, this is &lt;em&gt;easy to solve&lt;/em&gt; using the &lt;code&gt;aspect-ratio&lt;/code&gt; &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;CSS property&lt;/a&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-css&quot;&gt;CSS&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-css&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token selector&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;aspect-ratio&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;width / height&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;/* e.g. 1200 / 800 */&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;By using this property, the empty image tag will &lt;em&gt;occupy the necessary height&lt;/em&gt; from the get-go based on the provided aspect ratio and the possible image width. This &lt;strong&gt;prevents a sudden shift&lt;/strong&gt; of content once the image has loaded.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Properly Add Google Analytics in NextJS]]></title><description><![CDATA[In NextJS, Google Analytics should be loaded using the  Script component  which  optimizes loading performance  of third-party scripts. As…]]></description><link>https://dilshankelsen.com/properly-add-google-analytics-in-nextjs/</link><guid isPermaLink="false">https://dilshankelsen.com/properly-add-google-analytics-in-nextjs/</guid><pubDate>Mon, 03 Apr 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In NextJS, Google Analytics should be loaded using the &lt;strong&gt;Script component&lt;/strong&gt; which &lt;em&gt;optimizes loading performance&lt;/em&gt; of third-party scripts. As the script should be &lt;em&gt;loaded on every page&lt;/em&gt; of your app, it&amp;#x27;s recommend to insert it into a &lt;strong&gt;root file&lt;/strong&gt; of your app, such as &lt;code&gt;_app.jsx&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-code-title code-title-custom&quot;&gt;_app.jsx&lt;/div&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Script&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;strategy&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;afterInteractive&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;  &amp;lt;Script id=&amp;quot;google-analytics&amp;quot; strategy=&amp;quot;afterInteractive&amp;quot;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    {`&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      window.dataLayer = window.dataLayer || [];&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      function gtag(){window.dataLayer.push(arguments);}&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      gtag(&amp;#x27;js&amp;#x27;, new Date());&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      gtag(&amp;#x27;config&amp;#x27;, &amp;#x27;GA_MEASUREMENT_ID&amp;#x27;);&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    `}&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;afterInteractive&lt;/code&gt; &lt;strong&gt;strategy option&lt;/strong&gt; loads the script as early as possible &lt;em&gt;after&lt;/em&gt; some hydration on the page has occurred.
Be sure to replace &lt;code&gt;GA_MEASUREMENT_ID&lt;/code&gt; by the measurement ID of your Google Analytics &lt;em&gt;property&lt;/em&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Returning An Empty React Component]]></title><description><![CDATA[If you wish to return an  empty  React component, return either  true ,  false ,  null , or  undefined . These are all  valid React nodes…]]></description><link>https://dilshankelsen.com/returning-empty-react-component/</link><guid isPermaLink="false">https://dilshankelsen.com/returning-empty-react-component/</guid><pubDate>Mon, 27 Mar 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;If you wish to return an &lt;strong&gt;empty&lt;/strong&gt; React component, return either &lt;code&gt;true&lt;/code&gt;, &lt;code&gt;false&lt;/code&gt;, &lt;code&gt;null&lt;/code&gt;, or &lt;code&gt;undefined&lt;/code&gt;. These are all &lt;em&gt;valid React nodes&lt;/em&gt; and are ignored when rendering the DOM.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Is the same thing as:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;A &lt;strong&gt;caveat&lt;/strong&gt; to take note of is that &lt;em&gt;certain &amp;quot;falsy&amp;quot; values&lt;/em&gt; are still rendered by React, such as the number &lt;code&gt;0&lt;/code&gt; or an empty string &lt;code&gt;&amp;quot;&amp;quot;&lt;/code&gt;. Therefore, if you do conditional rendering with the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator, you may &lt;em&gt;run into some surprises&lt;/em&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;messages&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;MessageList&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;messages&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript property-access&quot;&gt;messages&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// When `props.messages.length` is `0`, the number `0` is rendered instead of nothing.&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To &lt;strong&gt;avoid these surprises&lt;/strong&gt;, Kent C. Dodds recommends using &lt;a href=&quot;https://kentcdodds.com/blog/use-ternaries-rather-than-and-and-in-jsx&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;ternaries rather than &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;&lt;/a&gt; in JSX.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;messages&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;MessageList&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;messages&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript property-access&quot;&gt;messages&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword null nil&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Will render `MessageList` only if `props.messages.length` is not 0.&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[React Element vs JSX Element vs React Node vs React Component]]></title><description><![CDATA[Even for  experienced  React developers, the difference between a React/JSX Element, Node and Component can be somewhat elusive at first. So…]]></description><link>https://dilshankelsen.com/react-element-vs-jsx-element-vs-react-node-vs-react-component/</link><guid isPermaLink="false">https://dilshankelsen.com/react-element-vs-jsx-element-vs-react-node-vs-react-component/</guid><pubDate>Mon, 20 Mar 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Even for &lt;em&gt;experienced&lt;/em&gt; React developers, the difference between a React/JSX Element, Node and Component can be somewhat elusive at first. So here&amp;#x27;s a clear &lt;strong&gt;breakdown&lt;/strong&gt; of the terms.&lt;/p&gt;&lt;h3 id=&quot;react-element&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#react-element&quot; aria-label=&quot;react element permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;React Element&lt;/h3&gt;&lt;p&gt;A React Element is the &lt;strong&gt;smallest building block&lt;/strong&gt; of a React application and is an &lt;em&gt;object representation&lt;/em&gt; of a virtual DOM node. The creation of elements are cheap and are done as follows.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// React.createElement(type, [props], [...children])&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; element1 &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;createElement&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;div&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;italic&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Basic Element&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; element2 &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;createElement&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;CustomButton&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;red&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword null nil&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The first argument of the &lt;code&gt;createElement&lt;/code&gt; method describes the &lt;strong&gt;type of the element&lt;/strong&gt;. It can either be a &lt;em&gt;tag&lt;/em&gt; name (such as &amp;#x27;div&amp;#x27; or &amp;#x27;span&amp;#x27;), a React &lt;em&gt;component&lt;/em&gt; or a React &lt;em&gt;fragment&lt;/em&gt;. The 2&lt;sup&gt;nd&lt;/sup&gt; argument are the props of the element while the 3&lt;sup&gt;rd&lt;/sup&gt; its children.&lt;/p&gt;&lt;p&gt;In the end, &lt;code&gt;createElement&lt;/code&gt; returns a plain JavaScript object that describe a virtual DOM node, which is &lt;strong&gt;not to be confused&lt;/strong&gt; with a regular DOM node. For instance, &lt;code&gt;element1&lt;/code&gt; returns:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;element1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;/**&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt; *  {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt; *    $$typeof: [object Symbol] { ... },&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt; *    _owner: null,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt; *    _store: { ... },&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt; *    key: null,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt; *    props: {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt; *      children: &amp;quot;Basic Element&amp;quot;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt; *      className: &amp;quot;italic&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt; *    },&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt; *    ref: null,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt; *    type: &amp;quot;div&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt; *  }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt; **/&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;jsx-element&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#jsx-element&quot; aria-label=&quot;jsx element permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;JSX Element&lt;/h3&gt;&lt;p&gt;A JSX Element is really nothing else than &lt;strong&gt;syntactic sugar&lt;/strong&gt; for &lt;code&gt;createElement&lt;/code&gt;. The 2 previous examples can be &lt;em&gt;rewritten with JSX&lt;/em&gt;, which ultimately results in the same thing, i.e. plain JavaScript objects.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; element &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;italic&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Basic Element&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; element &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;CustomButton&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;react-component&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#react-component&quot; aria-label=&quot;react component permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;React Component&lt;/h3&gt;&lt;p&gt;A React component is a function that &lt;em&gt;returns one or more&lt;/em&gt; React Elements. Nothing more, nothing less.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function maybe-class-name&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; children &lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;italic&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;children&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;However, once we &amp;quot;call&amp;quot; this component using JSX syntax, it&amp;#x27;s &lt;em&gt;actually&lt;/em&gt; an Element of type &lt;strong&gt;React component&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Greetings!&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Which is the same as React.createElement(Text, null, &amp;quot;Greetings!&amp;quot;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;react-node&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#react-node&quot; aria-label=&quot;react node permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;React Node&lt;/h3&gt;&lt;p&gt;Lastly, a React Node is a more &lt;strong&gt;general term&lt;/strong&gt; for anything that is returned by a component, which &lt;em&gt;may be more&lt;/em&gt; than an Element. React nodes may be:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;A React element created like &lt;code&gt;&amp;lt;div /&amp;gt;&lt;/code&gt; or &lt;code&gt;createElement(&amp;#x27;div&amp;#x27;)&lt;/code&gt;&lt;/li&gt;&lt;li&gt;A portal created with &lt;code&gt;createPortal&lt;/code&gt;&lt;/li&gt;&lt;li&gt;A string&lt;/li&gt;&lt;li&gt;A number&lt;/li&gt;&lt;li&gt;&lt;code&gt;true&lt;/code&gt;, &lt;code&gt;false&lt;/code&gt;, &lt;code&gt;null&lt;/code&gt;, or &lt;code&gt;undefined&lt;/code&gt; (which are not displayed)&lt;/li&gt;&lt;li&gt;An array of other React nodes&lt;/li&gt;&lt;/ul&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function maybe-class-name&quot;&gt;Number&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Woah! &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;world&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// This return statement of `Number` is considered a React node.&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Add URL Parameters Using NextJS]]></title><description><![CDATA[NextJS makes it  easy  to add URL parameters with  next/router . The  router object , returned by the  useRouter  hook, accepts a URL of…]]></description><link>https://dilshankelsen.com/add-url-parameters-using-nextjs/</link><guid isPermaLink="false">https://dilshankelsen.com/add-url-parameters-using-nextjs/</guid><pubDate>Mon, 13 Mar 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;NextJS makes it &lt;em&gt;easy&lt;/em&gt; to add URL parameters with &lt;strong&gt;next/router&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; router &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useRouter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;em&gt;router object&lt;/em&gt;, returned by the &lt;code&gt;useRouter&lt;/code&gt; hook, accepts a URL of type &lt;code&gt;UrlObject&lt;/code&gt;, which is a &lt;a href=&quot;https://nodejs.org/api/url.html#urlobjectquery&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;NodeJS module&lt;/a&gt;. In particular, the &lt;code&gt;query&lt;/code&gt; property accepts an object which is &lt;em&gt;transformed&lt;/em&gt; into a &lt;strong&gt;URL query string&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// `.push` is used as an example. This work with `.replace` and `.prefetch` as well.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;router&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Path of the URL&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;pathname&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;/menu&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;fruit&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;banana&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;softDrink&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;fanta&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The example above would yield the &lt;em&gt;following result&lt;/em&gt;: &lt;code&gt;/menu?fruit=banana&amp;amp;softDrink=fanta&lt;/code&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Running Automated Scripts In DigitalOcean]]></title><description><![CDATA[To  automate  any command in DigitalOcean, you need to find the  Build and Run Command  section of your project. These instructions are for…]]></description><link>https://dilshankelsen.com/running-automated-scripts-after-build-on-digitalocean/</link><guid isPermaLink="false">https://dilshankelsen.com/running-automated-scripts-after-build-on-digitalocean/</guid><pubDate>Mon, 06 Mar 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;To &lt;strong&gt;automate&lt;/strong&gt; any command in DigitalOcean, you need to find the &lt;a href=&quot;https://docs.digitalocean.com/glossary/command/&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;Build and Run Command&lt;/a&gt; section of your project.&lt;/p&gt;&lt;div&gt;&lt;p&gt;These instructions are for an App Platform project.&lt;/p&gt;&lt;/div&gt;&lt;p&gt;In your project, head to the &lt;strong&gt;Settings page&lt;/strong&gt; and select the &lt;em&gt;component&lt;/em&gt; (e.g. React app) to which you wish to add a command. You should then see a &lt;code&gt;Commands&lt;/code&gt; section further down in the page.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Truncate Multiple Lines Of Text With CSS]]></title><description><![CDATA[Truncating, for example, any text beyond  the 2 nd  line  is fairly  simple  with the following properties. Property  -webkit-line-clamp…]]></description><link>https://dilshankelsen.com/truncate-multiple-lines-of-text-with-css/</link><guid isPermaLink="false">https://dilshankelsen.com/truncate-multiple-lines-of-text-with-css/</guid><pubDate>Mon, 27 Feb 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Truncating, for example, any text beyond &lt;em&gt;the 2&lt;sup&gt;nd&lt;/sup&gt; line&lt;/em&gt; is fairly &lt;strong&gt;simple&lt;/strong&gt; with the following properties.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-css&quot;&gt;CSS&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-css&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token selector class&quot;&gt;.text-box&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; -webkit-box&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;-webkit-box-orient&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; vertical&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;-webkit-line-clamp&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;/* From which line on to truncate */&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;overflow&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; hidden&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Property &lt;code&gt;-webkit-line-clamp&lt;/code&gt; does most of the magic, but needs to be used &lt;em&gt;in combination with&lt;/em&gt; the properties above to work. This solution enjoys &lt;strong&gt;strong support&lt;/strong&gt; among browsers, which makes it the &lt;em&gt;most desirable&lt;/em&gt; method of achieving multiline truncation.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Is ForwardRef Necessary To Pass Down A React Ref]]></title><description><![CDATA[Whenever you wish to pass down a  ref  to a child component, React  recommends  to use  forwardRef . But why? Actually,  you don't have to…]]></description><link>https://dilshankelsen.com/is-forwardref-necessary-to-pass-down-react-ref/</link><guid isPermaLink="false">https://dilshankelsen.com/is-forwardref-necessary-to-pass-down-react-ref/</guid><pubDate>Thu, 23 Feb 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Whenever you wish to pass down a &lt;code&gt;ref&lt;/code&gt; to a child component, React &lt;em&gt;recommends&lt;/em&gt; to use &lt;code&gt;forwardRef&lt;/code&gt;. But why? Actually, &lt;strong&gt;you don&amp;#x27;t have to&lt;/strong&gt;. As stated 📝 in React&amp;#x27;s (older) &lt;a href=&quot;https://reactjs.org/docs/forwarding-refs.html&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;official documentation&lt;/a&gt; about forwarding refs:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;Ref forwarding is a technique for automatically passing a ref through a component to one of its children. This is typically not necessary for most components in the application. However, it can be useful for some kinds of components, especially in reusable component libraries.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Therefore, you may pass down a &lt;code&gt;ref&lt;/code&gt; using &lt;em&gt;any prop name&lt;/em&gt;, e.g. &lt;code&gt;customRef={ref}&lt;/code&gt;. There is no difference in terms of functionality. However, if you &lt;em&gt;attempt&lt;/em&gt; to pass it down using the prop name &lt;code&gt;ref&lt;/code&gt;, you &lt;strong&gt;must use&lt;/strong&gt; &lt;code&gt;forwardRef&lt;/code&gt;, otherwise you&amp;#x27;ll &lt;strong&gt;receive an error&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;So why is &lt;code&gt;ref&lt;/code&gt; a &lt;em&gt;reserved prop name&lt;/em&gt;? 🤔 The answer is to &lt;strong&gt;create a standardized and predictable API&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;For instance, if you maintain a library, &lt;em&gt;developers would need to be aware&lt;/em&gt; how they can pass down a &lt;code&gt;ref&lt;/code&gt; to your component. Keep in mind, this would require &lt;strong&gt;additional documentation&lt;/strong&gt;. In fact, this was the case before the hook&amp;#x27;s introduction in &lt;a href=&quot;https://github.com/facebook/react/blob/main/CHANGELOG.md#1630-march-29-2018&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;React 16.3&lt;/a&gt;, when &lt;code&gt;innerRef&lt;/code&gt; had become a widely used convention.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Load Balancer Briefly Explained]]></title><description><![CDATA[A load balancer is a  hardware or software  unit that  intercepts and distributes  network traffic to a group of servers. It allows the…]]></description><link>https://dilshankelsen.com/load-balancer-briefly-explained/</link><guid isPermaLink="false">https://dilshankelsen.com/load-balancer-briefly-explained/</guid><pubDate>Mon, 20 Feb 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A load balancer is a &lt;em&gt;hardware or software&lt;/em&gt; unit that &lt;strong&gt;intercepts and distributes&lt;/strong&gt; network traffic to a group of servers. It allows the application to scale up or down by being aware of which servers are under- or overutilized, and routes user traffic accordingly.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:54%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA2klEQVQoz6VR0Q6CMAz0/79OjcYHM8RJRBaQgRsy1hbnZowiaqKXPTRde3e7Tfo/MLlX5M9HUFI0LNNIL8vDQaIHWk+NmNcmlS0NlRGnLOYif8cFAI5OtbZQlh6VQz3n+11R9v5GaV3KMkzo7th0NSIRQixOy6QCfG+7M92WcxZF1lpHnNaryqTBO1BvYBCYV4vZQqRbV17tKZXntydUjWhM7Zad8iY7zbh8Ug7ZROvFPmHPUQUjFgB9k1pLyuDIV7kBQBpL+97Bg2x5cf7+VaPI5DkWmn5bHuACFPeIiitdLlQAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Load Balancer Diagram&quot; title=&quot;Load Balancer Diagram&quot; src=&quot;/static/54e63595c7e6cf0000c21d9b764b7de6/5a190/image-1.png&quot; srcSet=&quot;/static/54e63595c7e6cf0000c21d9b764b7de6/772e8/image-1.png 200w,/static/54e63595c7e6cf0000c21d9b764b7de6/e17e5/image-1.png 400w,/static/54e63595c7e6cf0000c21d9b764b7de6/5a190/image-1.png 800w,/static/54e63595c7e6cf0000c21d9b764b7de6/9c177/image-1.png 880w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;As found on the &lt;a href=&quot;https://github.com/karanpratapsingh/system-design#load-balancing&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;GitHub page of karanpratapsingh&lt;/a&gt;.&lt;/figcaption&gt;&lt;p&gt;For instance, &lt;em&gt;if&lt;/em&gt; a server were to drop out, the load balancer would be responsible for &lt;strong&gt;redirecting&lt;/strong&gt; traffic to servers that are capable of serving them.&lt;/p&gt;&lt;p&gt;The importance of load balancers becomes particularly important during traffic spikes.&lt;/p&gt;&lt;h2 id=&quot;traffic-distribution&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#traffic-distribution&quot; aria-label=&quot;traffic distribution permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Traffic distribution&lt;/h2&gt;&lt;p&gt;The distribution of traffic happens with the use of algorithms, with each having their own &lt;em&gt;pros and cons&lt;/em&gt;. &lt;a href=&quot;https://www.cloudflare.com/learning/performance/types-of-load-balancing-algorithms/&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;Common examples&lt;/a&gt; of algorithms are:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Distributing traffic load sequentially on every server (&lt;strong&gt;Round Robin&lt;/strong&gt;),&lt;/li&gt;&lt;li&gt;Using the IP address of the client (&lt;strong&gt;IP Hash&lt;/strong&gt;), or&lt;/li&gt;&lt;li&gt;Randomizing the distribution (&lt;strong&gt;Power of Two Choices&lt;/strong&gt;).&lt;/li&gt;&lt;/ul&gt;</content:encoded></item><item><title><![CDATA[UseImperativeHandle Hook Easily Explained]]></title><description><![CDATA[What does the  useImperativeHandle  hook do? In the simplest terms, it allows you to  control what is returned  by a  ref  prop…]]></description><link>https://dilshankelsen.com/useimperativehandle-hook-easily-explained/</link><guid isPermaLink="false">https://dilshankelsen.com/useimperativehandle-hook-easily-explained/</guid><pubDate>Thu, 16 Feb 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;What does the &lt;code&gt;useImperativeHandle&lt;/code&gt; hook do? In the simplest terms, it allows you to &lt;strong&gt;control what is returned&lt;/strong&gt; by a &lt;code&gt;ref&lt;/code&gt; prop, specifically &lt;em&gt;the&lt;/em&gt; &lt;code&gt;ref&lt;/code&gt; &lt;em&gt;handle&lt;/em&gt;. The handle is usually &lt;strong&gt;a DOM node&lt;/strong&gt;, but by using this hook, the handler would instead be replaced by an &lt;em&gt;object with methods&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;In what situations&lt;/strong&gt; would you use this? As far as I can tell, its main use case is to &lt;a href=&quot;https://beta.reactjs.org/learn/manipulating-the-dom-with-refs#exposing-a-subset-of-the-api-with-an-imperative-handle&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;restrict access to the DOM&lt;/a&gt;. Imagine you are &lt;em&gt;the creator&lt;/em&gt; of a form &lt;strong&gt;library&lt;/strong&gt;. You would want to maintain strong control over the form elements and &lt;em&gt;prevent users&lt;/em&gt; from unnecessarily manipulating the DOM. It should be noted that this hook is &lt;em&gt;quite advanced&lt;/em&gt; in its use case and is therefore &lt;strong&gt;rarely used&lt;/strong&gt;.&lt;/p&gt;&lt;h2 id=&quot;common-example&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#common-example&quot; aria-label=&quot;common example permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Common Example&lt;/h2&gt;&lt;p&gt;Take the example of an input field for which you &lt;strong&gt;solely expose the ability to focus&lt;/strong&gt; it. The &lt;strong&gt;1&lt;sup&gt;st&lt;/sup&gt;&lt;/strong&gt; argument of &lt;code&gt;useImperativeHandle&lt;/code&gt; &lt;em&gt;accepts the ref passed in&lt;/em&gt; as a prop from the parent component. The &lt;strong&gt;2&lt;sup&gt;nd&lt;/sup&gt;&lt;/strong&gt; argument &lt;em&gt;accepts the new handler&lt;/em&gt;, i.e. a JavaScript object with custom methods. Lastly, the &lt;strong&gt;3&lt;sup&gt;rd&lt;/sup&gt;&lt;/strong&gt; argument &lt;em&gt;takes in an array of dependencies&lt;/em&gt;, the same as many other React hooks.&lt;/p&gt;&lt;div code=&quot;const MyInput = React.forwardRef((props, ref) =&amp;gt; {
  const realInputRef = React.useRef(null)

  React.useImperativeHandle(
    ref, // Target the ref passed in by the parent.
    () =&amp;gt; ({
      // Only expose focus and nothing else.
      focus() {
        realInputRef.current.focus()
      },
    }),
    [] // Add any dependencies the hook relies on.
  )

  return &amp;lt;input {...props} ref={realInputRef} /&amp;gt;
})

function Form() {
  const inputRef = React.useRef(null)

  function handleClick() {
    // `current` contains a JavaScript object, not a DOM node.
    inputRef.current.focus()
  }

  return (
    &amp;lt;&amp;gt;
      &amp;lt;MyInput ref={inputRef} /&amp;gt; {/* Remove the ref and see what happens! */}
      &amp;lt;button onClick={handleClick}&amp;gt;Focus the input&amp;lt;/button&amp;gt;
    &amp;lt;/&amp;gt;
  )
}

render(&amp;lt;Form /&amp;gt;)&quot; language=&quot;jsx&quot; class=&quot;live-highlight&quot; style=&quot;position:relative;text-align:left;box-sizing:border-box;padding:0;overflow:hidden;white-space:pre;font-family:monospace&quot;&gt;&lt;textarea style=&quot;margin:0;border:0;background:none;box-sizing:inherit;display:inherit;font-family:inherit;font-size:inherit;font-style:inherit;font-variant-ligatures:inherit;font-weight:inherit;letter-spacing:inherit;line-height:inherit;tab-size:inherit;text-indent:inherit;text-rendering:inherit;text-transform:inherit;white-space:pre-wrap;word-break:keep-all;overflow-wrap:break-word;position:absolute;top:0;left:0;height:100%;width:100%;resize:none;color:inherit;overflow:hidden;-moz-osx-font-smoothing:grayscale;-webkit-font-smoothing:antialiased;-webkit-text-fill-color:transparent;padding-top:10px;padding-right:10px;padding-bottom:10px;padding-left:10px&quot; class=&quot;npm__react-simple-code-editor__textarea&quot; autoCapitalize=&quot;off&quot; autoComplete=&quot;off&quot; autoCorrect=&quot;off&quot; spellcheck=&quot;false&quot; data-gramm=&quot;false&quot;&gt;const MyInput = React.forwardRef((props, ref) =&amp;gt; {
  const realInputRef = React.useRef(null)

  React.useImperativeHandle(
    ref, // Target the ref passed in by the parent.
    () =&amp;gt; ({
      // Only expose focus and nothing else.
      focus() {
        realInputRef.current.focus()
      },
    }),
    [] // Add any dependencies the hook relies on.
  )

  return &amp;lt;input {...props} ref={realInputRef} /&amp;gt;
})

function Form() {
  const inputRef = React.useRef(null)

  function handleClick() {
    // `current` contains a JavaScript object, not a DOM node.
    inputRef.current.focus()
  }

  return (
    &amp;lt;&amp;gt;
      &amp;lt;MyInput ref={inputRef} /&amp;gt; {/* Remove the ref and see what happens! */}
      &amp;lt;button onClick={handleClick}&amp;gt;Focus the input&amp;lt;/button&amp;gt;
    &amp;lt;/&amp;gt;
  )
}

render(&amp;lt;Form /&amp;gt;)&lt;/textarea&gt;&lt;pre aria-hidden=&quot;true&quot; style=&quot;margin:0;border:0;background:none;box-sizing:inherit;display:inherit;font-family:inherit;font-size:inherit;font-style:inherit;font-variant-ligatures:inherit;font-weight:inherit;letter-spacing:inherit;line-height:inherit;tab-size:inherit;text-indent:inherit;text-rendering:inherit;text-transform:inherit;white-space:pre-wrap;word-break:keep-all;overflow-wrap:break-word;position:relative;pointer-events:none;padding-top:10px;padding-right:10px;padding-bottom:10px;padding-left:10px&quot;&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token keyword&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;MyInput&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;forwardRef&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot; style=&quot;opacity:0.7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; ref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; realInputRef &lt;/span&gt;&lt;span class=&quot;token operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;useRef&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword null nil&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot; style=&quot;display:inline-block&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;useImperativeHandle&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    ref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot; style=&quot;color:hsl(30, 20%, 50%)&quot;&gt;// Target the ref passed in by the parent.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token comment&quot; style=&quot;color:hsl(30, 20%, 50%)&quot;&gt;// Only expose focus and nothing else.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;focus&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        realInputRef&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;focus&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot; style=&quot;color:hsl(30, 20%, 50%)&quot;&gt;// Add any dependencies the hook relies on.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot; style=&quot;display:inline-block&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag spread punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag spread operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token tag spread&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;token tag spread punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot; style=&quot;color:hsl(75, 70%, 60%)&quot;&gt;ref&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;realInputRef&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot; style=&quot;display:inline-block&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function maybe-class-name&quot;&gt;Form&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; inputRef &lt;/span&gt;&lt;span class=&quot;token operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;useRef&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword null nil&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot; style=&quot;display:inline-block&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;handleClick&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot; style=&quot;color:hsl(30, 20%, 50%)&quot;&gt;// `current` contains a JavaScript object, not a DOM node.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    inputRef&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;focus&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot; style=&quot;display:inline-block&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;MyInput&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot; style=&quot;color:hsl(75, 70%, 60%)&quot;&gt;ref&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;inputRef&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token comment&quot; style=&quot;color:hsl(30, 20%, 50%)&quot;&gt;/* Remove the ref and see what happens! */&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot; style=&quot;color:hsl(75, 70%, 60%)&quot;&gt;onClick&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;handleClick&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Focus the input&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot; style=&quot;display:inline-block&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;Form&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;style type=&quot;text/css&quot;&gt;
/**
 * Reset the text fill color so that placeholder is visible
 */
.npm__react-simple-code-editor__textarea:empty {
  -webkit-text-fill-color: inherit !important;
}

/**
 * Hack to apply on some CSS on IE10 and IE11
 */
@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
  /**
    * IE doesn&apos;t support &apos;-webkit-text-fill-color&apos;
    * So we use &apos;color: transparent&apos; to make the text transparent on IE
    * Unlike other browsers, it doesn&apos;t affect caret color in IE
    */
  .npm__react-simple-code-editor__textarea {
    color: transparent !important;
  }

  .npm__react-simple-code-editor__textarea::selection {
    background-color: #accef7 !important;
    color: transparent !important;
  }
}
&lt;/style&gt;&lt;/div&gt;&lt;div class=&quot;live-preview&quot;&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Get Local Time From UTC Timestamp In JavaScript]]></title><description><![CDATA[If you've got a  UTC timestamp  string, turn it into a  Date object , and JavaScript will automatically convert it to your  local  timezone…]]></description><link>https://dilshankelsen.com/get-local-time-from-utc-timestamp-in-javascript/</link><guid isPermaLink="false">https://dilshankelsen.com/get-local-time-from-utc-timestamp-in-javascript/</guid><pubDate>Mon, 13 Feb 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;If you&amp;#x27;ve got a &lt;strong&gt;UTC timestamp&lt;/strong&gt; string,&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; utcString &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;toISOString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;utcString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// &amp;quot;2022-10-30T15:15:41.471Z&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;turn it into a &lt;em&gt;Date object&lt;/em&gt;, and JavaScript will automatically convert it to your &lt;strong&gt;local&lt;/strong&gt; timezone.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; localTime &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;utcString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;localTime&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// `Date` Sun Oct 30 2022 16:15:41 GMT+0100 (Mitteleuropäische Normalzeit)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;For a &lt;em&gt;string representation&lt;/em&gt; of the date, use the &lt;code&gt;toLocaleString&lt;/code&gt; method.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; localeString &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; localTime&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;toLocaleString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;localeString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// &amp;quot;30.10.2022, 16:15:41&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Learn How To Use Vuetify’s Grid System]]></title><description><![CDATA[Vuetify’s grid system is built on top of Flexbox, a CSS3 web layout model, which makes responsive web design a breeze. Those of you who are…]]></description><link>https://dilshankelsen.com/learn-vuetify-grid-system/</link><guid isPermaLink="false">https://dilshankelsen.com/learn-vuetify-grid-system/</guid><pubDate>Wed, 08 Feb 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Vuetify’s grid system is built on top of Flexbox, a CSS3 web layout model, which makes responsive web design a breeze. Those of you who are already familiar with Bootstrap‘s grid system will recognize many of the concepts below. However, if you aren’t, stress not! For you will learn how to use the Vuetify’s grid system in no time after reading this tutorial. 😉&lt;/p&gt;&lt;div&gt;&lt;p&gt;This post covers &lt;strong&gt;version 1 and 2&lt;/strong&gt; of Vuetify, and by extension &lt;strong&gt;version 3&lt;/strong&gt; since there have been &lt;a href=&quot;https://next.vuetifyjs.com/en/getting-started/upgrade-guide/&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;no changes&lt;/a&gt; to the Grid API.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;contain-those-grids-bruh&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#contain-those-grids-bruh&quot; aria-label=&quot;contain those grids bruh permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Contain those grids, bruh…&lt;/h2&gt;&lt;p&gt;Before we start talking about all the nitty gritty of the system, it’s important to understand that any grid system you create will usually be placed inside a container. In the case of Vuetify, it’s the &lt;code&gt;&amp;lt;v-container&amp;gt;&lt;/code&gt; tags, which will automatically center itself on the page. You may even use the &lt;code&gt;fluid&lt;/code&gt; prop, like so &lt;code&gt;&amp;lt;v-container fluid&amp;gt;&lt;/code&gt;, to fully extend its width. ↔&lt;/p&gt;&lt;h2 id=&quot;its-time-to-col-and-row&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#its-time-to-col-and-row&quot; aria-label=&quot;its time to col and row permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;It’s time to col and row&lt;/h2&gt;&lt;p&gt;Vuetify’s grid system is made up of a 12 point system, meaning that each row in your grid is split equally into &lt;em&gt;a maximum&lt;/em&gt; of 12 columns. Each row inside &lt;code&gt;&amp;lt;v-container&amp;gt;&lt;/code&gt; is delineated with &lt;code&gt;&amp;lt;v-row&amp;gt;&lt;/code&gt; (or &lt;code&gt;&amp;lt;v-layout row&amp;gt;&lt;/code&gt; in v1.x) tags. By default, space is maintained between all the columns, i.e. 24px to be exact. You may reduce this space with the prop &lt;code&gt;dense&lt;/code&gt;, or even eliminate it with &lt;code&gt;no-gutters&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;After creating a grid row, it’s time to fill it with content using &lt;code&gt;&amp;lt;v-col&amp;gt;&lt;/code&gt; (or &lt;code&gt;&amp;lt;v-flex&amp;gt;&lt;/code&gt; in v1.x), which are *spoilers* your columns. 😛 You can imagine them as a box or element of content (which I call henceforth as &lt;em&gt;flex unit&lt;/em&gt;) that can occupy at least 1 column in said row.&lt;/p&gt;&lt;p&gt;Vuetify has &lt;a href=&quot;https://vuetifyjs.com/en/components/grids/#usage&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;its own set of breakpoints&lt;/a&gt; that you can use to determine the size of a flex unit depending on the screen size. For instance, inserting &lt;code&gt;cols=&amp;quot;12&amp;quot;&lt;/code&gt; as a prop means that the flex unit will occupy 12 columns in that row for all screen sizes by default. On the other hand, &lt;code&gt;md=&amp;quot;6&amp;quot;&lt;/code&gt; would render the flex unit 6 columns wide on a medium-sized screen and larger.&lt;/p&gt;&lt;p&gt;A code snippet can be seen below, in which two flex units are occupying 6 columns each for breakpoint &lt;code&gt;md&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;If you’d like to, you can play around with the code &lt;a href=&quot;https://codepen.io/dkelsen/pen/eYzQRYR&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-container&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-row&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;no-gutters&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;md&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      ...&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;md&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      ...&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-row&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-container&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:6.999999999999999%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAABCAYAAADeko4lAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAPklEQVQI12P4W17+/19q6v9/aWkY+C8Q/09P+/85Ie3/IrOs/5M1Uv5P00r5P1UTFU/TSv0/WTXx/0LH0v8ATtQxgkfk7sgAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Two elements in row&quot; title=&quot;Two elements in row&quot; src=&quot;/static/4b068b01d5a6a9e069a1c7608b593818/5a190/figure-1.png&quot; srcSet=&quot;/static/4b068b01d5a6a9e069a1c7608b593818/772e8/figure-1.png 200w,/static/4b068b01d5a6a9e069a1c7608b593818/e17e5/figure-1.png 400w,/static/4b068b01d5a6a9e069a1c7608b593818/5a190/figure-1.png 800w,/static/4b068b01d5a6a9e069a1c7608b593818/914ae/figure-1.png 860w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;&lt;strong&gt;Figure 1. &lt;/strong&gt;Two flex units occupying 6 columns&lt;/figcaption&gt;&lt;h2 id=&quot;wrap-to-the-rescue&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#wrap-to-the-rescue&quot; aria-label=&quot;wrap to the rescue permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Wrap to the rescue!&lt;/h2&gt;&lt;p&gt;So you might be asking yourself &lt;em&gt;“What happens if I exceed 12 columns in total?”&lt;/em&gt;. 🤔&lt;/p&gt;&lt;p&gt;Well, in that scenario Vuetify will wrap any excess columns into a new line. For example, in Figure 2, the 1&lt;sup&gt;st&lt;/sup&gt; flex unit occupies 6 columns while the 2&lt;sup&gt;nd&lt;/sup&gt; one occupies 8, thus putting the total at 14. As the 2&lt;sup&gt;nd&lt;/sup&gt; column can’t fit in the existing line, it is pushed down to a new one.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:574px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:19.499999999999996%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAYAAACOXx+WAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAmklEQVQY02P4X1L8/39qCin43/+U5P//c7K//5/UrXi/to+hN3Y2w+b0CQw7imYwMPyIif//08vv/08ff2Lxv59evv9/hIT/eNY8wX77ktOiZ6eskz85dZPs2bnbZRhmqCX87+H1JQX/g9B+/zp4A74D8TcQbufy+9EnGfmaYY514f9Jakmk4n8gerJqIhhPAYkpJ/yfYZD5FQBBpbTJ33JODwAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Children wrapping in the container&quot; title=&quot;Children wrapping in the container&quot; src=&quot;/static/5a1365e680c0d27ac30eeeff0c9a534a/86389/figure-2.png&quot; srcSet=&quot;/static/5a1365e680c0d27ac30eeeff0c9a534a/772e8/figure-2.png 200w,/static/5a1365e680c0d27ac30eeeff0c9a534a/e17e5/figure-2.png 400w,/static/5a1365e680c0d27ac30eeeff0c9a534a/86389/figure-2.png 574w&quot; sizes=&quot;(max-width: 574px) 100vw, 574px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;&lt;strong&gt;Figure 2. &lt;/strong&gt;Flex unit #2 pushed into a new line&lt;/figcaption&gt;&lt;p&gt;Not that hard, is it? You may play around with the code &lt;a href=&quot;https://codepen.io/dkelsen/pen/KKMrqQv&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt; and try this out with more than 2 flex units.&lt;/p&gt;&lt;p&gt;In case you&amp;#x27;re interested in some &lt;em&gt;ancient&lt;/em&gt; history, in the good old (rough) days of Vueitfy v1.x this wasn&amp;#x27;t the case. The grid system would actually attempt to squeeze all your flex units into one single row, giving some undesired results. Crazy shit, let me tell you. However, it could be solved with the prop &lt;code&gt;wrap&lt;/code&gt; in &lt;code&gt;&amp;lt;v-layout&amp;gt;&lt;/code&gt;. Thank God (and the developers) for these changes. Makes life easier for us, doesn&amp;#x27;t it? 😉&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:400px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:119.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAYABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAIDAQT/xAAXAQADAQAAAAAAAAAAAAAAAAAAAQID/9oADAMBAAIQAxAAAAGpnaNMnCWkFz5wH//EABsQAQEAAwADAAAAAAAAAAAAAAECAAMREhMh/9oACAEBAAEFAtrWBszxc7dy0B7ZM1dNLClR9//EABYRAQEBAAAAAAAAAAAAAAAAAAEQEf/aAAgBAwEBPwHCLP/EABURAQEAAAAAAAAAAAAAAAAAACAh/9oACAECAQE/AaP/xAAcEAACAQUBAAAAAAAAAAAAAAAAESEBAhIxkTL/2gAIAQEABj8CpS21snA9GXEbJgQuGj//xAAeEAEAAwACAgMAAAAAAAAAAAABABEhMUFRYXGRsf/aAAgBAQABPyEDaPLiUMD7gur3o6hZSgcRQE6We4YoUyOfMJI+X7FdKf/aAAwDAQACAAMAAAAQkxcC/8QAGREBAAIDAAAAAAAAAAAAAAAAAQAQESEx/9oACAEDAQE/EBHcyRHtf//EABcRAQEBAQAAAAAAAAAAAAAAAAEAEBH/2gAIAQIBAT8QEFxgDP/EAB0QAQACAgMBAQAAAAAAAAAAAAERIQAxQVFhoZH/2gAIAQEAAT8Q7QWoPu8C/FFUfmHQ6phI8E4f1Yi62r8wHsBeHNdzWCXI4iZ9xxQSwSrDqbaWp0a8E6jKZUIs3n//2Q==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Meme about Vuetify grid system wrapping&quot; title=&quot;Meme about Vuetify grid system wrapping&quot; src=&quot;/static/5860d721cacd95c5890b765b4e51708d/066f9/meme-old-days.jpg&quot; srcSet=&quot;/static/5860d721cacd95c5890b765b4e51708d/e07e9/meme-old-days.jpg 200w,/static/5860d721cacd95c5890b765b4e51708d/066f9/meme-old-days.jpg 400w&quot; sizes=&quot;(max-width: 400px) 100vw, 400px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;The good old days, am I right? 😂&lt;/figcaption&gt;&lt;h2 id=&quot;going-down-the-rabbit-hole&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#going-down-the-rabbit-hole&quot; aria-label=&quot;going down the rabbit hole permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Going down the rabbit hole&lt;/h2&gt;&lt;p&gt;Did you know you can nest your rows and columns? Yes, a great feature indeed. But tread carefully! It can be dangerous (for maintenance) if you take it too far. 😅 So what conditions need to be fulfilled for you to be able to nest your grid? Well, the only condition that needs to be met is that &lt;code&gt;&amp;lt;v-col&amp;gt;&lt;/code&gt; is a direct child of &lt;code&gt;&amp;lt;v-row&amp;gt;&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Below is an example of how you &lt;em&gt;could&lt;/em&gt; nest your grid. The possibilities are endless, really.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-row&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;md&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-row&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;no-gutters&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;md&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;md&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-row&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-row&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:768px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:13%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAADCAYAAACTWi8uAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAc0lEQVQI12OYpBz3f5ISfjwRipHZ6BhkzgT56P8MH939/n/28AdiPwz8wSPg/1839//Lwlr+h6bs/R8ft/F/bPxmrDguftP/iMTt/xlOmwf/P4MDn7II+X/JzO//XPvi/9nu0/4Xufb/L3SdgBPnuk76DwBzIojbzk4vUwAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Nested flex units in the Vuetify grid system&quot; title=&quot;Nested flex units in the Vuetify grid system&quot; src=&quot;/static/77b0d2121f89d86bedd6009a274f04c6/e5715/figure-3.png&quot; srcSet=&quot;/static/77b0d2121f89d86bedd6009a274f04c6/772e8/figure-3.png 200w,/static/77b0d2121f89d86bedd6009a274f04c6/e17e5/figure-3.png 400w,/static/77b0d2121f89d86bedd6009a274f04c6/e5715/figure-3.png 768w&quot; sizes=&quot;(max-width: 768px) 100vw, 768px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;&lt;strong&gt;Figure 3. &lt;/strong&gt;Nested rows and columns&lt;/figcaption&gt;&lt;p&gt;Once again, you may play around with the code &lt;a href=&quot;https://codepen.io/dkelsen/pen/KKMrqQv&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt; and see how far the rabbit hole goes.&lt;/p&gt;&lt;p&gt;Now, if you&amp;#x27;ve made it this far, &lt;em&gt;congratulations&lt;/em&gt;! You already have a firm understanding of how Vuetify&amp;#x27;s grid system works and should make the &lt;a href=&quot;https://vuetifyjs.com/en/components/grids/#usage&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;documentation&lt;/a&gt; much more easier to understand. However, there are still a few other features I&amp;#x27;d like to show you before you venture off into the wild.&lt;/p&gt;&lt;h2 id=&quot;gimme-some-space-please&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#gimme-some-space-please&quot; aria-label=&quot;gimme some space please permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Gimme some space, please!&lt;/h2&gt;&lt;p&gt;Now what happens if we have less than 12 columns in total &lt;em&gt;per line&lt;/em&gt; (remember, you can have multiple lines in a row)? Well, this is where things get interesting. By default, Vuetify&amp;#x27;s grid system will place all your flex units to the left. However, Vuetify &lt;a href=&quot;https://vuetifyjs.com/en/components/grids/#api&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;provides a host of different props&lt;/a&gt; to align and/or justify your content to your desire.&lt;/p&gt;&lt;p&gt;In the example below, two different props were used on each of the &lt;code&gt;&amp;lt;v-row&amp;gt;&lt;/code&gt; tags to get the output in Figure 4.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-container&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-row&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;justify&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;space-between&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;md&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      ...&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;md&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      ...&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-row&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-row&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;justify&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;space-around&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;md&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      ...&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;md&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      ...&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-row&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-container&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:16%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAADCAYAAACTWi8uAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA8ElEQVQI12P4l57+5l909G8gfvcvKurzv7y86f9dXBj+RUYy/MvNZQDyGf5lZTFMVklgmKaVyjBVM4WhVzKCoZnFk2G+bdHcftnoz32SkW97JSL+TFZNfM7wr6BA619amvK//Pyof6mpy4C4/19CQiFQrOJvba0cEDP8raxknGddyLjAoYQBiGWm6aSVTVSMK5qqkTxhilrislnG2VHTtFOV55jnaTL8LSkBuwDoktB/MTFP/oWG3vwXFvYAyH75t7TUGixfWMg0Qy+DabZJDgMQm09UiH3WJRTysFs45GaPWNiTGXrpoVO1UoCWlTIAAPKabmqfshF4AAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Justified flex units in the Vuetify grid system&quot; title=&quot;Justified flex units in the Vuetify grid system&quot; src=&quot;/static/78cc174fe51ec46661fdfa115f7d1678/5a190/figure-4.png&quot; srcSet=&quot;/static/78cc174fe51ec46661fdfa115f7d1678/772e8/figure-4.png 200w,/static/78cc174fe51ec46661fdfa115f7d1678/e17e5/figure-4.png 400w,/static/78cc174fe51ec46661fdfa115f7d1678/5a190/figure-4.png 800w,/static/78cc174fe51ec46661fdfa115f7d1678/914ae/figure-4.png 860w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;&lt;strong&gt;Figure 4. &lt;/strong&gt;Flex units justified using props&lt;/figcaption&gt;&lt;p&gt;Why not go ahead and try out some different props with the code &lt;a href=&quot;https://codepen.io/dkelsen/pen/vYKQJVZ&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;?&lt;/p&gt;&lt;h2 id=&quot;what-about-v-spacer-and-v-col&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#what-about-v-spacer-and-v-col&quot; aria-label=&quot;what about v spacer and v col permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;What about v-spacer and v-col?&lt;/h2&gt;&lt;p&gt;Besides the props for justifying and aligning content, there are two other common features you might use when it comes to adding space.&lt;/p&gt;&lt;p&gt;The first one is the &lt;code&gt;&amp;lt;v-spacer&amp;gt;&lt;/code&gt; tag. Imagine it as an invisible flex unit that will occupy as many columns as possible without triggering any wrapping. If you were to add a spacer at the beginning of a row, it will push everything to the right. If you add one between two flex units, they will be pushed all the way to both sides. What else is important?... Oh yeah, if you have more than 1 spacer, they will try to take up the same amount of columns.&lt;/p&gt;&lt;p&gt;So for example, you could center a single flex unit by placing a spacer on each side.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-container&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-row&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-spacer&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      ...&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-spacer&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-row&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-container&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Last but not least, we can offset specific flex units using the prop &lt;code&gt;offset-(size)=&amp;quot;(1-11)&amp;quot;&lt;/code&gt; in &lt;code&gt;&amp;lt;v-col&amp;gt;&lt;/code&gt;, with size referring to Vuetify&amp;#x27;s breakpoints and the numbers referring to the number of columns the flex unit is offset by. Another way to think of an offset is as an &amp;quot;invisible&amp;quot; flex unit.&lt;/p&gt;&lt;p&gt;The code snippet below is an example of using an offset on each flex unit and can be found &lt;a href=&quot;https://codepen.io/dkelsen/pen/qBNQPZm&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt; for your entertainment.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-container&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-row&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      ...&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      ...&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-col&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-row&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-container&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:664px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:9%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAACCAYAAABYBvyLAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAmUlEQVQI12NgAII/GzYw/PfwYP7v58fwr6Cg8l9i4v9/0dGv/8XEvAPiz/+Skp7/mTdP5G93F8Pf5mbGqVopLNN10xim66Z39ctG/+gSDH7UJRzyDIQZ4Aa6uzP/9/Vl+JefX/cvIQFk4FegYT+A9F+gBR//zJ0r+rcLaGBTE+Msk2yWOeZ5DEA8Y4pa0v8+qchv/TJRv0AYAOltTzR1oOM4AAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;2 offset flex units in the Vuetify grid system&quot; title=&quot;2 offset flex units in the Vuetify grid system&quot; src=&quot;/static/2583faccec0797f9cbdb6d14094ff3bb/31493/figure-5.png&quot; srcSet=&quot;/static/2583faccec0797f9cbdb6d14094ff3bb/772e8/figure-5.png 200w,/static/2583faccec0797f9cbdb6d14094ff3bb/e17e5/figure-5.png 400w,/static/2583faccec0797f9cbdb6d14094ff3bb/31493/figure-5.png 664w&quot; sizes=&quot;(max-width: 664px) 100vw, 664px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;&lt;strong&gt;Figure 5. &lt;/strong&gt;Each flex unit is offset by 3 columns&lt;/figcaption&gt;&lt;h2 id=&quot;the-end-is-nigh&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-end-is-nigh&quot; aria-label=&quot;the end is nigh permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The end is nigh!&lt;/h2&gt;&lt;p&gt;Vuetify&amp;#x27;s grid system offers so many features, it would take quite some time to explore every nook and cranny of it. Why not check out the &lt;a href=&quot;https://vuetifyjs.com/en/components/grids/#usage&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;documentation&lt;/a&gt; yourself? If you&amp;#x27;ve made it this far, I&amp;#x27;m sure you&amp;#x27;ll have no problem going on further without me! 😁&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Format Dates In MUI DateTime Picker]]></title><description><![CDATA[The  inputFormat  prop allows you to  specify  a format for the  MUI Datetime picker . By default, it uses the  American  date format of…]]></description><link>https://dilshankelsen.com/format-dates-in-mui-datetime-picker/</link><guid isPermaLink="false">https://dilshankelsen.com/format-dates-in-mui-datetime-picker/</guid><pubDate>Mon, 06 Feb 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The &lt;code&gt;inputFormat&lt;/code&gt; prop allows you to &lt;em&gt;specify&lt;/em&gt; a format for the &lt;strong&gt;MUI Datetime picker&lt;/strong&gt;. By default, it uses the &lt;em&gt;American&lt;/em&gt; date format of &lt;code&gt;month/day/year&lt;/code&gt;. To change it to &lt;code&gt;day/month/year&lt;/code&gt;, enter the following:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;DateTimePicker&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;inputFormat&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;DD/MM/YYYY hh:mm A&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;p&gt;The letters in the datetime format are case sensitive.&lt;/p&gt;&lt;/div&gt;&lt;p&gt;The capital &lt;code&gt;A&lt;/code&gt; at the end is a placeholder for &lt;strong&gt;AM/PM&lt;/strong&gt; (&lt;em&gt;capitalized&lt;/em&gt;). If you didn&amp;#x27;t quite find what you came for, I highly recommend checking out the &lt;a href=&quot;https://momentjs.com/docs/#/displaying/format/&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;documentation of Moment.js&lt;/a&gt; on the many formatting possibilities.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Compare Two Dates In JavaScript]]></title><description><![CDATA[When it comes to comparing two dates, you should  ask yourself : Do I want to  include the time of the day  in the comparison itself? If the…]]></description><link>https://dilshankelsen.com/compare-two-dates-in-javascript/</link><guid isPermaLink="false">https://dilshankelsen.com/compare-two-dates-in-javascript/</guid><pubDate>Thu, 02 Feb 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;When it comes to comparing two dates, you should &lt;strong&gt;ask yourself&lt;/strong&gt;: Do I want to &lt;em&gt;include the time of the day&lt;/em&gt; in the comparison itself? If the answer is yes, the &lt;em&gt;easiest way&lt;/em&gt; of performing a comparison is by using comparison signs (e.g. &lt;code&gt;&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;gt;=&lt;/code&gt;).&lt;/p&gt;&lt;h2 id=&quot;comparing-dates-with-time&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#comparing-dates-with-time&quot; aria-label=&quot;comparing dates with time permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Comparing dates with time&lt;/h2&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; date1 &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; date2 &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; date1 &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; date2&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;However, be careful when checking for &lt;em&gt;equality&lt;/em&gt;! An equality check of two different objects will always return &lt;code&gt;false&lt;/code&gt; as JavaScript performs an &lt;strong&gt;equality of reference&lt;/strong&gt; instead of an &lt;em&gt;equality of value&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;Instead, use the &lt;code&gt;.getTime&lt;/code&gt; method to retrieve the Unix timestamp of the date.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; date1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getTime&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; date2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getTime&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;comparing-days-only&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#comparing-days-only&quot; aria-label=&quot;comparing days only permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Comparing days only&lt;/h2&gt;&lt;p&gt;On the other hand, if you wish to &lt;strong&gt;ignore the time&lt;/strong&gt; in the comparison, &lt;code&gt;.setHours&lt;/code&gt; is your guy! What to do? Simply set the &lt;em&gt;time to midnight&lt;/em&gt; for any dates being compared.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; dateA &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;date1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; dateB &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;date2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;dateA&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;setHours&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;dateB&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;setHours&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; dateA &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; dateB&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Notice how I &lt;strong&gt;copied both dates&lt;/strong&gt; into new variables &lt;code&gt;dateA&lt;/code&gt; and &lt;code&gt;dateB&lt;/code&gt;. The reason for doing so is that &lt;code&gt;setHours&lt;/code&gt; will &lt;em&gt;mutate the original dates&lt;/em&gt;, which will cause undesired side-effects in your application if not accounted for. In addition, if you wish to &lt;strong&gt;check for equality&lt;/strong&gt;, use &lt;code&gt;.getTime&lt;/code&gt; as in the previous section.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Use Google Places API In React]]></title><description><![CDATA[Before you can use the  Google Places API , you first need to  ensure  that the  API is enabled . Loading the library To load the API…]]></description><link>https://dilshankelsen.com/use-google-places-api-in-react/</link><guid isPermaLink="false">https://dilshankelsen.com/use-google-places-api-in-react/</guid><pubDate>Mon, 30 Jan 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Before you can use the &lt;strong&gt;Google Places API&lt;/strong&gt;, you first need to &lt;em&gt;ensure&lt;/em&gt; that the &lt;a href=&quot;https://developers.google.com/maps/documentation/javascript/places#enable_apis&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;API is enabled&lt;/a&gt;.&lt;/p&gt;&lt;h2 id=&quot;loading-the-library&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#loading-the-library&quot; aria-label=&quot;loading the library permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Loading the library&lt;/h2&gt;&lt;p&gt;To load the API library, I &lt;em&gt;recommend&lt;/em&gt; using a custom hook, &lt;strong&gt;in particular&lt;/strong&gt; 👉 &lt;a href=&quot;https://usehooks.com/useScript/&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;this example&lt;/a&gt;. For the &lt;em&gt;TypeScript lovers&lt;/em&gt;, you&amp;#x27;re &lt;a href=&quot;https://usehooks-ts.com/react-hook/use-script&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;covered as well&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;In your &lt;strong&gt;Google Places Input&lt;/strong&gt;, i.e. &lt;em&gt;AutoComplete&lt;/em&gt; component, call the hook with the link below. Make sure to replace &lt;code&gt;YOUR_KEY&lt;/code&gt; with the API key retrieved during the setup earlier.&lt;/p&gt;&lt;div class=&quot;gatsby-code-title code-title-custom&quot;&gt;AutoComplete.tsx&lt;/div&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-tsx&quot;&gt;TSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-tsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; scriptStatus &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useScript&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// By default, Google Places will attempt to guess your language based on your country.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// However, you may specify it explicitly with the `language` parameter.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;https://maps.googleapis.com/maps/api/js?language=en&amp;amp;key=&lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token template-string interpolation constant&quot;&gt;YOUR_KEY&lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;&amp;amp;libraries=places&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;creating-the-autocomplete-component&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#creating-the-autocomplete-component&quot; aria-label=&quot;creating the autocomplete component permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Creating the AutoComplete component&lt;/h2&gt;&lt;p&gt;In the &lt;em&gt;Autocomplete&lt;/em&gt; component, we maintain &lt;strong&gt;2 refs&lt;/strong&gt;:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;One is of the component itself, which will contain the Google Places AutoComplete &lt;em&gt;class&lt;/em&gt;, and&lt;/li&gt;&lt;li&gt;The other is of the input field, inside the component, which Google Places uses to &lt;em&gt;automatically&lt;/em&gt; 🙌 &lt;em&gt;add dropdown options&lt;/em&gt; upon user input.&lt;/li&gt;&lt;/ul&gt;&lt;div class=&quot;gatsby-code-title code-title-custom&quot;&gt;AutoComplete.tsx&lt;/div&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-tsx&quot;&gt;TSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-tsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; autoCompleteRef &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token generic-function function&quot;&gt;useRef&lt;/span&gt;&lt;span class=&quot;token generic-function generic class-name operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token generic-function generic class-name&quot;&gt;google&lt;/span&gt;&lt;span class=&quot;token generic-function generic class-name punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token generic-function generic class-name&quot;&gt;maps&lt;/span&gt;&lt;span class=&quot;token generic-function generic class-name punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token generic-function generic class-name&quot;&gt;places&lt;/span&gt;&lt;span class=&quot;token generic-function generic class-name punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token generic-function generic class-name&quot;&gt;AutoComplete&lt;/span&gt;&lt;span class=&quot;token generic-function generic class-name operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; inputRef &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token generic-function function&quot;&gt;useRef&lt;/span&gt;&lt;span class=&quot;token generic-function generic class-name operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token generic-function generic class-name&quot;&gt;HTMLInputElement&lt;/span&gt;&lt;span class=&quot;token generic-function generic class-name operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Some code in the next example.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;ref&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;inputRef&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Following that, a &lt;code&gt;useEffect&lt;/code&gt; hook will take care of the &lt;strong&gt;ref setup&lt;/strong&gt;, including &lt;em&gt;data fetching&lt;/em&gt;. It should only run once upon the component mounting, hence the various &lt;em&gt;checks&lt;/em&gt; 💂‍♂️ in the beginning.&lt;/p&gt;&lt;p&gt;What you should pay attention to is the initialization of the &lt;code&gt;AutoComplete&lt;/code&gt; class, which accepts a &lt;em&gt;reference&lt;/em&gt; to the input field and an &lt;a href=&quot;https://developers.google.com/maps/documentation/javascript/reference/places-widget#AutoCompleteOptions&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;options object&lt;/a&gt;. The latter allows you to &lt;strong&gt;apply restrictions&lt;/strong&gt; such as countries and types of locations as well as the type of data you wish to receive.&lt;/p&gt;&lt;p&gt;Moreover, an anonymous function listens to the event &lt;code&gt;place_changed&lt;/code&gt;, which occurs whenever a user selects a location from the &lt;strong&gt;Google Places&lt;/strong&gt; generated dropdown.&lt;/p&gt;&lt;div class=&quot;gatsby-code-title code-title-custom&quot;&gt;AutoComplete.tsx&lt;/div&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-tsx&quot;&gt;TSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-tsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; options &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useMemo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    componentRestrictions&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; country&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;at&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    fields&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;address_components&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;geometry&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;name&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    types&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;establishment&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useEffect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Conditions to ensure that no multiple instances of the&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Google Places API class and event listener exist.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    autoCompleteRef&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    scriptStatus &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;loading&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;inputRef&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;google&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;google&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;maps&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;google&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;maps&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;places&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;scriptStatus &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;error&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Report error&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  autoCompleteRef&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;google&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;maps&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;places&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access maybe-class-name&quot;&gt;AutoComplete&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    inputRef&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    options&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  autoCompleteRef&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;addListener&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;place_changed&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;autoCompleteRef&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Retrieve the selected location with the `getPlace` method.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;onChange&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;autoCompleteRef&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getPlace&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;scriptStatus&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; options&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; onChange&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In this code example, &lt;code&gt;onChange&lt;/code&gt; is simply a &lt;code&gt;useState&lt;/code&gt; function that&amp;#x27;s passed in ⬇ &lt;em&gt;from the parent&lt;/em&gt; component.&lt;/p&gt;&lt;h2 id=&quot;gotcha&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#gotcha&quot; aria-label=&quot;gotcha permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Gotcha&lt;/h2&gt;&lt;p&gt;Lastly, you may want to set the value from the input field to &lt;code&gt;null&lt;/code&gt; &lt;strong&gt;on any key stroke&lt;/strong&gt; to ensure that only values retrieved by the API are accepted. &lt;em&gt;For instance&lt;/em&gt;, the user may remove their selection or accidentally delete a character or two in the field.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;diff&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-diff&quot;&gt;DIFF&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-diff&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token deleted-arrow deleted prefix deleted&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token deleted-arrow deleted line&quot;&gt;input&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token deleted-arrow deleted line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token unchanged prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token unchanged line&quot;&gt;   ref={inputRef}&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token unchanged line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted line&quot;&gt;   onChange={() =&amp;gt; onChange(null)}&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token inserted-sign inserted line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token unchanged prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token unchanged line&quot;&gt; /&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token unchanged line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Getting Unit/Suite Numbers In Google Places Autocomplete]]></title><description><![CDATA[It is possible to get the  unit/suite/door numbers  etc. for any address in any country. This information is shared through the  subpremise…]]></description><link>https://dilshankelsen.com/getting-unit-suite-numbers-in-google-places-autocomplete/</link><guid isPermaLink="false">https://dilshankelsen.com/getting-unit-suite-numbers-in-google-places-autocomplete/</guid><pubDate>Thu, 26 Jan 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;It is possible to get the &lt;strong&gt;unit/suite/door numbers&lt;/strong&gt; etc. for any address in any country. This information is shared through the &lt;code&gt;subpremise&lt;/code&gt; type returned by the &lt;a href=&quot;https://developers.google.com/maps/documentation/places/web-service/supported_types#table2&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;Places API&lt;/a&gt;. Knowing this, it begs the question &lt;em&gt;how does one get it&lt;/em&gt; when entering an address? 🤷‍♀️&lt;/p&gt;&lt;p&gt;Unfortunately, it &lt;strong&gt;depends very much on each country&lt;/strong&gt; and I couldn&amp;#x27;t find any specifics on this (maybe you know better 👀). For instance, &lt;em&gt;in Austria&lt;/em&gt;, it&amp;#x27;s common to write an address in the following format.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-text&quot;&gt;TEXT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-text&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Street address [house number]/[unit number]/[floor number]/[door number]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This will yield &lt;em&gt;no result&lt;/em&gt; with Google Places and instead it will return &lt;code&gt;Street address [house number]&lt;/code&gt;. However, there exists another more &lt;strong&gt;verbose format&lt;/strong&gt; in Austria.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-text&quot;&gt;TEXT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-text&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Street address [house number], Stiege [unit number], Stockwerk [floor number], Top [door number]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you were to provide it like this to Google Places (as I found out by &lt;em&gt;coincidence&lt;/em&gt; 🤦‍♀️), you will &lt;strong&gt;receive the information&lt;/strong&gt; correctly in &lt;code&gt;subpremise&lt;/code&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA['Types Of Property Are Incompatible' In GraphQL CodeGen]]></title><description><![CDATA[If you use  GraphQL CodeGen  to generate your types and  Prisma  on the backend, you will eventually come accross the following error due to…]]></description><link>https://dilshankelsen.com/types-of-property-are-incompatible-in-graphql-codegen/</link><guid isPermaLink="false">https://dilshankelsen.com/types-of-property-are-incompatible-in-graphql-codegen/</guid><pubDate>Mon, 23 Jan 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;If you use &lt;em&gt;GraphQL CodeGen&lt;/em&gt; to generate your types and &lt;em&gt;Prisma&lt;/em&gt; on the backend, you will eventually come accross the following error due to &lt;strong&gt;a divergence&lt;/strong&gt; in resolver types.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Types of property &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;X&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; are incompatible.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Type &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;import(&amp;quot;...&amp;quot;).X&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; is not assignable to &lt;/span&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;import(&amp;quot;.../resolvers-types&amp;quot;).X&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To avoid this divergence, you will need to map your &lt;em&gt;generated&lt;/em&gt; Prisma types to your defined &lt;em&gt;GraphQL schema&lt;/em&gt; types. To accomplish this, add the following &lt;code&gt;config&lt;/code&gt; properties in your CodeGen file.&lt;/p&gt;&lt;div class=&quot;gatsby-code-title code-title-custom&quot;&gt;codegen.yml&lt;/div&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;yml&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-yml&quot;&gt;YML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-yml&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token key atrule&quot;&gt;generates&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token key atrule&quot;&gt;./generated/resolvers-types.ts&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token key atrule&quot;&gt;plugins&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; typescript&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; typescript&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;resolvers&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token key atrule&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token key atrule&quot;&gt;mapperTypeSuffix&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Model&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token key atrule&quot;&gt;mappers&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token key atrule&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;@prisma/client#X&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Under &lt;code&gt;mappers&lt;/code&gt;, &lt;strong&gt;assign&lt;/strong&gt; each GraphQL schema type its &lt;em&gt;Prisma equivalent&lt;/em&gt;, e.g. &lt;code&gt;User: &amp;quot;@prisma/client#User&amp;quot;&lt;/code&gt;.&lt;/p&gt;&lt;div&gt;&lt;p&gt;&lt;code&gt;mapperTypeSuffix&lt;/code&gt; helps us avoid name collisions 💥 between your GraphQL and generated Prisma types.&lt;/p&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Display JavaScript Date In Specific Time Zone]]></title><description><![CDATA[Given a value of type  Date , you can get a  date string representation  of a  certain time zone  as follows. Don't know the  string value…]]></description><link>https://dilshankelsen.com/display-javascript-date-in-specific-time-zone/</link><guid isPermaLink="false">https://dilshankelsen.com/display-javascript-date-in-specific-time-zone/</guid><pubDate>Thu, 19 Jan 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Given a value of type &lt;code&gt;Date&lt;/code&gt;, you can get a &lt;strong&gt;date string representation&lt;/strong&gt; of a &lt;em&gt;certain time zone&lt;/em&gt; as follows.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; dt &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Date Thu Jan 05 2023 21:12:29 GMT+0100 (Mitteleuropäische Normalzeit)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;dt&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;toLocaleString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;en-GB&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;timeZone&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;America/New_York&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// &amp;quot;05/01/2023, 15:12:29&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Don&amp;#x27;t know the &lt;em&gt;string value&lt;/em&gt; of a specific timezone? 🤷‍♀️ &lt;code&gt;Intl.supportedValuesOf(&amp;#x27;timeZone&amp;#x27;)&lt;/code&gt; will return an array of all &lt;strong&gt;possible values&lt;/strong&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Restart Numbering Of Ordered List In DOCX JS]]></title><description><![CDATA[To restart the numbering of an ordered list, we need to give each list a  unique instance id  throughout the  entire  document. This…]]></description><link>https://dilshankelsen.com/restart-numbering-of-ordered-list-in-docx-js/</link><guid isPermaLink="false">https://dilshankelsen.com/restart-numbering-of-ordered-list-in-docx-js/</guid><pubDate>Mon, 16 Jan 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;To restart the numbering of an ordered list, we need to give each list a &lt;strong&gt;unique instance id&lt;/strong&gt; throughout the &lt;em&gt;entire&lt;/em&gt; document. This instance ID is set in the &lt;code&gt;Paragraph&lt;/code&gt; object.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Paragraph&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;A single list item&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;numbering&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;reference&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;numbering-style-1&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Unique for each list&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;gotcha&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#gotcha&quot; aria-label=&quot;gotcha permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Gotcha&lt;/h2&gt;&lt;p&gt;If you plan on implementing any &lt;strong&gt;nested lists&lt;/strong&gt;, you will need to ensure a unique ID for them &lt;em&gt;as well&lt;/em&gt;. Here&amp;#x27;s an example to visualize the potential problem.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-text&quot;&gt;TEXT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-text&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;List A with a unique ID of &amp;#x27;1&amp;#x27;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;1. (id 1)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;-- a. (nested list A needs a new ID of &amp;#x27;2&amp;#x27;)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;-- b. (id 2)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;2. (id 1)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;-- a. (Nested list 2A needs another unique ID of &amp;#x27;3&amp;#x27; to avoid becoming &amp;#x27;c&amp;#x27;)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;List B with a new ID of &amp;#x27;4&amp;#x27;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;1.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;2.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;3.&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Grouping An Array Of Objects By Key]]></title><description><![CDATA[Imagine the following dataset which we would like to  group by   type . There are  2 ways  to solve this task, either by using  plain…]]></description><link>https://dilshankelsen.com/grouping-array-of-objects-by-key/</link><guid isPermaLink="false">https://dilshankelsen.com/grouping-array-of-objects-by-key/</guid><pubDate>Thu, 12 Jan 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Imagine the following dataset which we would like to &lt;strong&gt;group by&lt;/strong&gt; &lt;code&gt;type&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; food &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;vegetable&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Spinach&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;fruit&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Pear&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;fruit&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;vegetable&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Carrot&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;vegetable&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Broccoli&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;There are &lt;em&gt;2 ways&lt;/em&gt; to solve this task, either by using &lt;em&gt;plain JavaScript&lt;/em&gt; or &lt;em&gt;Lodash&lt;/em&gt;.&lt;/p&gt;&lt;h2 id=&quot;plain-javascript&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#plain-javascript&quot; aria-label=&quot;plain javascript permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Plain JavaScript&lt;/h2&gt;&lt;p&gt;The &lt;em&gt;cleanest&lt;/em&gt; way to solve this problem with plain JavaScript is by using &lt;strong&gt;a reducer&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; foodTypes &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; food&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;accumulator&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; food&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Retrieve any existing values that have already been categorized by `type`.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// If none exist, start with an empty array.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; existingValues &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; accumulator&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;food&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Assign the new `food` value and any existing values to their respective `type`.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    accumulator&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;food&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;food&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token spread operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;existingValues&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; accumulator&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Start with an empty object.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;foodTypes&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Object { vegetable: (3) […], fruit: (2) […] }&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;using-lodash&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#using-lodash&quot; aria-label=&quot;using lodash permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Using Lodash&lt;/h2&gt;&lt;p&gt;Lodash provides a much more straightforward approach as the &lt;em&gt;implementation details are hidden&lt;/em&gt; by the library.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; foodTypes &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; _&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;groupBy&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;food&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;food&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; food&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Or _.groupBy(food, &amp;#x27;type&amp;#x27;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;foodTypes&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Object { vegetable: (3) […], fruit: (2) […] }&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Note that in the &lt;em&gt;second argument&lt;/em&gt; of &lt;code&gt;groupBy&lt;/code&gt;, you specify the property you wish to group by, either by passing a &lt;strong&gt;callback function&lt;/strong&gt; or the &lt;strong&gt;property name&lt;/strong&gt; as a string.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Unknown Type Date In GraphQL Schema]]></title><description><![CDATA[If you get an  error  ❌ similar to this, it means your  scalar type   Date  has not been defined. Natively, GraphQL only  supports 6 types…]]></description><link>https://dilshankelsen.com/unknown-type-date-in-graphql-schema/</link><guid isPermaLink="false">https://dilshankelsen.com/unknown-type-date-in-graphql-schema/</guid><pubDate>Mon, 09 Jan 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;If you get an &lt;em&gt;error&lt;/em&gt; ❌ similar to this,&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Failed to load schema from graphql/schema.ts: Unknown type: &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Date&amp;quot;&lt;/span&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;it means your &lt;strong&gt;scalar type&lt;/strong&gt; &lt;code&gt;Date&lt;/code&gt; has not been defined. Natively, GraphQL only &lt;em&gt;supports 6 types&lt;/em&gt; out of the box:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;Int&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;Float&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;String&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;Boolean&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;ID&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;As a result, any other types need to be &lt;em&gt;defined by&lt;/em&gt; the developer themselves.&lt;/p&gt;&lt;h2 id=&quot;creating-a-custom-scalar-type&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#creating-a-custom-scalar-type&quot; aria-label=&quot;creating a custom scalar type permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Creating a custom scalar type&lt;/h2&gt;&lt;p&gt;A scalar type &lt;code&gt;Date&lt;/code&gt; is quite &lt;em&gt;common&lt;/em&gt;. To use one, first define the new scalar type in your &lt;strong&gt;GraphQL schema&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;graphql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-graphql&quot;&gt;GRAPHQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-graphql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;scalar&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Date&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Next, GraphQL needs to know 🧠 &lt;em&gt;how&lt;/em&gt; to treat this type. For that, the &lt;code&gt;GraphQLScalarType&lt;/code&gt; class is to be used. You should &lt;strong&gt;pay attention&lt;/strong&gt; to the following methods:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;serialize&lt;/code&gt; - How the data should be &lt;em&gt;converted&lt;/em&gt; from its &lt;strong&gt;back-end representation&lt;/strong&gt; (i.e. the JavaScript &lt;code&gt;Date&lt;/code&gt; type) to a &lt;em&gt;JSON-compatible format&lt;/em&gt; for network requests.&lt;/li&gt;&lt;li&gt;&lt;code&gt;parseValue&lt;/code&gt; - How the data should be reverted from its &lt;strong&gt;serialized form&lt;/strong&gt; to &lt;em&gt;back-end&lt;/em&gt; representation.&lt;/li&gt;&lt;li&gt;&lt;code&gt;parseLiteral&lt;/code&gt; - How the data should be treated if it&amp;#x27;s &lt;strong&gt;hardcoded&lt;/strong&gt; in an incoming &lt;em&gt;query&lt;/em&gt;. It&amp;#x27;s helpful to know that this hardcoded value is part of the query document&amp;#x27;s (i.e. query graph) &lt;strong&gt;abstract syntax tree&lt;/strong&gt; 🌳 (AST).&lt;/li&gt;&lt;/ul&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;GraphQLScalarType&lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;Kind&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;graphql&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; dateScalar &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;GraphQLScalarType&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Date&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;description&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Date custom scalar type&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;serialize&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getTime&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Convert outgoing Date to integer for JSON&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;parseValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Convert incoming integer to Date&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;parseLiteral&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;ast&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;ast&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;Kind&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;INT&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Convert hard-coded AST string to integer and then to Date&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;parseInt&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;ast&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Invalid hard-coded value (not an integer)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword null nil&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Lastly, add the custom scalar type in your &lt;strong&gt;resolvers&lt;/strong&gt;, so GraphQL has &lt;em&gt;access&lt;/em&gt; to its definition.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; resolvers &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; dateScalar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;//...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[When To Use Given Or When In Cucumber]]></title><description><![CDATA[It can be difficult to know whether a step should be defined as a  Given   or   When  step. However, a general rule of advice is to keep the…]]></description><link>https://dilshankelsen.com/when-to-use-given-or-when-in-cucumber/</link><guid isPermaLink="false">https://dilshankelsen.com/when-to-use-given-or-when-in-cucumber/</guid><pubDate>Thu, 05 Jan 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;It can be difficult to know whether a step should be defined as a &lt;code&gt;Given&lt;/code&gt; &lt;em&gt;or&lt;/em&gt; &lt;code&gt;When&lt;/code&gt; step. However, a general rule of advice is to keep the &lt;strong&gt;scenarios as brief as possible&lt;/strong&gt;. The goal is to be &lt;em&gt;descriptive&lt;/em&gt;, not exhaustive. As a consequence, it&amp;#x27;s recommended to restrict yourself to a single &lt;code&gt;When&lt;/code&gt; statement in a scenario.&lt;/p&gt;&lt;p&gt;Here&amp;#x27;s a common example:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;gherkin&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-gherkin&quot;&gt;GHERKIN&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-gherkin&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Given the user is unauthenticated&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;When the user provides incorrect credentials&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;And the user clicks on &amp;quot;Login&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Then the user is prevented from logging in&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;One can &lt;em&gt;reasonably&lt;/em&gt; argue that entering a false username and password as well as submitting the credentials constitute actions of this scenario. However, if a scenario should only contain &lt;strong&gt;a single&lt;/strong&gt; &lt;code&gt;When&lt;/code&gt; step, this step should be responsible for the outcome. Taking this into consideration, one would &lt;em&gt;rewrite&lt;/em&gt; the previous scenario as follows.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;gherkin&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-gherkin&quot;&gt;GHERKIN&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-gherkin&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Given the user is unauthenticated&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;And the user clicks on &amp;quot;Login&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;When the user provides incorrect credentials&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Then the user is prevented from logging in&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The act of a user submitting their credentials should be part of the context, while entering false credentials should be the single &lt;code&gt;When&lt;/code&gt; statement. That is because it is &lt;strong&gt;responsible for the outcome&lt;/strong&gt;, i.e. being refused login.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Seed Your Database With Prisma]]></title><description><![CDATA[Create your script The  logic  of your  seed script  (e.g. create user data) is contained within a  main  function. Once your script is…]]></description><link>https://dilshankelsen.com/how-to-seed-your-database-with-prisma/</link><guid isPermaLink="false">https://dilshankelsen.com/how-to-seed-your-database-with-prisma/</guid><pubDate>Mon, 02 Jan 2023 00:00:00 GMT</pubDate><content:encoded>&lt;h2 id=&quot;create-your-script&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#create-your-script&quot; aria-label=&quot;create your script permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Create your script&lt;/h2&gt;&lt;p&gt;The &lt;em&gt;logic&lt;/em&gt; of your &lt;strong&gt;seed script&lt;/strong&gt; (e.g. create user data) is contained within a &lt;code&gt;main&lt;/code&gt; function. Once your script is &lt;em&gt;finished&lt;/em&gt; executing, the prisma client is terminated in the &lt;code&gt;finally&lt;/code&gt; method. Simultaneously, we keep an eye 👀 out for any potential &lt;strong&gt;errors&lt;/strong&gt; using the &lt;code&gt;catch&lt;/code&gt; method (e.g. due to an incorrect query).&lt;/p&gt;&lt;div class=&quot;gatsby-code-title code-title-custom&quot;&gt;seed.ts&lt;/div&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; prisma &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;PrismaClient&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Perform database queries here&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// await prisma.user.create(...)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;catch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;error &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    process&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;finally&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; prisma&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;$disconnect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;run-your-script&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#run-your-script&quot; aria-label=&quot;run your script permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Run your script&lt;/h2&gt;&lt;p&gt;To run your script, you will first need to have the following &lt;strong&gt;dependencies&lt;/strong&gt; installed.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# NPM&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; -D typescript ts-node @types/node&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# Yarn&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;yarn&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; --dev typescript ts-node @types/node&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Prisma &lt;em&gt;expects&lt;/em&gt; any seed scripts to be executed via the &lt;code&gt;seed&lt;/code&gt; field in the &lt;code&gt;prisma&lt;/code&gt; key of your &lt;code&gt;package.json&lt;/code&gt; file.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;json&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-json&quot;&gt;JSON&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-json&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;name&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;my-blog&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;version&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;1.0.0&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;prisma&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;seed&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;ts-node &amp;lt;path to file&amp;gt;/seed.ts&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;devDependencies&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;@types/node&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;18.7.12&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;ts-node&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;^10.9.1&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;typescript&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;4.8.0&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You may then &lt;em&gt;manually&lt;/em&gt; 💻 execute your script with:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# NPM&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;npx prisma db seed&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# Yarn&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;yarn&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; prisma db seed&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Be aware&lt;/strong&gt; that your seed script is run &lt;em&gt;automatically&lt;/em&gt; with &lt;code&gt;prisma migrate dev&lt;/code&gt; and &lt;code&gt;prisma migrate reset&lt;/code&gt;. If you wish this not to happen, append &lt;code&gt;--skip-seed&lt;/code&gt;.&lt;/p&gt;&lt;h3 id=&quot;script-execution-in-nextjs&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#script-execution-in-nextjs&quot; aria-label=&quot;script execution in nextjs permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Script execution in Next.js&lt;/h3&gt;&lt;p&gt;If you are running, for example, a &lt;em&gt;Next.js project&lt;/em&gt;, you will need to adjust your prisma seed field to &lt;strong&gt;include a compiler&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-code-title code-title-custom&quot;&gt;package.json&lt;/div&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;json&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-json&quot;&gt;JSON&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-json&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;prisma&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;   &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;seed&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;ts-node --compiler-options {\&amp;quot;module\&amp;quot;:\&amp;quot;CommonJS\&amp;quot;} &amp;lt;path to file&amp;gt;/seed.ts&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Creating A Scroll To Top Button With React MUI]]></title><description><![CDATA[There's a decent article by  Henrik from Apollo Learn  on how to create a  Scroll To Top button . However, it  contains a bug  🐛: namely…]]></description><link>https://dilshankelsen.com/creating-scroll-to-top-button-with-react-mui/</link><guid isPermaLink="false">https://dilshankelsen.com/creating-scroll-to-top-button-with-react-mui/</guid><pubDate>Thu, 29 Dec 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;There&amp;#x27;s a decent article by &lt;a href=&quot;https://apollo-learn.com/lessons/how-to-make-a-scroll-to-top-button-(mui-v5))-54&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;Henrik from Apollo Learn&lt;/a&gt; on how to create a &lt;em&gt;Scroll To Top button&lt;/em&gt;. However, it &lt;strong&gt;contains a bug&lt;/strong&gt; 🐛: namely, it uses the &lt;code&gt;body&lt;/code&gt; tag &lt;em&gt;instead&lt;/em&gt; of &lt;code&gt;window&lt;/code&gt; as a target for scrolling.&lt;/p&gt;&lt;p&gt;The problem is that &lt;code&gt;document&lt;/code&gt; will be &lt;code&gt;undefined&lt;/code&gt; when &lt;code&gt;useScrollTrigger&lt;/code&gt; is first run.&lt;/p&gt;&lt;div class=&quot;gatsby-code-title code-title-custom&quot;&gt;ScrollToTopFab.tsx&lt;/div&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-tsx&quot;&gt;TSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-tsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;ScrollToTopFab&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Use `window` instead of `body` as `document` will be `undefined` when the&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// hooks first runs. By default, useScrollTrigger will attach itself to `window`.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; trigger &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useScrollTrigger&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Number of pixels needed to scroll to toggle `trigger` to `true`.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    threshold&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; scrollToTop &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useCallback&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;scrollTo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; top&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; behavior&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;smooth&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Zoom&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;trigger&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;role&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;presentation&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag comment&quot;&gt;// Place the button in the bottom right corner.&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;sx&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;          position&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript string&quot;&gt;&amp;quot;fixed&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;          bottom&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript number&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;          right&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript number&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;          zIndex&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Fab&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;onClick&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;scrollToTop&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;primary&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;small&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;aria-label&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;Scroll back to top&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;KeyboardArrowUp&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;fontSize&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;medium&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Fab&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Zoom&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;ScrollToTopFab&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The variable &lt;code&gt;trigger&lt;/code&gt; is a &lt;em&gt;boolean&lt;/em&gt; responsible for showing the button once the user has &lt;strong&gt;scrolled enough&lt;/strong&gt;. If the user &lt;em&gt;clicks&lt;/em&gt; on the button, &lt;code&gt;scrollToTop&lt;/code&gt; will trigger a smooth scroll to the top of the window.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Why HTML Element Has Space Or Gap On Top]]></title><description><![CDATA[You may encounter a situation where an HTML element appears to be offset at the top, even though  no  top margin was set. You, my friend…]]></description><link>https://dilshankelsen.com/why-html-element-has-space-or-gap-on-top/</link><guid isPermaLink="false">https://dilshankelsen.com/why-html-element-has-space-or-gap-on-top/</guid><pubDate>Mon, 26 Dec 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;You may encounter a situation where an HTML element appears to be offset at the top, even though &lt;em&gt;no&lt;/em&gt; top margin was set. You, my friend, have just experienced &lt;strong&gt;margin collapse&lt;/strong&gt;! 👻&lt;/p&gt;&lt;p&gt;Here&amp;#x27;s a (personal) &lt;em&gt;example&lt;/em&gt;: you&amp;#x27;ve got a &lt;code&gt;body&lt;/code&gt; element which for some reason has an &lt;strong&gt;offset&lt;/strong&gt; at the top, with no visible margin upon inspection in the browser.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:537px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:72.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABd0lEQVQ4y6VUCW6DMBDk/9+rquYAwuUABhsHCMYcma5Nq6aXVDUrjRbvjodhMXiMMex2exRFiev1iq7r0PX9B7qe6j1E06LgAlXF0RKn/8TpMAwD4jiGJ4TEX2PQIy6q+bXPeQWvzBLw+IgqO6FKwx8hWIg08MFOAZo8/tbnBJmFYFEAj4V7+IcdeFWTbQ1NLixGYzCOxmVN2bg19QjGTK5mHQ9aY5wmlDlDtH+GlwV7t/gaVmCZZ0xEXpbF5XVdMVPNzste34cSFeKDEyR3b4LvJLs5SRLkee6glIJ9eUIIcq/d8Ou63vbQzWzImm+CqW8Fz3g0GnIYWcHz6QCWRJBN4xy8wzr4ivvePVdIiYKlSPwXeEUcwOgrzWab0/8w4zYbsPBgBX0sRj/8yLgtsCdmE5zGrXZbCbd/Aeu8CZZJgJE+rWleHjZp34eX0jmMdk+Iji9QnOHCz5TPLv8FlquqHG2Z4UQ6nv0hXKRApySUrB9C17Z4BcdNNLQPYhUxAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Margin collapse example problem&quot; title=&quot;Margin collapse example problem&quot; src=&quot;/static/7990b3d391a2005a9983fbab6d81947b/b1cde/image-1.png&quot; srcSet=&quot;/static/7990b3d391a2005a9983fbab6d81947b/772e8/image-1.png 200w,/static/7990b3d391a2005a9983fbab6d81947b/e17e5/image-1.png 400w,/static/7990b3d391a2005a9983fbab6d81947b/b1cde/image-1.png 537w&quot; sizes=&quot;(max-width: 537px) 100vw, 537px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;A scenario of margin collapse&lt;/figcaption&gt;&lt;p&gt;It turns out that the offset comes from &lt;em&gt;the child element&lt;/em&gt; inside the body. &lt;strong&gt;How&lt;/strong&gt; is this happening? 🤷‍♀️&lt;/p&gt;&lt;p&gt;In CSS, when the vertical margins of two &lt;strong&gt;block&lt;/strong&gt; elements meet, the &lt;strong&gt;largest margin&lt;/strong&gt; out of these two is used instead of combining them.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:440px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:61%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAACXBIWXMAAA86AAAPOgGXOdvCAAABOUlEQVQoz3VS7XKEIAz0/V/QqeiM7QmCVcT6gYp63bt4qdNO84MhuyRsFqL7K0JY8zx3rrv/ieM4sLZtq7XmFBEdz8Bu20LTNOM4cQEHIdM0fnXdlY32fQ8hLMuybdtLQljXFSudAA4WKbGcojBC7r3HzrY2EyIRSd/3qAFId1LfYRjSVCSJaKzlkofseZ6x87M3upRK4TSDXAwpGFhKNY4jQLAAIxKGMMaUSsviZq1FGRCSjT1W2KGUKoqiqipmI+LQDMJc66ytp2liS8gRBGYBixbOOXLklE10qUop5e3jva5rNGbDoBCnsX5WlTEV7njM6P0pmwYAqjWkSdwM+jozdCDNslQI4Z6vRUacxci7rkuStziOh+G05NdT0Xtf0uM0DML4nUkzG8bDk69X8McwOnFN+W/9F9+XW7sZJPe+UgAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Margin collapse of two elements on top of each other&quot; title=&quot;Margin collapse of two elements on top of each other&quot; src=&quot;/static/90304f7947a0b8c9a7002c74ada6e2c0/48c0e/image-2.png&quot; srcSet=&quot;/static/90304f7947a0b8c9a7002c74ada6e2c0/772e8/image-2.png 200w,/static/90304f7947a0b8c9a7002c74ada6e2c0/e17e5/image-2.png 400w,/static/90304f7947a0b8c9a7002c74ada6e2c0/48c0e/image-2.png 440w&quot; sizes=&quot;(max-width: 440px) 100vw, 440px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;The most common example of margin collapse. Image found on &lt;a href=&quot;http://blog.firsthand.ca/2010/10/collapsing-margins-in-css.html&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;Firsthand Blog&lt;/a&gt;&lt;/figcaption&gt;&lt;p&gt;How does this &lt;em&gt;relate&lt;/em&gt; to the stated example problem?&lt;/p&gt;&lt;p&gt;As it stands, there is no &lt;strong&gt;padding&lt;/strong&gt; or &lt;strong&gt;element with height&lt;/strong&gt; at the very top of the &lt;code&gt;body&lt;/code&gt; element preventing the top margin of the &lt;code&gt;div&lt;/code&gt; tag touching the top margin of the &lt;code&gt;body&lt;/code&gt;. This causes both margins to &lt;em&gt;collapse&lt;/em&gt; 💥 and have the larger margin from the &lt;code&gt;div&lt;/code&gt; stick out of the parent element.&lt;/p&gt;&lt;h2 id=&quot;solutions&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#solutions&quot; aria-label=&quot;solutions permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Solution(s)&lt;/h2&gt;&lt;p&gt;This problem can be solved &lt;em&gt;by either&lt;/em&gt;:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Not setting&lt;/strong&gt; a top margin for the &lt;code&gt;div&lt;/code&gt;, or&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Adding&lt;/strong&gt; a top padding to the &lt;code&gt;body&lt;/code&gt;.&lt;/li&gt;&lt;/ul&gt;</content:encoded></item><item><title><![CDATA[Using An Array As A Key In A JavaScript Map]]></title><description><![CDATA[Unlike regular objects,  Maps  in JavaScript allow you to use  any  key type for your  key value  pairs. As the example above 👆 shows…]]></description><link>https://dilshankelsen.com/using-array-as-key-in-javascript-map/</link><guid isPermaLink="false">https://dilshankelsen.com/using-array-as-key-in-javascript-map/</guid><pubDate>Thu, 22 Dec 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Unlike regular objects, &lt;em&gt;Maps&lt;/em&gt; in JavaScript allow you to use &lt;strong&gt;any&lt;/strong&gt; key type for your &lt;em&gt;key value&lt;/em&gt; pairs.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Using an Array as a key fails&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; regularObject &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;key1a&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;key1b&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;value&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Uncaught SyntaxError: missing ] in computed property name&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; arrayKey &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;key1a&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;key1b&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; regularObject &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;arrayKey&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;value&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Variable name is turned into a string&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; key &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; regularObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token template-string interpolation&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; key&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// arrayKey: string&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; mapObject &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;mapObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;arrayKey&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;value&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Key remains an object&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; key &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; mapObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;keys&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token template-string interpolation&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; key&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// key1a,key1b: object&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As the example above 👆 shows, using an Array in an object literal will simply &lt;strong&gt;fail&lt;/strong&gt; or, in the case of a variable, &lt;strong&gt;convert&lt;/strong&gt; the variable name into a &lt;em&gt;string&lt;/em&gt;.&lt;/p&gt;&lt;h2 id=&quot;caveat&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#caveat&quot; aria-label=&quot;caveat permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Caveat&lt;/h2&gt;&lt;p&gt;This sounds great at first! 🤩 You can easily fetch the value with the &lt;em&gt;appropriate&lt;/em&gt; object.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;mapObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;arrayKey&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// &amp;quot;value&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;However, the key works &lt;strong&gt;by reference&lt;/strong&gt;! This means you need to use the same &lt;em&gt;object reference&lt;/em&gt; to retrieve the same value! As such, if you were to use a &lt;em&gt;&amp;quot;different&amp;quot;&lt;/em&gt; Array, even with the &lt;strong&gt;same values&lt;/strong&gt;, you would retrieve &lt;em&gt;undefined&lt;/em&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; mapObject &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;mapObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;key1a&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;key1b&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;value&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;mapObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;key1a&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;key1b&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// undefined&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This makes Maps &lt;em&gt;initially&lt;/em&gt; more difficult to use as you &lt;strong&gt;may not be able to&lt;/strong&gt; save a reference to the original object that you used as a &lt;em&gt;key&lt;/em&gt;.&lt;/p&gt;&lt;h2 id=&quot;solution&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#solution&quot; aria-label=&quot;solution permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Solution&lt;/h2&gt;&lt;p&gt;How do we solve this? 🤔 You may resort to using a &lt;em&gt;string&lt;/em&gt; by joining the elements of your Array.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; mapObject &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;mapObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;key1a&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;key1b&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;::&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;value&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;mapObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;key1a&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;key1b&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;::&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// &amp;quot;value&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;However, this is &lt;strong&gt;only recommended&lt;/strong&gt; if your Array only contains &lt;em&gt;primitive types&lt;/em&gt; and if you know for &lt;em&gt;CERTAIN&lt;/em&gt; that your separator (in this example &lt;code&gt;::&lt;/code&gt;) does &lt;em&gt;NOT&lt;/em&gt; appear in your Array.&lt;/p&gt;&lt;p&gt;If that is not the case, I &lt;em&gt;recommend&lt;/em&gt; using a library such as &lt;a href=&quot;https://github.com/anko/array-keyed-map&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;array-keyed-map&lt;/a&gt; where someone already figured out the problem.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Pagination Strategies Explained]]></title><description><![CDATA[There are two main approaches to pagination on a database-level: Offset pagination : retrieves the content of a page by  traversing items…]]></description><link>https://dilshankelsen.com/pagination-strategies-explained/</link><guid isPermaLink="false">https://dilshankelsen.com/pagination-strategies-explained/</guid><pubDate>Mon, 19 Dec 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;There are two main approaches to pagination on a database-level:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;&lt;strong&gt;Offset pagination&lt;/strong&gt;: retrieves the content of a page by &lt;em&gt;traversing items&lt;/em&gt; in a list (i.e. the offset). For instance, in a list of 20 items with a page consisting of 5 items each, the database would have to traverse the first 15 items &lt;em&gt;before&lt;/em&gt; reaching the contents of page 4.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cursor-based pagination&lt;/strong&gt;: retrieves the content of a page by &lt;em&gt;using a marker&lt;/em&gt; (i.e. cursor) where the last page ended. This allows the database &lt;strong&gt;to skip&lt;/strong&gt; any items to reach the next page. Using the previous example, to retrieve page 4 one would reference the last item of page 3.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h2 id=&quot;which-one-to-use&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#which-one-to-use&quot; aria-label=&quot;which one to use permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Which one to use&lt;/h2&gt;&lt;p&gt;The main &lt;em&gt;advantage&lt;/em&gt; of the &lt;strong&gt;offset strategy&lt;/strong&gt; is that it enables users to jump to any page of content. However, it&amp;#x27;s biggest &lt;em&gt;downside&lt;/em&gt; is its slow performance on large datasets as it needs to traverse the entire list to retrieve a page.&lt;/p&gt;&lt;p&gt;On the other hand, &lt;strong&gt;cursor-based pagination&lt;/strong&gt; scales well at a database-level, which makes it the preferred choice for (large) applications. However, a downside of this choice is the inability to skip to any pages. Naturally, this makes it more suitable for apps that use &lt;em&gt;infinite scrolling&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;/&amp;gt; &lt;a href=&quot;/pagination-in-prisma-and-graphql/&quot;&gt;Next part&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Pagination In React Apollo Client]]></title><description><![CDATA[Continuing with the theme of fetching  hundreds  of events: In this post, you'll learn how to  implement pagination on the client-side  with…]]></description><link>https://dilshankelsen.com/pagination-in-react-apollo-client/</link><guid isPermaLink="false">https://dilshankelsen.com/pagination-in-react-apollo-client/</guid><pubDate>Thu, 15 Dec 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Continuing with the theme of fetching &lt;em&gt;hundreds&lt;/em&gt; of events: In this post, you&amp;#x27;ll learn how to &lt;strong&gt;implement pagination on the client-side&lt;/strong&gt; with &lt;a href=&quot;https://www.apollographql.com/docs/react/&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;Apollo client&lt;/a&gt; and infinite scrolling.&lt;/p&gt;&lt;div&gt;&lt;p&gt;This example describes the implementation details of &lt;strong&gt;cursor-based&lt;/strong&gt; pagination on the client side. For more information on the &lt;em&gt;database and GraphQL implementation&lt;/em&gt;, check out the previous pagination post.&lt;/p&gt;&lt;p&gt;/&amp;gt; &lt;a href=&quot;/pagination-in-prisma-and-graphql/&quot;&gt;Go to previous post&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;query-document&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#query-document&quot; aria-label=&quot;query document permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Query document&lt;/h2&gt;&lt;p&gt;While the &lt;code&gt;events&lt;/code&gt; query is defined in a separate GraphQL schema file, we need a &lt;strong&gt;query document&lt;/strong&gt; to tell Apollo &lt;em&gt;what event data&lt;/em&gt; we are requesting.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;graphql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-graphql&quot;&gt;GRAPHQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-graphql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token definition-query function&quot;&gt;events&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$filter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token atom-input class-name&quot;&gt;EventsInput&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property-query&quot;&gt;events&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token attr-name&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$filter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token object&quot;&gt;edges&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# More fields, etc...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token object&quot;&gt;pageInfo&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;endCursor&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;hasNextPage&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;initial-query&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#initial-query&quot; aria-label=&quot;initial query permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Initial query&lt;/h2&gt;&lt;p&gt;For the &lt;em&gt;initial request&lt;/em&gt;, we only need to pass in the &lt;strong&gt;limiter as a variable&lt;/strong&gt; to the &lt;code&gt;EventsDocument&lt;/code&gt; query.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  data&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Needed for paginating results later on.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  fetchMore&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  networkStatus&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useQuery&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;EventsDocument&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;variables&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// `LIMIT` may represent any number.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;LIMIT&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;notifyOnNetworkStatusChange&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Make note of &lt;code&gt;notifyOnNetworkStatusChange&lt;/code&gt;. By setting it to &lt;code&gt;true&lt;/code&gt; in the options parameter of &lt;code&gt;useQuery&lt;/code&gt;, we get more &lt;em&gt;fine-grained information&lt;/em&gt; on the network status with &lt;code&gt;networkStatus&lt;/code&gt;. This is necessary to show the appropriate loading indicator, as we will make &lt;strong&gt;two different types of requests&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;For the &lt;strong&gt;initial request&lt;/strong&gt;, we would check for the network status &lt;code&gt;loading&lt;/code&gt;. However, later on, when we &lt;em&gt;fetch more data&lt;/em&gt; using &lt;code&gt;fetchMore&lt;/code&gt;, we would check a status of &lt;code&gt;fetchMore&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;networkStatus &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;NetworkStatus&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;loading&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;data &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;events&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;SpinningCircle&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;displaying-data&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#displaying-data&quot; aria-label=&quot;displaying data permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Displaying data&lt;/h2&gt;&lt;p&gt;Displaying the data is as simple as iterating it with the &lt;code&gt;map&lt;/code&gt; method. No magic here.&lt;/p&gt;&lt;p&gt;However, keep in mind we would like to implement &lt;strong&gt;infinite scrolling&lt;/strong&gt;. This means when we approach the final event in the list, we need to fetch more events. &lt;em&gt;How do we do this&lt;/em&gt;?&lt;/p&gt;&lt;p&gt;There&amp;#x27;s a great library called &lt;a href=&quot;https://www.npmjs.com/package/react-waypoint&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;React Waypoint&lt;/a&gt; that allows us to &lt;strong&gt;trigger a function&lt;/strong&gt; as soon as a Waypoint element (i.e. after the final event) &lt;em&gt;enters the viewport&lt;/em&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;events&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;edges&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; index&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; thisArray&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript property-access&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;EventCard&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;/* Add a Waypoint element after the last element */&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;index &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; thisArray&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Waypoint&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;onEnter&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;fetchMoreEvents&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;networkStatus &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;NetworkStatus&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;fetchMore&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;SpinningCircle&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;When the Waypoint element enters the viewport, it triggers the &lt;code&gt;fetchMoreEvents&lt;/code&gt; function, which calls the &lt;code&gt;fetchMore&lt;/code&gt; function from &lt;code&gt;useQuery&lt;/code&gt;.
Notice how we &lt;em&gt;display a loading spinner&lt;/em&gt; when we fetch more data and check the network for a &lt;code&gt;fetchMore&lt;/code&gt; &lt;strong&gt;status&lt;/strong&gt;.&lt;/p&gt;&lt;h2 id=&quot;fetching-paginated-data&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#fetching-paginated-data&quot; aria-label=&quot;fetching paginated data permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Fetching paginated data&lt;/h2&gt;&lt;p&gt;After &lt;code&gt;fetchMoreEvents&lt;/code&gt; is triggered, we call &lt;code&gt;fetchMore&lt;/code&gt; only once we&amp;#x27;ve confirmed that there is actually &lt;strong&gt;more data&lt;/strong&gt; to be found using &lt;code&gt;hasNextPage&lt;/code&gt;. Once we fetch, we pass in the &lt;em&gt;cursor information&lt;/em&gt; in addition to the limiter.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;events&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; edges&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; pageInfo &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; data&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;fetchMoreEvents&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;pageInfo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;hasNextPage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;fetchMore&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;variables&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;LIMIT&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;cursor&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; pageInfo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;endCursor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;updateQuery&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;prevResult&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; fetchMoreResult &lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      fetchMoreResult&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;events&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;edges&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token spread operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;prevResult&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;events&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;edges&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token spread operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;fetchMoreResult&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;events&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;edges&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; fetchMoreResult&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Pay special attention to &lt;code&gt;updateQuery&lt;/code&gt;. ⚠ &lt;strong&gt;By default&lt;/strong&gt;, if we query new data, our existing events will simply be overwritten by the new data. We do not want this!&lt;/p&gt;&lt;p&gt;Instead, we want to &lt;strong&gt;merge the our existing data into to newly fetched set of data&lt;/strong&gt;. In the function, we are merging our existing results &lt;code&gt;prevResults.events.edges&lt;/code&gt; into our new results &lt;code&gt;fetchMoreResult.events.edges&lt;/code&gt;, which we then &lt;em&gt;return&lt;/em&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Pagination In Prisma And GraphQL]]></title><description><![CDATA[Assume we are retrieving  hundreds  of events for a website and we do so by  paginating  the results. This example describes the…]]></description><link>https://dilshankelsen.com/pagination-in-prisma-and-graphql/</link><guid isPermaLink="false">https://dilshankelsen.com/pagination-in-prisma-and-graphql/</guid><pubDate>Mon, 12 Dec 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Assume we are retrieving &lt;em&gt;hundreds&lt;/em&gt; of events for a website and we do so by &lt;em&gt;paginating&lt;/em&gt; the results.&lt;/p&gt;&lt;div&gt;&lt;p&gt;This example describes the implementation details of &lt;strong&gt;cursor-based&lt;/strong&gt; pagination on the server side.&lt;/p&gt;&lt;/div&gt;&lt;p&gt;Designing the GraphQL schema, we would have an &lt;code&gt;events&lt;/code&gt; &lt;strong&gt;query&lt;/strong&gt; that:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Accepts an &lt;em&gt;input&lt;/em&gt; of type &lt;code&gt;EventsInput&lt;/code&gt;, and&lt;/li&gt;&lt;li&gt;Returns an &lt;em&gt;output&lt;/em&gt; of type &lt;code&gt;EventsResponse&lt;/code&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;graphql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-graphql&quot;&gt;GRAPHQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-graphql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Query&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token attr-name&quot;&gt;events&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token attr-name&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token atom-input class-name&quot;&gt;EventsInput&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;EventsResponse&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;query-input&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#query-input&quot; aria-label=&quot;query input permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Query input&lt;/h3&gt;&lt;p&gt;The input would consist of &lt;strong&gt;two variables&lt;/strong&gt;:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;first&lt;/code&gt; - how many records to &lt;em&gt;limit the query&lt;/em&gt; to, and&lt;/li&gt;&lt;li&gt;&lt;code&gt;cursor&lt;/code&gt; - which record to start the query from (&lt;strong&gt;fundamental principle&lt;/strong&gt; behind cursor-based pagination).&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;The cursor value is &lt;strong&gt;optional&lt;/strong&gt; as it&amp;#x27;s not required to query the &lt;em&gt;very first page&lt;/em&gt; of results.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;graphql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-graphql&quot;&gt;GRAPHQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-graphql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token atom-input class-name&quot;&gt;EventsInput&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token attr-name&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token scalar&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token attr-name&quot;&gt;cursor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token scalar&quot;&gt;ID&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;query-output&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#query-output&quot; aria-label=&quot;query output permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Query output&lt;/h3&gt;&lt;p&gt;The output of the query would contain the &lt;em&gt;queried results&lt;/em&gt; in the &lt;code&gt;edges&lt;/code&gt; field. The &lt;code&gt;pageInfo&lt;/code&gt; field contains info on whether there is &lt;strong&gt;another&lt;/strong&gt; page/batch of events available as well as what the &lt;em&gt;ID of the cursor&lt;/em&gt; (i.e. final record) of the current page is.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;graphql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-graphql&quot;&gt;GRAPHQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-graphql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;EventsResponse&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token attr-name&quot;&gt;edges&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Event&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token attr-name&quot;&gt;pageInfo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;PageInfo&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;PageInfo&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token attr-name&quot;&gt;endCursor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token scalar&quot;&gt;ID&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token attr-name&quot;&gt;hasNextPage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token scalar&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Similarly, the cursor is &lt;em&gt;nullable&lt;/em&gt; as we do not expect one in an &lt;em&gt;empty&lt;/em&gt; events query.&lt;/p&gt;&lt;h2 id=&quot;prisma&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#prisma&quot; aria-label=&quot;prisma permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Prisma&lt;/h2&gt;&lt;p&gt;After defining the GraphQL schema of paginated event data, we need to take care of actually &lt;strong&gt;retrieving and returning&lt;/strong&gt; the data. This happens in the &lt;code&gt;events&lt;/code&gt; &lt;em&gt;resolver&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;The &lt;strong&gt;1&lt;sup&gt;st&lt;/sup&gt; step&lt;/strong&gt; consists of querying all events using the &lt;em&gt;pagination properties&lt;/em&gt; the resolver receives.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Resolver&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;events&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;_parent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; input &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; _context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; first&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; cursor &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; input&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; events &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; prisma&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;findMany&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Limit the number of events returned by this query.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    take&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; first&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Conditionally use a cursor if it exists.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;cursor &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      skip&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Do not include the cursor itself in the query result.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      cursor&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        id&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; cursor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If no results are retrieved, it means we&amp;#x27;ve reached &lt;em&gt;the end of the pagination&lt;/em&gt; (... or perhaps there&amp;#x27;s &lt;strong&gt;no events&lt;/strong&gt; in the database 😆) and we return &lt;em&gt;early&lt;/em&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;events&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;length &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    edges&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    pageInfo&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      endCursor&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      hasNextPage&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If there is data, the next step is to &lt;strong&gt;identify the cursor&lt;/strong&gt; of that batch of results. Then we use said cursor to &lt;em&gt;peak into the next page&lt;/em&gt;. If that query contains any events, it means that there is another page of results.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; newCursor &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; events&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;events&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;length &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; nextPage &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; prisma&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;findMany&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Same as before, limit the number of events returned by this query.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    take&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; first&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    skip&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Do not include the cursor itself in the query result.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    cursor&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      id&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; newCursor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    edges&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; events&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    pageInfo&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      endCursor&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; newCursor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      hasNextPage&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; nextPage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;length &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Lastly, we &lt;strong&gt;return&lt;/strong&gt; any information we retrieved (&lt;em&gt;including the existence&lt;/em&gt; of a next page) in the GraphQL query.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[A Simple Development SMTP Server For Linux]]></title><description><![CDATA[If you're looking for a  simple  SMTP server on  Linux or WSL , MailCatcher is a great solution. To install it, you'll first need its…]]></description><link>https://dilshankelsen.com/simple-development-smtp-server-for-linux/</link><guid isPermaLink="false">https://dilshankelsen.com/simple-development-smtp-server-for-linux/</guid><pubDate>Thu, 08 Dec 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;If you&amp;#x27;re looking for a &lt;em&gt;simple&lt;/em&gt; SMTP server on &lt;strong&gt;Linux or WSL&lt;/strong&gt;, MailCatcher is a great solution. To install it, you&amp;#x27;ll first need its dependencies, which includes
&lt;em&gt;Ruby&lt;/em&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; update&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; -y build-essential software-properties-common&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; -y libsqlite3-dev ruby-dev&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If these commands &lt;em&gt;fail&lt;/em&gt;, you may need to &lt;strong&gt;prepend&lt;/strong&gt; &lt;code&gt;sudo&lt;/code&gt;. Then install &lt;em&gt;MailCatcher&lt;/em&gt; as a Ruby gem,&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;gem &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; mailcatcher&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and launch it with:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;mailcatcher&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;em&gt;email client&lt;/em&gt; is then available at &lt;code&gt;http://127.0.0.1:1080&lt;/code&gt;. The &lt;strong&gt;SMTP server&lt;/strong&gt; itself is reachable at &lt;code&gt;127.0.0.1:1025&lt;/code&gt;. A password and username are &lt;em&gt;not&lt;/em&gt; needed to use the server.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Set A Default NVM Node.js Version]]></title><description><![CDATA[You can set a  specific  Node version as your  default  with: Of course, the specified version must  already  be  installed . If not, run:]]></description><link>https://dilshankelsen.com/set-default-nvm-nodejs-version/</link><guid isPermaLink="false">https://dilshankelsen.com/set-default-nvm-nodejs-version/</guid><pubDate>Mon, 05 Dec 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;You can set a &lt;strong&gt;specific&lt;/strong&gt; Node version as your &lt;em&gt;default&lt;/em&gt; with:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;nvm &lt;/span&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; default &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# nvm alias default 14&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# nvm alias default 16.17.0&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Of course, the specified version must &lt;em&gt;already&lt;/em&gt; be &lt;strong&gt;installed&lt;/strong&gt;. If not, run:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;nvm &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# nvm install 14&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# nvm install 16.17.0&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Sentry Not Receiving Any Error Logs]]></title><description><![CDATA[If you have Sentry  set up correctly  in your project and you are not receiving any error logs in the dashboard, your  adblocker  may be…]]></description><link>https://dilshankelsen.com/sentry-not-receiving-any-error-logs/</link><guid isPermaLink="false">https://dilshankelsen.com/sentry-not-receiving-any-error-logs/</guid><pubDate>Thu, 01 Dec 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;If you have Sentry &lt;em&gt;set up correctly&lt;/em&gt; in your project and you are not receiving any error logs in the dashboard, your &lt;strong&gt;adblocker&lt;/strong&gt; may be preventing ✋ Sentry from reporting any errors.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Get All AWS Resources Via API]]></title><description><![CDATA[AWS provides  an SDK  called  @aws-sdk/client-config-service , which allows you to  query all AWS resource s using an  SQL command . To…]]></description><link>https://dilshankelsen.com/get-all-aws-config-items/</link><guid isPermaLink="false">https://dilshankelsen.com/get-all-aws-config-items/</guid><pubDate>Mon, 28 Nov 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;AWS provides &lt;a href=&quot;https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-config-service/index.html&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;an SDK&lt;/a&gt; called &lt;code&gt;@aws-sdk/client-config-service&lt;/code&gt;, which allows you to &lt;strong&gt;query all AWS resource&lt;/strong&gt;s using an &lt;em&gt;SQL command&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;To start off, &lt;em&gt;install&lt;/em&gt; the SDK with a package manager of your choice.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; @aws-sdk/client-config-service&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;yarn&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; @aws-sdk/client-config-service&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then initialize the &lt;strong&gt;Config Service Client&lt;/strong&gt; class which takes &lt;a href=&quot;https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-config-service/interfaces/configserviceclientconfig.html&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;several arguments&lt;/a&gt;, with your &lt;em&gt;account credentials&lt;/em&gt; and &lt;em&gt;region&lt;/em&gt; being the essential ones.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; configClient &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;ConfigServiceClient&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  region&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  credentials&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The next, and &lt;strong&gt;most important step&lt;/strong&gt;, is to initialize &lt;code&gt;SelectResourceConfigCommand&lt;/code&gt; by passing &lt;a href=&quot;https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-config-service/interfaces/selectresourceconfigcommandinput.html&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;an SQL SELECT query&lt;/a&gt; into the class&amp;#x27; constructor.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; params&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; SelectResourceConfigCommandInput &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  Expression&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;SELECT *&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Get ALL resources&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; command &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SelectResourceConfigCommand&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;p&gt;In the &lt;strong&gt;AWS Config page&lt;/strong&gt; under &lt;em&gt;Advanced queries&lt;/em&gt; (located in the sidebar), you may play around with SELECT queries to &lt;em&gt;figure out&lt;/em&gt; how to query the resources you need.&lt;/p&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:700px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:67.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAAsTAAALEwEAmpwYAAAC1ElEQVQ4y1VT2W7UMBTNbyLEC2JRqbpIULoIdaGA2lJKW4kifoA3BE88IfEPRV0pbZmZxElm4kmcxc7iHK7dTgFLR3Hsc0/uPffGeb6+jbXtD9jY3sOrzV0sre9g9fUOXmxs32B1bcverW6+x/ePKzj9MouDz/PY/zSPrR3ib+7h5Zt3WNnYhZNlGUarJVyyPrwgRLfXA+fcnja6QdMY1EjLFrEEYgUkhOr63IJ4zjBOoFSJtm2RphlOj49xeHSEHweHODi7xEXXg+u66F3DdXtgzAXz6Om58DzPftz3A8RxDEcIcZNhQcIngcCZN0CPCzChwBKJMJUIbqAQZBWhRD8vMcgrRAW9pyUikcHhSWpLNUuWV4InHR/nXojeIEZ3hH6MXpSgE0ToMR9dP0QgJCJZW8G+ERY5nIRdoJa5FSxJMAh8uB6D5/tIyd+yquy5UhKq1ii5i/rbW5T7X5GpCkWeQ5BVIk0t36mKlHzXVlAphbwo7KWUEgXtDSmjoLquUZG4NBw6Mx8wnJzuSnX1NHFOOOCWNBI02fA0B88UEioly2jPhxBkTZFLEq1tYNNoykMTR6HDc6iqoWSoZD6MbYetIImZ8RjkNTWkREWJp5mAFzIEPAQXHLUZD8r2OgRCNTjpF/CIb6pzEpFakb+CNXhBRpPJZiVZAi9iGKQD9EWfJkHa0kdJJNSUgyCHn14LSqksYSRovMrKxo5DpVuYwfdZYBEPE3tvrBkJClXjKMzRp1GSkgTNr0BLG5CXmsQ1kfQgK3XVtDqKIn3261z7QaAjzjVVo6kZmgLIxFbnZa29RGkh65Yaox38s7S+6nZBDgwV/XK0z6gBl7876HS6NFLhf7wR9yevMCxhK3Wm5pb96bkV9nB6gd2fnGMPpubZrbFZdmd8lt2fWmB3x2fY7XuTdD7Hpog38XSJPXr8jE3OLrNJs3+yyMZmltj4zJI/MbN4+Qc5if5J6ubsWgAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;AWS Config page&quot; title=&quot;AWS Config page&quot; src=&quot;/static/8177fd7e9213f1631fcebb17c393be57/8c557/image-1.png&quot; srcSet=&quot;/static/8177fd7e9213f1631fcebb17c393be57/772e8/image-1.png 200w,/static/8177fd7e9213f1631fcebb17c393be57/e17e5/image-1.png 400w,/static/8177fd7e9213f1631fcebb17c393be57/8c557/image-1.png 700w&quot; sizes=&quot;(max-width: 700px) 100vw, 700px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Click the &amp;quot;New query&amp;quot; button to start a query&lt;/figcaption&gt;&lt;p&gt;Last, but not least, &lt;strong&gt;send your query&lt;/strong&gt; to AWS and receive a response with your &lt;a href=&quot;https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-config-service/interfaces/selectresourceconfigcommandoutput.html&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;&lt;em&gt;requested data&lt;/em&gt;&lt;/a&gt; of type &lt;code&gt;SelectResourceConfigCommandOutput&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; response &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; client&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;command&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Apply And Delete A Git Stash At The Same Time]]></title><description><![CDATA[One can  apply and delete  the latest stash  simultaneously  with the  pop  command. If you wish to pop a  specific  stash:]]></description><link>https://dilshankelsen.com/apply-and-delete-git-stash-at-same-time/</link><guid isPermaLink="false">https://dilshankelsen.com/apply-and-delete-git-stash-at-same-time/</guid><pubDate>Thu, 24 Nov 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;One can &lt;strong&gt;apply and delete&lt;/strong&gt; the latest stash &lt;em&gt;simultaneously&lt;/em&gt; with the &lt;code&gt;pop&lt;/code&gt; command.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; stash pop&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you wish to pop a &lt;em&gt;specific&lt;/em&gt; stash:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; stash pop stash@&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Delete Database In Prisma From Terminal]]></title><description><![CDATA[In  Prisma , you can  delete and recreate  your database with: Be sure to only use this command in  development  environments]]></description><link>https://dilshankelsen.com/delete-database-in-prisma-from-terminal/</link><guid isPermaLink="false">https://dilshankelsen.com/delete-database-in-prisma-from-terminal/</guid><pubDate>Mon, 21 Nov 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In &lt;a href=&quot;https://www.prisma.io/docs/reference/api-reference/command-reference#migrate-reset&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;Prisma&lt;/a&gt;, you can &lt;strong&gt;delete and recreate&lt;/strong&gt; your database with:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ prisma migrate reset&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;p&gt;Be sure to only use this command in &lt;em&gt;development&lt;/em&gt; environments&lt;/p&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Validate File Size With Yup]]></title><description><![CDATA[Yup does not provide any  out of the box  validation for files. Fortunately, one can use  tests a.k.a assertions  to provide custom…]]></description><link>https://dilshankelsen.com/validate-file-size-with-yup/</link><guid isPermaLink="false">https://dilshankelsen.com/validate-file-size-with-yup/</guid><pubDate>Thu, 17 Nov 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Yup does not provide any &lt;em&gt;out of the box&lt;/em&gt; validation for files. Fortunately, one can use &lt;strong&gt;tests a.k.a assertions&lt;/strong&gt; to provide custom validation. Let&amp;#x27;s take a look 👀 at the &lt;a href=&quot;https://github.com/jquense/yup#schematestname-string-message-string--function--any-test-function-schema&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;function definition&lt;/a&gt; of such an assertion.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-text&quot;&gt;TEXT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-text&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Schema.test(name: string, message: string | function | any, test: function): Schema&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In the &lt;strong&gt;1&lt;sup&gt;st&lt;/sup&gt; parameter&lt;/strong&gt;, we pass in a name for the test, e.g. &lt;code&gt;fileSize&lt;/code&gt;. The &lt;strong&gt;2&lt;sup&gt;nd&lt;/sup&gt; parameter&lt;/strong&gt; takes in a message that would be displayed in case of a &lt;em&gt;failed&lt;/em&gt; ❌ assertion. Lastly, the &lt;strong&gt;final parameter&lt;/strong&gt; is a function that evaluates the passed in value (&lt;em&gt;in this case&lt;/em&gt; a file) and returns a boolean.&lt;/p&gt;&lt;p&gt;Here&amp;#x27;s a &lt;em&gt;code example&lt;/em&gt; of how one would implement the custom file validation.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; schema&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; yup&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;SchemaOf&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;ExampleFormInput&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; yup&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// ... other fields&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  fileUpload&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; yup&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token generic-function function&quot;&gt;mixed&lt;/span&gt;&lt;span class=&quot;token generic-function generic class-name operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token generic-function generic class-name&quot;&gt;FileList&lt;/span&gt;&lt;span class=&quot;token generic-function generic class-name operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Pass in the type of `fileUpload`&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;fileSize&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Only documents up to 2MB are permitted.&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      files &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;files &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Check if `files` is defined&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        files&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;length &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Check if `files` is not an empty list&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;files&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;every&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;file &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; file&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;size &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2_000_000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We define the &lt;code&gt;fileUpload&lt;/code&gt; field of type &lt;em&gt;mixed&lt;/em&gt; and pass in a generic &lt;code&gt;FileList&lt;/code&gt; type. If you were to &lt;strong&gt;inspect/log&lt;/strong&gt; the file data you obtain from your input tag, it would look as follows: &lt;code&gt;[ File, File ]&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Concerning the validation logic, we &lt;em&gt;first&lt;/em&gt; check if &lt;code&gt;files&lt;/code&gt; is defined. If it is, then we check if any files exists in the list. Lastly, we &lt;strong&gt;convert&lt;/strong&gt; the file list into an array so we can run the &lt;code&gt;every&lt;/code&gt; method on it to check if &lt;strong&gt;all&lt;/strong&gt; the uploaded files are &lt;strong&gt;below 2MB&lt;/strong&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[TypeScript "as const" explained]]></title><description><![CDATA[as const  in TypeScript is known as  const assertion . By appending it to a  literal value  (e.g. object literals, string literals or array…]]></description><link>https://dilshankelsen.com/typescript-as-const-explained/</link><guid isPermaLink="false">https://dilshankelsen.com/typescript-as-const-explained/</guid><pubDate>Mon, 14 Nov 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;code&gt;as const&lt;/code&gt; in TypeScript is known as &lt;strong&gt;const assertion&lt;/strong&gt;. By appending it to a &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#literals&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;literal value&lt;/a&gt; (e.g. object literals, string literals or array literals), we tell the TypeScript compiler to assume &lt;strong&gt;the most narrow type definition&lt;/strong&gt; for that value.&lt;/p&gt;&lt;p&gt;This can be best shown with an &lt;em&gt;example&lt;/em&gt;:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; fruits &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;apple&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;banana&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// const fruits: string[]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; vegetables &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;lettuce&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;broccoli&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;beans&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// const vegetables: readonly [&amp;quot;lettuce&amp;quot;, &amp;quot;broccoli&amp;quot;, &amp;quot;beans&amp;quot;]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In the first array, TypeScript &lt;em&gt;assumes&lt;/em&gt; that &lt;code&gt;fruits&lt;/code&gt; is an array of strings. However, in the 2&lt;sup&gt;nd&lt;/sup&gt; array with the use of a const assertion, TypeScript &lt;em&gt;assumes&lt;/em&gt; &lt;code&gt;vegetables&lt;/code&gt; is an array with 3 elements of type &lt;code&gt;lettuce&lt;/code&gt;, &lt;code&gt;broccoli&lt;/code&gt; and &lt;code&gt;beans&lt;/code&gt;, in that &lt;strong&gt;exact&lt;/strong&gt; order.&lt;/p&gt;&lt;p&gt;&lt;em&gt;Most importantly&lt;/em&gt;, however, it will render them &lt;strong&gt;immutable&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;vegetables&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;corn&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Cannot assign to &amp;#x27;2&amp;#x27; because it is a read-only property.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;vegetables&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;corn&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Property &amp;#x27;push&amp;#x27; does not exist on type &amp;#x27;readonly [&amp;quot;lettuce&amp;quot;, &amp;quot;broccoli&amp;quot;, &amp;quot;beans&amp;quot;]&amp;#x27;.&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Yet, be aware that there exist a &lt;a href=&quot;https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#caveats-1&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;few exceptions&lt;/a&gt; to this rule of immutability.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Solve NextAuth Error No Secret In Production]]></title><description><![CDATA[If you get the following error by  NextAuth  in  production , it means you are  missing  a secret that is used for various  cryptographic…]]></description><link>https://dilshankelsen.com/solve-next-auth-error-no-secret-in-production/</link><guid isPermaLink="false">https://dilshankelsen.com/solve-next-auth-error-no-secret-in-production/</guid><pubDate>Thu, 10 Nov 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;If you get the following error by &lt;em&gt;NextAuth&lt;/em&gt; in &lt;strong&gt;production&lt;/strong&gt;,&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;zsh&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-zsh&quot;&gt;ZSH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-zsh&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;MissingSecret [MissingSecretError]: Please define a `secret` in production.&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;it means you are &lt;strong&gt;missing&lt;/strong&gt; a secret that is used for various &lt;em&gt;cryptographic tasks&lt;/em&gt; such as signing cookies, creating keys, etc.
By &lt;em&gt;default&lt;/em&gt;, one does not need to set a secret in development mode as a hash is &lt;strong&gt;automatically generated&lt;/strong&gt; using the &lt;code&gt;options&lt;/code&gt; object passed into the &lt;a href=&quot;https://next-auth.js.org/configuration/initialization#simple-initialization&quot; rel=&quot;noopener noreferrer&quot;&gt;NextAuth function&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;In production, however, you must explicitly set one using the &lt;strong&gt;environment variable&lt;/strong&gt; &lt;code&gt;NEXTAUTH_SECRET&lt;/code&gt;. You can easily &lt;em&gt;generate a secret&lt;/em&gt; with the command below.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;ZSH&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-ZSH&quot;&gt;ZSH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-ZSH&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ openssl rand -base64 32&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[The Difference Between Iterable And Enumerable In JavaScript]]></title><description><![CDATA[The main difference between iterable and enumerable is that the former  applies to   values  and the latter to  properties . Let's start…]]></description><link>https://dilshankelsen.com/difference-between-iterable-enumarable-in-javascript/</link><guid isPermaLink="false">https://dilshankelsen.com/difference-between-iterable-enumarable-in-javascript/</guid><pubDate>Mon, 07 Nov 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The main difference between iterable and enumerable is that the former &lt;em&gt;applies to&lt;/em&gt; &lt;strong&gt;values&lt;/strong&gt; and the latter to &lt;strong&gt;properties&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;Let&amp;#x27;s start with an &lt;strong&gt;iterable object&lt;/strong&gt;. When an object is iterable, it means that it has an iterator. An iterator is simply another object that &lt;em&gt;gives you access 🔑 to the values of the object it&amp;#x27;s attached to&lt;/em&gt;. &lt;strong&gt;Examples&lt;/strong&gt; of objects with an iterator are arrays and strings.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; animals &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;cat&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;bear&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;mouse&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;/* Iterate an array */&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; animal &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;of&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; animals&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;animal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// cat&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// bear&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// mouse&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;/* Iterate a string */&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; letter &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;of&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;cat&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;letter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// c&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// a&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// t&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Using a &lt;code&gt;for...of&lt;/code&gt; loop, we can &lt;strong&gt;iterate all the values&lt;/strong&gt; of the &lt;code&gt;animals&lt;/code&gt; array and the string &lt;code&gt;cat&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;&lt;em&gt;On the other hand&lt;/em&gt;, 👀 when we talk about enumerability, we talk about &lt;strong&gt;going over the properties&lt;/strong&gt; of an object. Take again the first example from before and let&amp;#x27;s apply a &lt;code&gt;for...in&lt;/code&gt; loop instead.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; animals &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;cat&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;bear&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;mouse&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;/* Enumerate over an array */&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; animal &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; animals&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;animal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// 0&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// 1&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// 2&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;em&gt;Instead&lt;/em&gt; of getting the values of the array, we get &lt;strong&gt;the property, i.e. key&lt;/strong&gt;, of each value instead. In an array or string, it&amp;#x27;s simply the &lt;em&gt;index&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;If we were to apply this to a &lt;em&gt;regular&lt;/em&gt; object with string properties,&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; person &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;   &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;19&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;   &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Jack&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;   &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;70kg&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;/* Enumerate over an object */&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; key &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; person&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// age&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// name&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// weight&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;we would &lt;strong&gt;get back the names of the properties&lt;/strong&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Refresh GraphQL Data After Mutation In Apollo Client]]></title><description><![CDATA[Imagine this : you have queried data and you just performed a mutation that  alters  the queried data. Naturally, you want the data to…]]></description><link>https://dilshankelsen.com/refresh-graphql-data-after-mutation-in-apollo-client/</link><guid isPermaLink="false">https://dilshankelsen.com/refresh-graphql-data-after-mutation-in-apollo-client/</guid><pubDate>Thu, 03 Nov 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;em&gt;Imagine this&lt;/em&gt;: you have queried data and you just performed a mutation that &lt;strong&gt;alters&lt;/strong&gt; the queried data. Naturally, you want the data to &lt;em&gt;update&lt;/em&gt; after said mutation.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// `data` stays the same after `deleteEvent` is called.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; data &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useQuery&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;EventsDocument&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;deleteEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useMutation&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;DeleteEventDocument&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;By &lt;strong&gt;default&lt;/strong&gt;, this will not happen. To &lt;em&gt;enable it&lt;/em&gt;, you need to specify in the mutation&amp;#x27;s options field &lt;code&gt;refetchQueries&lt;/code&gt; the appropriate values, i.e. the GraphQL document and query name.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// `data` updates after `deleteEvent` is finished executing.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; data &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useQuery&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;EventsDocument&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;deleteEvent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useMutation&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;DeleteEventDocument&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;refetchQueries&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;EventsDocument&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Document node object used in the query&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;events&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Name of the query&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Package-lock.json Rewritten After Dependencies Installation]]></title><description><![CDATA[If you find yourself having your entire  package-lock.json  file being rewritten after running  npm install , you may be using an incorrect…]]></description><link>https://dilshankelsen.com/package-lock-rewritten-after-dependencies-installation/</link><guid isPermaLink="false">https://dilshankelsen.com/package-lock-rewritten-after-dependencies-installation/</guid><pubDate>Mon, 31 Oct 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;If you find yourself having your entire &lt;code&gt;package-lock.json&lt;/code&gt; file being rewritten after running &lt;code&gt;npm install&lt;/code&gt;, you may be using an incorrect ❌ node version for your project.&lt;/p&gt;&lt;p&gt;You can check your currently used version with &lt;code&gt;node -v&lt;/code&gt; in your terminal and compare it to the supported node version in the &lt;code&gt;engines&lt;/code&gt; field of your &lt;code&gt;package.json&lt;/code&gt; file.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Enum Arrays In PostgreSQL Explained]]></title><description><![CDATA[Enumerated types, a.k.a  enums , represent a  set of predefined values . For instance, you could represent programming languages with an…]]></description><link>https://dilshankelsen.com/enum-arrays-in-postgresql-explained/</link><guid isPermaLink="false">https://dilshankelsen.com/enum-arrays-in-postgresql-explained/</guid><pubDate>Thu, 27 Oct 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Enumerated types, a.k.a &lt;em&gt;enums&lt;/em&gt;, represent a &lt;strong&gt;set of predefined values&lt;/strong&gt;. For instance, you could represent programming languages with an enum.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;sql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-sql&quot;&gt;SQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-sql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;CREATE&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;TYPE&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; programming_language &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;AS&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;ENUM&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;java&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;ruby&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;javascript&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;php&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;CREATE&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;TABLE&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; employees &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# ...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;language&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; programming_language&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;id&lt;/th&gt;&lt;th&gt;developer&lt;/th&gt;&lt;th&gt;language&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;Jack&lt;/td&gt;&lt;td&gt;java&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;Stan&lt;/td&gt;&lt;td&gt;ruby&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;Attempting to enter a value that is &lt;em&gt;not&lt;/em&gt; defined in the Enum set will result in an &lt;strong&gt;error&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;sql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-sql&quot;&gt;SQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-sql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;INSERT&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;INTO&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; employees &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;VALUES&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Ellie&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;rust&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# ERROR: invalid input value for enum programming_language: &amp;quot;rust&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;storing-enum-arrays&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#storing-enum-arrays&quot; aria-label=&quot;storing enum arrays permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Storing enum arrays&lt;/h2&gt;&lt;p&gt;Certain databases such as &lt;strong&gt;PostgreSQL&lt;/strong&gt; &lt;em&gt;support&lt;/em&gt; the storage of enum arrays. That is, &lt;em&gt;instead&lt;/em&gt; of being restricted to storing a single enum value in a single cell, you may store &lt;strong&gt;multiple&lt;/strong&gt;. To enable this feature, make the &lt;em&gt;following&lt;/em&gt; changes in the &lt;code&gt;CREATE TABLE&lt;/code&gt; query.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;sql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-sql&quot;&gt;SQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-sql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;CREATE&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;TABLE&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; employees &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# ...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    languages programming_language ARRAY &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In the database, arrays are represented by &lt;code&gt;{}&lt;/code&gt; &lt;strong&gt;curly braces&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;sql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-sql&quot;&gt;SQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-sql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;INSERT&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;INTO&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; employees &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;VALUES&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Jack&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;{java,javascript}&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;INSERT&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;INTO&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; employees &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;VALUES&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Stan&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;{ruby}&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;id&lt;/th&gt;&lt;th&gt;developer&lt;/th&gt;&lt;th&gt;languages&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;Jack&lt;/td&gt;&lt;td&gt;{java,javascript}&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;Stan&lt;/td&gt;&lt;td&gt;{ruby}&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;Likewise, attempting to insert an &lt;em&gt;invalid&lt;/em&gt; enum value into the array will be caught by the &lt;strong&gt;database constraint&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;sql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-sql&quot;&gt;SQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-sql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;INSERT&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;INTO&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; employees &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;VALUES&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Ellie&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;{rust, javascript}&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# ERROR: invalid input value for enum programming_language: &amp;quot;rust&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;updating-enum-arrays&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#updating-enum-arrays&quot; aria-label=&quot;updating enum arrays permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Updating enum arrays&lt;/h3&gt;&lt;p&gt;You can &lt;em&gt;update&lt;/em&gt; your array with the following command:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;sql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-sql&quot;&gt;SQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-sql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;UPDATE&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; employees &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;SET&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; languages&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;php&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;WHERE&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; developer&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Jack&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This will insert &lt;code&gt;php&lt;/code&gt; in 2&lt;sup&gt;nd&lt;/sup&gt; spot and &lt;strong&gt;overwrite&lt;/strong&gt; any pre-existing values.&lt;/p&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;id&lt;/th&gt;&lt;th&gt;developer&lt;/th&gt;&lt;th&gt;languages&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;Jack&lt;/td&gt;&lt;td&gt;{java,php}&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;Stan&lt;/td&gt;&lt;td&gt;{ruby}&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;</content:encoded></item><item><title><![CDATA[Database Indexing Explained]]></title><description><![CDATA[During a query, a database will scan  all  the rows of a table to find the matching data. Indexing serves the purpose of  speeding up SQL…]]></description><link>https://dilshankelsen.com/database-indexing-explained/</link><guid isPermaLink="false">https://dilshankelsen.com/database-indexing-explained/</guid><pubDate>Mon, 24 Oct 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;During a query, a database will scan &lt;em&gt;all&lt;/em&gt; the rows of a table to find the matching data. Indexing serves the purpose of &lt;strong&gt;speeding up SQL searches/queries&lt;/strong&gt; and &lt;em&gt;functions the same&lt;/em&gt; as a book 📚 index. With the latter, you would &lt;em&gt;refer to the index&lt;/em&gt; to find out on which page your desired content is located.&lt;/p&gt;&lt;div&gt;&lt;p&gt;Be aware that indexing comes at a &lt;em&gt;cost&lt;/em&gt;. Any table with an index will take longer to update simply because &lt;em&gt;the index itself&lt;/em&gt; needs to be updated as well.&lt;/p&gt;&lt;/div&gt;&lt;p&gt;In a database, you create an index for one or more columns as follows.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;sql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-sql&quot;&gt;SQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-sql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;CREATE&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;INDEX&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; index_name &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;ON&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; table_name &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;column_name_1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; column_name_2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Pay attention to the &lt;strong&gt;order of the columns&lt;/strong&gt; when creating your index as it will look up &lt;em&gt;the first column&lt;/em&gt; in the index, then the next one, and so on in a query. Depending on which column data you require, it can have an &lt;em&gt;impact&lt;/em&gt; on your query speed 💨.&lt;/p&gt;&lt;p&gt;Furthermore, any &lt;strong&gt;non-indexed column will benefit&lt;/strong&gt; from the increase in speed if it&amp;#x27;s &lt;em&gt;looked up together&lt;/em&gt; with an indexed column.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;sql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-sql&quot;&gt;SQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-sql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;COUNT&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; table_name&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;WHERE&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; non_indexed_column &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;value 1&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;AND&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; indexed_column &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;value 2&amp;#x27;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Delete A Project In Google Cloud]]></title><description><![CDATA[In your  Google Cloud console , select the project  you would like to delete  from the top left corner of the navigation bar. Once selected…]]></description><link>https://dilshankelsen.com/delete-project-in-google-cloud/</link><guid isPermaLink="false">https://dilshankelsen.com/delete-project-in-google-cloud/</guid><pubDate>Thu, 20 Oct 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In your &lt;a href=&quot;https://console.cloud.google.com/&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;Google Cloud console&lt;/a&gt;, select the project &lt;strong&gt;you would like to delete&lt;/strong&gt; from the top left corner of the navigation bar.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:20%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAs0lEQVQI1y2L207DMBBE8/9voEIl2kD7BUglbSHid3gore0kRLH34h1Mwug8zNHsVg+N33z4l9Nld/yuD5fHw3XV+MLTu99/hufWL2xbtzq5+8bdvV3XZ1e3oX79qgD+J//ACH/JgAATbAJshiR3rN6sB2LR+UCrRFxglpsLXT+I5mxgyVNkYtUMzUYkwzB23UBUZhSNiVPiiueIlLkfx9EMZqaqtrQ5RBRCiCmWzsJFl69fW+ncxyDlb9oAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Google Cloud Dashboard Navigation&quot; title=&quot;Google Cloud Dashboard Navigation&quot; src=&quot;/static/a62a55c63fb529b4ab5d3c189071b9f0/5a190/image-1.png&quot; srcSet=&quot;/static/a62a55c63fb529b4ab5d3c189071b9f0/772e8/image-1.png 200w,/static/a62a55c63fb529b4ab5d3c189071b9f0/e17e5/image-1.png 400w,/static/a62a55c63fb529b4ab5d3c189071b9f0/5a190/image-1.png 800w,/static/a62a55c63fb529b4ab5d3c189071b9f0/c1b63/image-1.png 1200w,/static/a62a55c63fb529b4ab5d3c189071b9f0/add4c/image-1.png 1452w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Just a few clicks away!&lt;/figcaption&gt;&lt;p&gt;Once selected, click on the &lt;em&gt;3 dots&lt;/em&gt; ⫶ in the top right corner next to your profile and select &lt;em&gt;Project settings&lt;/em&gt; in the dropdown.
You should then see the option &lt;strong&gt;SHUT DOWN&lt;/strong&gt; 🗑️ appear below the navigation bar, which allows you to &lt;em&gt;delete&lt;/em&gt; your project.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[What Are RPC APIs]]></title><description><![CDATA[RPC stands for  remote procedure call  and simply refers to calling another function. Unlike REST, which is all about  resources  (i.e. data…]]></description><link>https://dilshankelsen.com/what-are-rpc-apis/</link><guid isPermaLink="false">https://dilshankelsen.com/what-are-rpc-apis/</guid><pubDate>Mon, 17 Oct 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;RPC stands for &lt;strong&gt;remote procedure call&lt;/strong&gt; and simply refers to calling another function. Unlike REST, which is all about &lt;em&gt;resources&lt;/em&gt; (i.e. data), RPC is all about &lt;em&gt;actions&lt;/em&gt;, i.e. &lt;strong&gt;triggering remote functions&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;As a great example, &lt;a href=&quot;https://api.slack.com/web#slack-web-api__basics&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;Slack&amp;#x27;s Web API&lt;/a&gt; consists of &lt;em&gt;HTTP RPC-style methods&lt;/em&gt; where you call (e.g. &lt;em&gt;POST&lt;/em&gt;) an action using a URL in the form of &lt;code&gt;https://slack.com/api/METHOD_FAMILY.method&lt;/code&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Remove Table Borders In DOCX JS]]></title><description><![CDATA[Removing the borders of a table is  not so obvious , 🤔 especially if you draw your experience from CSS. The existing solution to achieving…]]></description><link>https://dilshankelsen.com/remove-table-borders-in-docx-js/</link><guid isPermaLink="false">https://dilshankelsen.com/remove-table-borders-in-docx-js/</guid><pubDate>Thu, 13 Oct 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Removing the borders of a table is &lt;em&gt;not so obvious&lt;/em&gt;, 🤔 especially if you draw your experience from CSS. The existing solution to achieving this goal is to simply define the &lt;strong&gt;border color&lt;/strong&gt; as white, giving the appearance that they have been removed.&lt;/p&gt;&lt;div&gt;&lt;p&gt;In a DOCX table, borders are defined on a &lt;em&gt;cell level&lt;/em&gt;.&lt;/p&gt;&lt;/div&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;TableCell&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;borders&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;top&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;FFFFFF&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;bottom&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;FFFFFF&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;FFFFFF&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;FFFFFF&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Many-to-many SQL Relationship Explained]]></title><description><![CDATA[A many-to-many relationship is defined by  many records in one table relating to many records in another table . As a typical example, take…]]></description><link>https://dilshankelsen.com/many-to-many-sql-relationship-explained/</link><guid isPermaLink="false">https://dilshankelsen.com/many-to-many-sql-relationship-explained/</guid><pubDate>Mon, 10 Oct 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A many-to-many relationship is defined by &lt;strong&gt;many records in one table relating to many records in another table&lt;/strong&gt;. As a typical example, take a school. &lt;em&gt;Every&lt;/em&gt; class will have many students enrolled, but at the same time &lt;em&gt;every&lt;/em&gt; student student will attend many classes.&lt;/p&gt;&lt;p&gt;If one were to &lt;strong&gt;represent this in a database&lt;/strong&gt;, one would &lt;em&gt;assume&lt;/em&gt; to add multiple foreign keys 🔑 in a single column as seen below.&lt;/p&gt;&lt;p&gt;A &lt;strong&gt;student&lt;/strong&gt; table, and&lt;/p&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Class Id&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;Dilshan&lt;/td&gt;&lt;td&gt;1, 2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;Jessica&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;A &lt;strong&gt;class&lt;/strong&gt; table.&lt;/p&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Student Id&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;Finance&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;Accounting&lt;/td&gt;&lt;td&gt;1, 2&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;However, &lt;strong&gt;adding comma separated values is not possible&lt;/strong&gt; in a database. Adding a &lt;em&gt;second foreign key column&lt;/em&gt; instead for classes or students isn&amp;#x27;t possible either. Neither should we add another row, else we introduce &lt;em&gt;data duplication&lt;/em&gt;.&lt;/p&gt;&lt;h2 id=&quot;joining-table&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#joining-table&quot; aria-label=&quot;joining table permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Joining Table&lt;/h2&gt;&lt;p&gt;To solve this issue, one must employ the use of a &lt;strong&gt;joining table&lt;/strong&gt; (also known as a &lt;em&gt;junction or bridging table&lt;/em&gt;). The point is to &lt;em&gt;break up&lt;/em&gt; 💔 the many-to-many relationship into &lt;strong&gt;2 one-to-many relationships&lt;/strong&gt;. It&amp;#x27;s basically an intermediate table that records any combination of foreign keys of 2 tables. Furthermore, it&amp;#x27;s common practice to name the joining table after the tables it&amp;#x27;s linking, e.g. &lt;code&gt;student_class&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;To &lt;strong&gt;illustrate this&lt;/strong&gt;, the example from above can be &lt;em&gt;properly remodeled&lt;/em&gt; as follows:&lt;/p&gt;&lt;p&gt;A &lt;strong&gt;student&lt;/strong&gt; (one-to-many with &lt;code&gt;student_class&lt;/code&gt;) table,&lt;/p&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;Dilshan&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;Jessica&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;A &lt;strong&gt;student_class&lt;/strong&gt; table, and&lt;/p&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Student Id&lt;/th&gt;&lt;th&gt;Class Id&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;A &lt;strong&gt;class&lt;/strong&gt; (one-to-many with &lt;code&gt;student_class&lt;/code&gt;) table.&lt;/p&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;Finance&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;Accounting&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;</content:encoded></item><item><title><![CDATA[Create An Ordered List In DOCX JS]]></title><description><![CDATA[Regarding  ordered or numbered lists , I've found the DOCX documentation to be quite  sparse , even differing 🙃 from provided examples on…]]></description><link>https://dilshankelsen.com/create-ordered-list-in-docx-js/</link><guid isPermaLink="false">https://dilshankelsen.com/create-ordered-list-in-docx-js/</guid><pubDate>Thu, 06 Oct 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Regarding &lt;strong&gt;ordered or numbered lists&lt;/strong&gt;, I&amp;#x27;ve found the DOCX documentation to be quite &lt;em&gt;sparse&lt;/em&gt;, even differing 🙃 from provided examples on their GitHub page. Here&amp;#x27;s what has worked for me.&lt;/p&gt;&lt;p&gt;The &lt;strong&gt;configuration&lt;/strong&gt; of your ordered list is set in the &lt;code&gt;Document&lt;/code&gt; object:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; doc &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;docx&lt;/span&gt;&lt;span class=&quot;token class-name punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Document&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// numbering: { ... } - insert ordered list configuration here&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In the configuration object, you may have &lt;strong&gt;one or multiple styles&lt;/strong&gt; defined. Notice 👈 that the leveling &lt;em&gt;starts&lt;/em&gt; at &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; numbering &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// May contain multiple styles&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;reference&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;numbering-style-1&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;levels&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;upperRoman&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;%1)&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// results in &amp;#x27;1)&amp;#x27;, &amp;#x27;2)&amp;#x27;, etc.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;alignment&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;AlignmentType&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;START&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;paragraph&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;              &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;indent&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;720&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;hanging&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;260&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;decimal&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;%2.&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// results in &amp;#x27;1.&amp;#x27;, &amp;#x27;2.&amp;#x27;, etc.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;alignment&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;AlignmentType&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;START&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;paragraph&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;              &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Increase indentation for nested levels&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;              &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;indent&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1440&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;hanging&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;980&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// And so on...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In the body of your document, you need to &lt;em&gt;explicitly inform&lt;/em&gt; your list to use your custom numbering style by &lt;strong&gt;referencing&lt;/strong&gt; its ID. In addition, you also need to define the level of each list item.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Paragraph&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Some ordered item&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;numbering&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;reference&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;numbering-style-1&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[GraphQL Nodes And Edges Explained]]></title><description><![CDATA[Getting a rudimentary understanding of  graph theory  can go a long way in  solidifying  your understanding of nodes and edges in GraphQL…]]></description><link>https://dilshankelsen.com/graphql-nodes-and-edges-explained/</link><guid isPermaLink="false">https://dilshankelsen.com/graphql-nodes-and-edges-explained/</guid><pubDate>Mon, 03 Oct 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Getting a rudimentary understanding of &lt;strong&gt;graph theory&lt;/strong&gt; can go a long way in &lt;em&gt;solidifying&lt;/em&gt; your understanding of nodes and edges in GraphQL.&lt;/p&gt;&lt;h2 id=&quot;what-is-a-graph&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#what-is-a-graph&quot; aria-label=&quot;what is a graph permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;What is a graph?&lt;/h2&gt;&lt;p&gt;In simple terms, a graph is an &lt;em&gt;interconnected web&lt;/em&gt; of data. Each data point (e.g. books or authors) represents a &lt;strong&gt;node&lt;/strong&gt; and each of these nodes is connected by what are called &lt;strong&gt;edges&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:320px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:81.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAIAAACZeshMAAAACXBIWXMAAAsTAAALEwEAmpwYAAABlUlEQVQoz5VT21aDMBDs/3+TfkEfhFooxwrFKvdwL4HcqAPR1mNr1X3ISTaZ3Z3ZzeL4aeM4cs6xHn82IYRhGMvlMooiHBfaq5QKwzBJEnhv4HEVBIHruk3TTGD9NM9z0zQ3jmPbFqX0KgxrlmU7z0vTFHgp5UdmANbWxn1+Xj3atGfz6ytgVPfwYKxnA4UPMBdjnLVhUpQ1JTXvBnnCaNPHpOhfAzALq6o6c2ZClS1nYqwOEyxvWNHwr5m5HMPs0PX8VMUZ3FJxoAKbtByEnO4OvUQJddvVVVk2NG+k6+3u7+9e9nst8CzYDCYVY1xpDErQEUlRrW0nCgPb2TIQU6qua8bYqZwp8zD0/j4ipNCurGRcToHQudXKhDamaWAELluwgOKO40AE3/cJIVNyKspmCt8zsfXekBlRvmp+BgspMTRosmVZ+hHu04opNUKzganZcd2msqE7MsdxrGUAtzDO/NcoJu3xpi0u+4lJ2GxsjKHn7UDqd/C3MQIRCLHdPv0DfPo6mGGw6LruT2VffoPbf1PbO3k5nZPyuWo0AAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Graph example&quot; title=&quot;Graph example&quot; src=&quot;/static/aa0e194b275a5bba81c941059f04f3db/72799/image-1.png&quot; srcSet=&quot;/static/aa0e194b275a5bba81c941059f04f3db/772e8/image-1.png 200w,/static/aa0e194b275a5bba81c941059f04f3db/72799/image-1.png 320w&quot; sizes=&quot;(max-width: 320px) 100vw, 320px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;A basic graph with nodes and edges&lt;/figcaption&gt;&lt;p&gt;It&amp;#x27;s important to note that &lt;strong&gt;edges are just as important as nodes&lt;/strong&gt; as they contain &lt;em&gt;metadata about the relationship&lt;/em&gt; of two nodes.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Pretty Print Or Format JSON String In JavaScript]]></title><description><![CDATA[Unknown to many,  JSON.stringify  has  2 additional parameters  that allow us to  format  a JSON string. To start off, you will need to…]]></description><link>https://dilshankelsen.com/pretty-print-format-json-string-in-javascript/</link><guid isPermaLink="false">https://dilshankelsen.com/pretty-print-format-json-string-in-javascript/</guid><pubDate>Mon, 26 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Unknown to many, &lt;code&gt;JSON.stringify&lt;/code&gt; has &lt;strong&gt;2 additional parameters&lt;/strong&gt; that allow us to &lt;em&gt;format&lt;/em&gt; a JSON string.&lt;/p&gt;&lt;p&gt;To start off, you will need to parse the string into a JavaScript object, &lt;em&gt;then back&lt;/em&gt; into a JSON string.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; jsonString &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;{&amp;quot;car&amp;quot;:&amp;quot;Audi&amp;quot;,&amp;quot;pet&amp;quot;:&amp;quot;Dog&amp;quot;,&amp;quot;location&amp;quot;:{&amp;quot;city&amp;quot;:&amp;quot;Vienna&amp;quot;,&amp;quot;country&amp;quot;:&amp;quot;Austria&amp;quot;}}&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; parsedJsonString &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;JSON&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;jsonString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// JavaScript object&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; formattedJsonString &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;JSON&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;stringify&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;parsedJsonString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword null nil&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;formattedJsonString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;/**&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;  &amp;quot;{&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;    \&amp;quot;car\&amp;quot;: \&amp;quot;Audi\&amp;quot;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;    \&amp;quot;pet\&amp;quot;: \&amp;quot;Dog\&amp;quot;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;    \&amp;quot;location\&amp;quot;: {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;      \&amp;quot;city\&amp;quot;: \&amp;quot;Vienna\&amp;quot;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;      \&amp;quot;country\&amp;quot;: \&amp;quot;Austria\&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;    }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;  }&amp;quot; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;*/&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;strong&gt;3&lt;sup&gt;rd&lt;/sup&gt;&lt;/strong&gt; (i.e. &lt;code&gt;space&lt;/code&gt;) &lt;strong&gt;parameter&lt;/strong&gt; is of interest to us as it inserts whitespace into the output JSON string. It accepts a number (e.g. &lt;code&gt;2&lt;/code&gt;), which represents the &lt;em&gt;amount of whitespace characters&lt;/em&gt; inserted at each line and indentation.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[An Introduction To GraphQL]]></title><description><![CDATA[GraphQL is described as  a query language for your API , but what exactly does that mean? To get a deeper understanding of this succinct…]]></description><link>https://dilshankelsen.com/introduction-to-graphql/</link><guid isPermaLink="false">https://dilshankelsen.com/introduction-to-graphql/</guid><pubDate>Mon, 19 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;GraphQL is described as &lt;em&gt;a query language for your API&lt;/em&gt;, but what exactly does that mean? To get a deeper understanding of this succinct description, let me explain &lt;strong&gt;why it was created&lt;/strong&gt; in the first place.&lt;/p&gt;&lt;h2 id=&quot;the-problem-it-solves&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-problem-it-solves&quot; aria-label=&quot;the problem it solves permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The problem it solves&lt;/h2&gt;&lt;p&gt;Before GraphQL, Representational State Transfer (REST) was &lt;em&gt;the&lt;/em&gt; 👉 &lt;strong&gt;de facto&lt;/strong&gt; architecture for APIs on your server. However, as time went on, it became unsuitable for most modern web development needs and caused 2 major issues known as &lt;strong&gt;under-fetching&lt;/strong&gt; and &lt;strong&gt;over-fetching&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;Let&amp;#x27;s illustrate these issues with an example. Assume you have two endpoints:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;/directors/:id&lt;/code&gt; returns &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;age&lt;/code&gt;, &lt;code&gt;biography&lt;/code&gt; and &lt;code&gt;movieIds&lt;/code&gt; given a specific director id.&lt;/li&gt;&lt;li&gt;&lt;code&gt;/movies/:id&lt;/code&gt; returns &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;title&lt;/code&gt;, &lt;code&gt;description&lt;/code&gt;, &lt;code&gt;rating&lt;/code&gt; and &lt;code&gt;directorId&lt;/code&gt; given a specific movie id.&lt;/li&gt;&lt;/ul&gt;&lt;h3 id=&quot;under-fetching&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#under-fetching&quot; aria-label=&quot;under fetching permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Under-fetching&lt;/h3&gt;&lt;p&gt;To display information of a 🎬 director and &lt;em&gt;all&lt;/em&gt; his movies, we would first need to fetch that director from the &lt;code&gt;directors&lt;/code&gt; endpoint (e.g. &lt;code&gt;/directors/14&lt;/code&gt;). Subsequently, we would then need to &lt;strong&gt;fetch each movie seperately&lt;/strong&gt; that is listed in the &lt;code&gt;movieIds&lt;/code&gt; property. Assuming it&amp;#x27;s only 1 movie, that&amp;#x27;s already 2 requests in total. But if it&amp;#x27;s an industrious film maker like Spielberg, &lt;em&gt;there will be many more&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;In this sense, the client side is &lt;em&gt;under-fetching&lt;/em&gt;, as in &lt;strong&gt;not getting enough data&lt;/strong&gt; in a single request.&lt;/p&gt;&lt;h3 id=&quot;over-fetching&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#over-fetching&quot; aria-label=&quot;over fetching permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Over-fetching&lt;/h3&gt;&lt;p&gt;&lt;em&gt;Over-fetching&lt;/em&gt; happens in the sense of &lt;strong&gt;receiving unnecessary data&lt;/strong&gt; on each request, as each endpoint gives a &lt;em&gt;predefined structure of data&lt;/em&gt; back. For instance, you might not care to show the description of each movie in the authors page, but only the title and rating.&lt;/p&gt;&lt;p&gt;Together, these issue can be noticeable and painful on &lt;strong&gt;low performance devices&lt;/strong&gt; 📲 and &lt;strong&gt;slow bandwidth&lt;/strong&gt;.&lt;/p&gt;&lt;h2 id=&quot;graphql-approach&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#graphql-approach&quot; aria-label=&quot;graphql approach permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;GraphQL approach&lt;/h2&gt;&lt;p&gt;Knowing these issues, &lt;strong&gt;Facebook&lt;/strong&gt; developed a more efficient alternative that would allow you to fetch &lt;em&gt;only&lt;/em&gt; the resources &lt;em&gt;you asked (i.e. queried) for&lt;/em&gt; in a &lt;em&gt;single&lt;/em&gt; request. This is done by structuring your request in the &lt;em&gt;form&lt;/em&gt; of a graph, also known as the Graph &lt;strong&gt;Q&lt;/strong&gt;uery &lt;strong&gt;L&lt;/strong&gt;anguage.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;graphql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-graphql&quot;&gt;GRAPHQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-graphql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token definition-query function&quot;&gt;DirectorInformation&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property-query&quot;&gt;director&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token attr-name&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;biography&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token object&quot;&gt;movies&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;rating&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Adding Path Aliases In Gatsby]]></title><description><![CDATA[Similar to  Webpack , adding  path aliases  in Gatsby is quite straightforward. To accomplish this, we need to  add a plugin library . Then…]]></description><link>https://dilshankelsen.com/adding-path-aliases-in-gatsby/</link><guid isPermaLink="false">https://dilshankelsen.com/adding-path-aliases-in-gatsby/</guid><pubDate>Mon, 12 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Similar to &lt;a href=&quot;/adding-path-aliases-in-webpack-projects&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;Webpack&lt;/a&gt;, adding &lt;em&gt;path aliases&lt;/em&gt; in Gatsby is quite straightforward. To accomplish this, we need to &lt;strong&gt;add a plugin library&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; i --save gatsby-plugin-alias-imports&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then in your &lt;code&gt;gatsby-config.js&lt;/code&gt; file, add the following plugin &lt;em&gt;configuration&lt;/em&gt;:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; path &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;path&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;exports&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;plugins&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;gatsby-plugin-alias-imports&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;extensions&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To specify your import aliases, simply add &lt;strong&gt;keys&lt;/strong&gt; (&lt;em&gt;i.e. the shortened paths&lt;/em&gt;) and &lt;strong&gt;values&lt;/strong&gt; (&lt;em&gt;i.e. the full paths&lt;/em&gt;) in the &lt;code&gt;alias&lt;/code&gt; object.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token literal-property property&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token string-property property&quot;&gt;&amp;quot;~components&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; path&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;__dirname&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;src/components&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token string-property property&quot;&gt;&amp;quot;context&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; path&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;__dirname&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;src/context&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Run A Single Cucumber Scenario Or Feature]]></title><description><![CDATA[To run a single Cucumber scenario or feature, one has to use  tags . Tags are  keywords  prepended with an  @  sign, which are inserted…]]></description><link>https://dilshankelsen.com/run-single-cucumber-scenario-or-feature/</link><guid isPermaLink="false">https://dilshankelsen.com/run-single-cucumber-scenario-or-feature/</guid><pubDate>Mon, 05 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;To run a single Cucumber scenario or feature, one has to use &lt;strong&gt;tags&lt;/strong&gt;. Tags are &lt;em&gt;keywords&lt;/em&gt; prepended with an &lt;code&gt;@&lt;/code&gt; sign, which are inserted before a &lt;strong&gt;section heading&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;gherkin&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-gherkin&quot;&gt;GHERKIN&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-gherkin&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;@authentication&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Feature: Authenticate user&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  @login&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  Scenario: User login&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    ...&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  @logout&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  Scenario: User logout&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    ...&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If a tag is assigned to a feature, every scenario in it will &lt;em&gt;inherit&lt;/em&gt; the tag. &lt;strong&gt;In other words&lt;/strong&gt;, running a feature tag will run &lt;em&gt;all of its&lt;/em&gt; scenario. Running a tag can be achieved as follows:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; run cucumber-js --tags @login&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;running-multiple-features-or-scenarios&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#running-multiple-features-or-scenarios&quot; aria-label=&quot;running multiple features or scenarios permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Running multiple features or scenarios&lt;/h2&gt;&lt;p&gt;To run &lt;strong&gt;more than one feature or scenario&lt;/strong&gt;, simply append your tags to the command.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; run cucumber-js --tags @login @logout @anotherTag&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Share Common Steps Between Gherkin Scenarios]]></title><description><![CDATA[If you notice yourself writing the same  Given  steps over and over again  in all scenarios  of a  feature : You can extract those steps…]]></description><link>https://dilshankelsen.com/share-common-steps-between-gherkin-scenarios/</link><guid isPermaLink="false">https://dilshankelsen.com/share-common-steps-between-gherkin-scenarios/</guid><pubDate>Mon, 29 Aug 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;If you notice yourself writing the same &lt;code&gt;Given&lt;/code&gt; steps over and over again &lt;strong&gt;in all scenarios&lt;/strong&gt; of a &lt;em&gt;feature&lt;/em&gt;:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;gherkin&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-gherkin&quot;&gt;GHERKIN&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-gherkin&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Scenario A:&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  Given User is logged in&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  And User did X&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  ...&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Scenario B:&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  Given User is logged in&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  When User goes to page Y&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  ...&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can extract those steps using the &lt;strong&gt;keyword&lt;/strong&gt; &lt;code&gt;Background&lt;/code&gt; as follows:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;gherkin&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-gherkin&quot;&gt;GHERKIN&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-gherkin&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Background:&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  Given User is logged in&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Scenario A:&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  Given User did X&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  ...&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Scenario B:&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  When User goes to page Y&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  ...&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This keyword runs &lt;em&gt;before&lt;/em&gt; each scenario, but &lt;em&gt;after&lt;/em&gt; any &lt;code&gt;Before&lt;/code&gt; hooks.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Conjugation in Given When Then Gherkin Steps]]></title><description><![CDATA[There are  no hard and fast rules  when it comes to writing Gherkin scenarios. Nonetheless, following the  past-present-conditional tense…]]></description><link>https://dilshankelsen.com/conjugation-given-when-then-gherkin-steps/</link><guid isPermaLink="false">https://dilshankelsen.com/conjugation-given-when-then-gherkin-steps/</guid><pubDate>Mon, 22 Aug 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;There are &lt;em&gt;no hard and fast rules&lt;/em&gt; when it comes to writing Gherkin scenarios. Nonetheless, following the &lt;strong&gt;past-present-conditional tense structure&lt;/strong&gt; is typically seen as good practice.&lt;/p&gt;&lt;h2 id=&quot;given&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#given&quot; aria-label=&quot;given permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Given&lt;/h2&gt;&lt;p&gt;&lt;code&gt;Given&lt;/code&gt; statements are &lt;em&gt;passive&lt;/em&gt; and set up a &lt;em&gt;context or condition&lt;/em&gt; for the entire scenario. They indicate that something &lt;strong&gt;already happened&lt;/strong&gt; and might still be taking place.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;gherkin&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-gherkin&quot;&gt;GHERKIN&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-gherkin&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Given User navigated to the login page (past tense)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Given User is on the login page (present continuous)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Given User has been granted admin privileges (perfect tense)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The exact tense to use is up to you!&lt;/p&gt;&lt;h2 id=&quot;when&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#when&quot; aria-label=&quot;when permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;When&lt;/h2&gt;&lt;p&gt;&lt;code&gt;When&lt;/code&gt; statements are &lt;em&gt;active&lt;/em&gt; and indicate that an &lt;strong&gt;event is occurring&lt;/strong&gt;. Naturally, this means the present tense should be used.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;gherkin&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-gherkin&quot;&gt;GHERKIN&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-gherkin&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;When User enters their username and password (present tense)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;When User clicks &amp;quot;Log in&amp;quot; (present tense)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;then&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#then&quot; aria-label=&quot;then permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Then&lt;/h2&gt;&lt;p&gt;&lt;code&gt;Then&lt;/code&gt; statements are &lt;em&gt;passive&lt;/em&gt; and indicate an &lt;strong&gt;expected yet uncertain&lt;/strong&gt; outcome, state or behaviour.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;gherkin&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-gherkin&quot;&gt;GHERKIN&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-gherkin&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Then User should be logged into website (conditional tense)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The keyword &lt;code&gt;should&lt;/code&gt; is quite suited to indicate this sentiment.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Change A File Back To A Previous Commit]]></title><description><![CDATA[Assume you  unintentionally made changes  to a file a few commits ago. To  revert it  to an earlier file version, i.e. commit, you can first…]]></description><link>https://dilshankelsen.com/change-file-back-to-previous-commit/</link><guid isPermaLink="false">https://dilshankelsen.com/change-file-back-to-previous-commit/</guid><pubDate>Mon, 15 Aug 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Assume you &lt;em&gt;unintentionally made changes&lt;/em&gt; to a file a few commits ago. To &lt;strong&gt;revert it&lt;/strong&gt; to an earlier file version, i.e. commit, you can first list the version history of said file.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; log --oneline &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Once you know &lt;strong&gt;which version to revert your file back to&lt;/strong&gt;, pick its &lt;em&gt;commit hash&lt;/em&gt; (e.g. &lt;code&gt;902744c&lt;/code&gt;) and run the following command:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; checkout &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;commit hash&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; -- &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then &lt;em&gt;stage and commit&lt;/em&gt; the changes.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Inspect A Disappearing Element In Browser]]></title><description><![CDATA[There are  two ways  you can achieve this. Both involve  activating the debugger  of your browser to freeze the DOM: Pasting the following…]]></description><link>https://dilshankelsen.com/inspect-disappearing-element-in-browser/</link><guid isPermaLink="false">https://dilshankelsen.com/inspect-disappearing-element-in-browser/</guid><pubDate>Mon, 08 Aug 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;There are &lt;strong&gt;two ways&lt;/strong&gt; you can achieve this. Both involve &lt;em&gt;activating the debugger&lt;/em&gt; of your browser to freeze the DOM:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Pasting the following code snippet in your browser&amp;#x27;s console will &lt;strong&gt;automatically&lt;/strong&gt; active the debugger in 5 seconds.&lt;/li&gt;&lt;/ul&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;setTimeout&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;debugger&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;To &lt;strong&gt;manually&lt;/strong&gt; trigger the debugger, open the &lt;em&gt;Debugger&lt;/em&gt; tab in Firefox or the &lt;em&gt;Sources&lt;/em&gt; tab in a Chromium browser and hit F8.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;In either case, ensure that the element is &lt;em&gt;showing before&lt;/em&gt; the debugger is triggered.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Update Node.js Dependencies]]></title><description><![CDATA[Recently, I decided to upgrade the Node.js environment of my blog. This meant updating npm packages that might have new  breaking changes…]]></description><link>https://dilshankelsen.com/update-nodejs-dependencies/</link><guid isPermaLink="false">https://dilshankelsen.com/update-nodejs-dependencies/</guid><pubDate>Mon, 01 Aug 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Recently, I decided to upgrade the Node.js environment of my blog. This meant updating npm packages that might have new &lt;em&gt;breaking changes&lt;/em&gt;. Here&amp;#x27;s how I did it. 👇&lt;/p&gt;&lt;h2 id=&quot;updating-npm-packages&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#updating-npm-packages&quot; aria-label=&quot;updating npm packages permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Updating npm packages&lt;/h2&gt;&lt;p&gt;By running &lt;code&gt;npm outdated&lt;/code&gt;, you get a list of all your existing packages with information on the &lt;em&gt;currently used&lt;/em&gt; version, the &lt;em&gt;recommended&lt;/em&gt; version &lt;strong&gt;given your Node.js version&lt;/strong&gt; and the &lt;em&gt;latest&lt;/em&gt; publicly available package version.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:603px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:24.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAIAAADKYVtkAAAACXBIWXMAAAsTAAALEwEAmpwYAAABBUlEQVQY0xXP626CMBjGcS5mm8pBVKCUtxwrLahFBVrGISMxWXb/l7Ca/D78k+fTY/RKSqUikqCIYEJCID4CLwAPgWnhy7Wv6y6My3pQOGUNlwmpHs+p65eIMGOY2Tiz199VjkyNbForOfGnLIeJeyjKaVPx1sKwu8cOJqXDk4At669UP2pYjcf9/Gxo25U6hKB3raE6LoIeTmFWCFoIOyR2nTiI5G4JHm37+SakZnzzos3ShRfsCOkeJ25UHYEf4eCEHxs/p6Ku2j0kltAjYW6VIT4ur66fO7kYjWTskov27GP43AYbE33t3rYm2uwCfazIb7YPVhnbAcQODU+FeAz1tdP+AQkNTk8hzwk7AAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Output of npm update&quot; title=&quot;Output of npm update&quot; src=&quot;/static/842cb8695c44a4291ff8a3dd3b892208/9128f/image-1.png&quot; srcSet=&quot;/static/842cb8695c44a4291ff8a3dd3b892208/772e8/image-1.png 200w,/static/842cb8695c44a4291ff8a3dd3b892208/e17e5/image-1.png 400w,/static/842cb8695c44a4291ff8a3dd3b892208/9128f/image-1.png 603w&quot; sizes=&quot;(max-width: 603px) 100vw, 603px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Output of npm outdated&lt;/figcaption&gt;&lt;p&gt;Those that require an update will be &lt;strong&gt;marked in red&lt;/strong&gt;, as was the case with &lt;code&gt;@fortawesome/react-fontawesome&lt;/code&gt;. To &lt;em&gt;update&lt;/em&gt; them, simply run &lt;code&gt;npm update&lt;/code&gt;.&lt;/p&gt;&lt;h2 id=&quot;upgrading-nodejs&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#upgrading-nodejs&quot; aria-label=&quot;upgrading nodejs permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Upgrading Node.js&lt;/h2&gt;&lt;p&gt;Next up, &lt;em&gt;upgrade&lt;/em&gt; your Node environment! The actual procedure itself &lt;strong&gt;depends&lt;/strong&gt; on how you have it installed. If you use a &lt;strong&gt;version manager&lt;/strong&gt; such as nvm, it&amp;#x27;s as simple as &lt;code&gt;nvm install 16&lt;/code&gt; and &lt;code&gt;nvm use 16&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;After doing this, list in &lt;code&gt;package.json&lt;/code&gt; the new Node and npm versions &lt;strong&gt;under engines&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; -v &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; -v&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;v16.14.2&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8.5&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;.0&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;json&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-json&quot;&gt;JSON&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-json&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;engines&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;node&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;&amp;gt;=16.14.2&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;npm&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;&amp;gt;=8.5.0&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;upgrading-npm-packages&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#upgrading-npm-packages&quot; aria-label=&quot;upgrading npm packages permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Upgrading npm packages&lt;/h2&gt;&lt;p&gt;By &lt;em&gt;upgrading&lt;/em&gt; your Node.js environment, there will inevitably be &lt;strong&gt;packages that don&amp;#x27;t support&lt;/strong&gt; it yet. It is up to you to identify these packages, e.g. by &lt;em&gt;building&lt;/em&gt; the project.&lt;/p&gt;&lt;div&gt;&lt;p&gt;It&amp;#x27;s highly recommended to focus on one package at a time and to commit locally regularly. Things can get messy real quick. 🙈&lt;/p&gt;&lt;/div&gt;&lt;p&gt;Here&amp;#x27;s an &lt;em&gt;example of mine&lt;/em&gt;. When running my project again, I encountered several of these errors below.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:22.499999999999996%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAIAAADKYVtkAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA1ElEQVQY042P7W6DMAxFeZhuFEinLQlJCF9dk5iElAEVfZ/uzeeiab8nHV1ZR7J1nXw35lGbjTaaKFLINBdZIY+5+A+J6X2vbddAXYNUVtcglEGqGijrXlKWZuXrkWP+7mR4/W8ZZguL82t/HsGvDuZPc7UwG/t1sROaph3QtJ2vtJOVac8BB6Wt0i7x4ebDOsZt8Lfxend+CXGL0x0NehgW+WzhhLqQU4UfkbeqICrfSTgz/CNIHgUbFZ8kiyUNgoWSBU49fYcTaQ8p3ZuLnWd/TOQHH4RD2HBPCdMAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;node sass build error&quot; title=&quot;node sass build error&quot; src=&quot;/static/7ec70ed7837112024edb0cf7bb14e844/5a190/image-2.png&quot; srcSet=&quot;/static/7ec70ed7837112024edb0cf7bb14e844/772e8/image-2.png 200w,/static/7ec70ed7837112024edb0cf7bb14e844/e17e5/image-2.png 400w,/static/7ec70ed7837112024edb0cf7bb14e844/5a190/image-2.png 800w,/static/7ec70ed7837112024edb0cf7bb14e844/63ec5/image-2.png 812w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Build error caused by an incompatible package version&lt;/figcaption&gt;&lt;p&gt;In this case, &lt;code&gt;node-sass&lt;/code&gt; was giving me troubles. What I did next was:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Identify which version&lt;/strong&gt; of the package was compatible with which version of Node. This &lt;a href=&quot;https://github.com/sass/node-sass&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot;&gt;turned out quite easy&lt;/a&gt;, 😅 but it won&amp;#x27;t always be.&lt;/li&gt;&lt;li&gt;Install the &lt;em&gt;correct&lt;/em&gt; version with &lt;code&gt;npm install node-sass@6&lt;/code&gt;.&lt;/li&gt;&lt;li&gt;Afterwards, I &lt;strong&gt;deleted&lt;/strong&gt; the &lt;code&gt;node_modules&lt;/code&gt; folder and &lt;code&gt;package-lock.json&lt;/code&gt; file, and attempted to reinstall everything. This makes it easier to spot if &lt;em&gt;other packages are using an older version&lt;/em&gt; of &lt;code&gt;node-sass&lt;/code&gt;, which I then I had to update as well.&lt;/li&gt;&lt;li&gt;Rinse and repeat.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Overall, there&amp;#x27;s &lt;strong&gt;no one-size-fits-all&lt;/strong&gt; procedure. It comes down to trying things out and &lt;em&gt;hoping&lt;/em&gt; for the best. 😬 If things get out of hand, you can always &lt;strong&gt;revert&lt;/strong&gt; to an older commit.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Using A Debounce Function In JavaScript]]></title><description><![CDATA[A debounce function  groups  several function calls  into one  single call under the condition that they occur close enough to each other…]]></description><link>https://dilshankelsen.com/using-debounce-function-in-javascript/</link><guid isPermaLink="false">https://dilshankelsen.com/using-debounce-function-in-javascript/</guid><pubDate>Mon, 25 Jul 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A debounce function &lt;strong&gt;groups&lt;/strong&gt; several function calls &lt;em&gt;into one&lt;/em&gt; single call under the condition that they occur close enough to each other.&lt;/p&gt;&lt;div&gt;&lt;p&gt;This is not to be confused with throttling, which limits function calls to a given time interval. Check out &lt;a href=&quot;https://web.archive.org/web/20220117092326/http://demo.nimius.net/debounce_throttle/&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;this visual example&lt;/a&gt; for the difference.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;debounce-explained&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#debounce-explained&quot; aria-label=&quot;debounce explained permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Debounce explained&lt;/h2&gt;&lt;p&gt;Check this scenario out: Imagine your debounce function has a &lt;strong&gt;timeout&lt;/strong&gt; of &lt;em&gt;300 milliseconds&lt;/em&gt; as specified below. If the same function is called 10 times with each call happening less than 300ms apart, the function will be executed on the 10th time, &lt;strong&gt;on the condition&lt;/strong&gt; that the &lt;em&gt;11th function call&lt;/em&gt; does not occur or happens &lt;em&gt;more than 300ms&lt;/em&gt; after the 10th one.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;debounce&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; timeout &lt;/span&gt;&lt;span class=&quot;token parameter operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token parameter number&quot;&gt;300&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; timer&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter spread operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;clearTimeout&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;timer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    timer &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setTimeout&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token spread operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; timeout&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;practical-application&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#practical-application&quot; aria-label=&quot;practical application permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Practical application&lt;/h3&gt;&lt;p&gt;We can apply this scenario to the &lt;code&gt;mousemove&lt;/code&gt; event. Our &lt;strong&gt;callback function&lt;/strong&gt; will be triggered on the condition that the mouse event is, &lt;em&gt;at some point&lt;/em&gt;, called more than 500ms apart.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token dom variable&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;mousemove&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;debounce&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Debounced!&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Opening File In Text Editor From Mac Terminal]]></title><description><![CDATA[You can open any file from your Mac's terminal with the  following command . There are a  2 things  you should keep in mind: The  -a  flag…]]></description><link>https://dilshankelsen.com/opening-file-in-text-editor-from-mac-terminal/</link><guid isPermaLink="false">https://dilshankelsen.com/opening-file-in-text-editor-from-mac-terminal/</guid><pubDate>Mon, 27 Sep 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;You can open any file from your Mac&amp;#x27;s terminal with the &lt;em&gt;following command&lt;/em&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; -a TextEdit filename.extension&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;There are a &lt;em&gt;2 things&lt;/em&gt; you should keep in mind:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;The &lt;code&gt;-a&lt;/code&gt; flag allows you to &lt;strong&gt;specify which application to use&lt;/strong&gt;, and&lt;/li&gt;&lt;li&gt;This command is run in the &lt;strong&gt;same directory as the file&lt;/strong&gt;. If not, &lt;em&gt;prepend&lt;/em&gt; the path to the filename.&lt;/li&gt;&lt;/ul&gt;</content:encoded></item><item><title><![CDATA[How To Hide Popper If Reference Outside View]]></title><description><![CDATA[The PopperJS element has  an attribute  which  keeps track  of the visibility of its reference element. Naturally, we can make use of this…]]></description><link>https://dilshankelsen.com/hide-popper-if-reference-outside-view/</link><guid isPermaLink="false">https://dilshankelsen.com/hide-popper-if-reference-outside-view/</guid><pubDate>Sat, 25 Sep 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The PopperJS element has &lt;strong&gt;an attribute&lt;/strong&gt; which &lt;em&gt;keeps track&lt;/em&gt; of the visibility of its reference element. Naturally, we can make use of this to &lt;em&gt;conditionally hide&lt;/em&gt; our Popper.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;scss&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-scss&quot;&gt;SCSS&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-scss&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token selector parent important&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token selector&quot;&gt;[data-popper-reference-hidden=&amp;#x27;true&amp;#x27;] &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;opacity&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;pointer-events&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; none&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Optionally&lt;/strong&gt;, we can add some animation to the transition.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-css&quot;&gt;CSS&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-css&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token property&quot;&gt;transition&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; opacity &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;.15&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; ease-out&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[TypeScript Return Type For SetTimeout Function]]></title><description><![CDATA[Without actually knowing the return type of  setTimeout , we can have TypeScript  infer the type  by using the  utility type   ReturnType…]]></description><link>https://dilshankelsen.com/typescript-return-type-for-settimeout-function/</link><guid isPermaLink="false">https://dilshankelsen.com/typescript-return-type-for-settimeout-function/</guid><pubDate>Thu, 23 Sep 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Without actually knowing the return type of &lt;code&gt;setTimeout&lt;/code&gt;, we can have TypeScript &lt;strong&gt;infer the type&lt;/strong&gt; by using the &lt;em&gt;utility type&lt;/em&gt; &lt;code&gt;ReturnType&amp;lt;Type&amp;gt;&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; timer&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; ReturnType&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; setTimeout&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setTimeout&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Hello World!&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:293px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:18.999999999999996%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAyUlEQVQI1xWMzY6DIBhF3TJoCwp8/AkIFIg1Tsakm6ab2cz7P9LozclZndzudm64IIQori3Mihs2Tk7xYIQGRiidBAjnuA+jVGd25cOlDmH0hRHu8f1GsmpFb0lVMbGHg58yl6AHOo1zEOuuth1iHjDuERr6HmHcVbsW0wxza42/7+Pvc6S8SMmOLaSgStTrw7ak92LMbHlMsjYoddIme+i8jA4Cp7B4+/p+vo9tCZ6OJHkZZshBasnOl5atAOBLFEti1t0p9Vb8A/vbGS17Z4iYAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Type of timer on hover&quot; title=&quot;Type of timer on hover&quot; src=&quot;/static/bef9d24ceab1ebd64df6f488034a3378/3cf3e/image-1.png&quot; srcSet=&quot;/static/bef9d24ceab1ebd64df6f488034a3378/772e8/image-1.png 200w,/static/bef9d24ceab1ebd64df6f488034a3378/3cf3e/image-1.png 293w&quot; sizes=&quot;(max-width: 293px) 100vw, 293px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Type of timer on hover&lt;/figcaption&gt;&lt;p&gt;If you then &lt;em&gt;hover over&lt;/em&gt; the &lt;code&gt;timer&lt;/code&gt; constant, you&amp;#x27;ll see that it&amp;#x27;s of type &lt;code&gt;number&lt;/code&gt;. In fact, &lt;code&gt;setTimeout&lt;/code&gt; returns a &lt;strong&gt;numeric id&lt;/strong&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Get Stock Price With MOEX API In Google Sheets]]></title><description><![CDATA[The Moscow Stock Exchange (MOEX) 🇷🇺 offers a great API to fetch any securities related data  for free . However, it can be  confusing  at…]]></description><link>https://dilshankelsen.com/stock-price-with-moex-api-google-sheets/</link><guid isPermaLink="false">https://dilshankelsen.com/stock-price-with-moex-api-google-sheets/</guid><pubDate>Mon, 20 Sep 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The Moscow Stock Exchange (MOEX) 🇷🇺 offers a great API to fetch any securities related data &lt;strong&gt;for free&lt;/strong&gt;. However, it can be &lt;em&gt;confusing&lt;/em&gt; at first to wrap your head around it.&lt;/p&gt;&lt;h2 id=&quot;formula&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#formula&quot; aria-label=&quot;formula permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Formula&lt;/h2&gt;&lt;p&gt;This &lt;em&gt;Excel formula&lt;/em&gt; is the key to it all. &lt;strong&gt;Pay attention to&lt;/strong&gt; &lt;code&gt;&amp;lt;ADD CODE HERE&amp;gt;&lt;/code&gt; towards the end. This needs to be &lt;em&gt;replaced&lt;/em&gt; with the appropriate code of your desired stock.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;Excel&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-Excel&quot;&gt;EXCEL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-Excel&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;=IMPORTxml(&amp;quot;https://iss.moex.com/iss/engines/stock/markets/bonds/boards/TQOB/securities.xml?iss.meta=off&amp;amp;iss.only=securities&amp;amp;securities.columns=SECID,PREVADMITTEDQUOTE&amp;quot;, concatenate(&amp;quot;//row[@SECID=&amp;#x27;&amp;quot;, &amp;lt;ADD CODE HERE&amp;gt; ,&amp;quot;&amp;#x27;]/@PREVADMITTEDQUOTE&amp;quot;))&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;gazprom-example&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#gazprom-example&quot; aria-label=&quot;gazprom example permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Gazprom Example&lt;/h3&gt;&lt;p&gt;Let&amp;#x27;s say you want to get the price of a particular stock, e.g. &lt;em&gt;Gazprom&lt;/em&gt;. First off, &lt;strong&gt;search the stock&lt;/strong&gt; on the &lt;a href=&quot;https://www.moex.com/en/&quot; target=&quot;_blank&quot;&gt;MOEX website&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:294px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:74.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAIAAABr+ngCAAAACXBIWXMAAAsTAAALEwEAmpwYAAADWUlEQVQoz2WTa0wTWRTH54OaqNmIiQpk1UTDGtndGOOHzfqMHyRqaiJZBQ3KB1SkQNGQ+EiM0Q9q4iMmJipQpFgqTCnQTikKCSpYUORRUBTrA+Kjta0zc+d1Z6a082j3Ftf4YX+55+R/7/lw7j3nXEyaQZ4xOeVEORqVAM35nklQkFVVEkVBgDTgAMsJECLHsDwFOJZhseQPEonkT3Q9CSJS1QX53k20m44pAVIIUkJM0Wg+Kshx5GU5ium6HovFvoZCrycm/H7/+w8f3s4Ak0mxt5O110a1xHQ0mtA1XVNVRdU1hIqAEP6XGZ3SNE2SJEVRNA14XqCFuKanLqMnUiT/B0qJ4fbmOoulqrra2tDgaGlxEQRut1fV1JjN5qbGRhxHcYfD0VJbV3fr9q1ai9XW1Npgs5nv1Nmbm7HikuL8fXnbDTvy9ucfKSkurzAVHS7atduQuyf3qPGoscxYaiorM5Xn7csz7DYUHSw8VVFprDDtLzxwrPIYBuU4gLGYmojHdQ1CXZY0Xac4KQQgCXgGvSAYjEKoqBo6AaxAfgM84ORIBFUMa3J4rDhhw4l7dgLHnbjdFQgEGRpEwt8AYBmGZWmaZ1mO40gKUDSDFoqyqDgkic3NWP9Lxoa56X/PWbxuVtqf2Lws7yOvODoefjqM+iuKIqpqqvsQIilJ4ve5oAEIhULYr9l7Vq4tWLFm79JsQ/pv2xav2Eh09lyytJ2+Yb1ai7e3e1qd7jaXu7XN5SKQJhzI3J6WZqdvZBTbWXB1846zS3//J3P1rkVZOWnLN/X0DQ5PfPKOvBt4MfnpczgQJCf8kyOj41Mfvw4Nj/levJp6/2XK+zo4+QX7K6fyj/Ulmdm56at2LsnKWbBsw8Cgr8P37tr94Zru0dttDymGS+ip0VAUBQlEXIlrCU0QBGxe+paFmVszVuYsWr5ldtoabP6q7ifPzxNDRTVdxjtdpVcs9e2PNVVhOB4NfpgCSIiiFJueZlkWKzh0suDQidLj54pNZ/ILK/IPlg/6XlZ1Dl10DVx2D1xvvH/5rrt/zN/U1e98PGh2Pqxu7e4fe4NGFTUAE3ieYQAD0DdJ9UPguVCEtD99Y+kdr+95aXvQ1/igr7691+p5ctfT29Dh/S6mAmFU/38B55Obu0fmr5oAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Search bar on MOEX website&quot; title=&quot;Search bar on MOEX website&quot; src=&quot;/static/ed4bc0fb0a108cc33785acfd7de6bc29/04a86/image-1.png&quot; srcSet=&quot;/static/ed4bc0fb0a108cc33785acfd7de6bc29/772e8/image-1.png 200w,/static/ed4bc0fb0a108cc33785acfd7de6bc29/04a86/image-1.png 294w&quot; sizes=&quot;(max-width: 294px) 100vw, 294px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Search for your desired security on &lt;br/&gt; the MOEX website&lt;/figcaption&gt;&lt;p&gt;Once you&amp;#x27;re on the details page of your desired stock, locate its &lt;strong&gt;Security Code&lt;/strong&gt; (e.g. GAZP). Then plug it into the formula and let the magic happen! Don&amp;#x27;t forget 🧠 to put the code in &lt;em&gt;double quotes&lt;/em&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Create A Custom React Hook]]></title><description><![CDATA[First off,  what  is a custom hook? It's nothing more than a function that  uses another React hook . If it doesn't use one, then it's…]]></description><link>https://dilshankelsen.com/how-to-create-custom-react-hook/</link><guid isPermaLink="false">https://dilshankelsen.com/how-to-create-custom-react-hook/</guid><pubDate>Sat, 18 Sep 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;First off, &lt;em&gt;what&lt;/em&gt; is a custom hook? It&amp;#x27;s nothing more than a function that &lt;strong&gt;uses another React hook&lt;/strong&gt;. If it doesn&amp;#x27;t use one, then it&amp;#x27;s simply a &lt;em&gt;helper function&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;It&amp;#x27;s &lt;em&gt;common practice&lt;/em&gt; to prepend &lt;code&gt;use&lt;/code&gt; to the function name. While it serves no technical ⚙️ purpose, it helps your &lt;strong&gt;linter&lt;/strong&gt; warn for errors.&lt;/p&gt;&lt;h2 id=&quot;example-hook&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#example-hook&quot; aria-label=&quot;example hook permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Example hook&lt;/h2&gt;&lt;p&gt;To demonstrate just how &lt;strong&gt;simple and easy&lt;/strong&gt; it is for a function to qualify as a hook, here&amp;#x27;s a an example:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;useNumber&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;someNumber&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; someNumber&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Probably the most useless hook you&amp;#x27;ve ever seen, 😅 but it perfectly demonstrates the point I want to get across: you need to use at a &lt;strong&gt;minimum 1 single hook&lt;/strong&gt;. &lt;/p&gt;&lt;p&gt;It &lt;em&gt;doesn&amp;#x27;t matter&lt;/em&gt; what you return. It may be a single value, an array, an object or even &lt;strong&gt;nothing&lt;/strong&gt; at all.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; number &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useNumber&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// 10&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[What To Watch Out For In JavaScript Symbols]]></title><description><![CDATA[In a previous article, I covered the basics of  JavaScript Symbols . However,  before  using them, there are a  few caveats  you should be…]]></description><link>https://dilshankelsen.com/what-to-watch-out-for-in-javascript-symbols/</link><guid isPermaLink="false">https://dilshankelsen.com/what-to-watch-out-for-in-javascript-symbols/</guid><pubDate>Thu, 16 Sep 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In a previous article, I covered the basics of &lt;a href=&quot;/javascript-symbols-simply-explained&quot;&gt;JavaScript Symbols&lt;/a&gt;. However, &lt;em&gt;before&lt;/em&gt; using them, there are a &lt;strong&gt;few caveats&lt;/strong&gt; you should be aware ⚠️ of.&lt;/p&gt;&lt;h2 id=&quot;not-always-unique&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#not-always-unique&quot; aria-label=&quot;not always unique permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Not always unique&lt;/h2&gt;&lt;p&gt;As it turns out, Symbols aren&amp;#x27;t always unique, at least if you&amp;#x27;re &lt;em&gt;using&lt;/em&gt; &lt;code&gt;Symbol.for&lt;/code&gt;. If you create one using this method, a &lt;strong&gt;key 🔑 is required&lt;/strong&gt; which acts as an &lt;em&gt;identifier&lt;/em&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;key&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;key&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// true&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This means that any Symbols with the same key &lt;em&gt;represent&lt;/em&gt; the same Symbol.&lt;/p&gt;&lt;h2 id=&quot;keys-are-hidden&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#keys-are-hidden&quot; aria-label=&quot;keys are hidden permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Keys are hidden&lt;/h2&gt;&lt;p&gt;Symbols used as keys in an object are &lt;strong&gt;hidden 🙈 from methods&lt;/strong&gt; such as&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;Object.keys&lt;/code&gt;, and&lt;/li&gt;&lt;li&gt;&lt;code&gt;Object.getOwnPropertyNames()&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;as well as &lt;strong&gt;iterators&lt;/strong&gt; like:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;for...in&lt;/code&gt;, and&lt;/li&gt;&lt;li&gt;&lt;code&gt;for...of&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; fruits &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;banana&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;apple&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;keys&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;fruits&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Array [ &amp;quot;apple&amp;quot; ]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; fruit &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; fruits&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;fruit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// apple&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To &lt;em&gt;actually&lt;/em&gt; ☝️ &lt;em&gt;retrieve&lt;/em&gt; all the Symbols of an object, use the &lt;code&gt;getOwnPropertySymbols&lt;/code&gt; instead.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getOwnPropertySymbols&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;fruits&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Array [ Symbol(&amp;quot;banana&amp;quot;) ]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;convert-to-string&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#convert-to-string&quot; aria-label=&quot;convert to string permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Convert to string&lt;/h2&gt;&lt;p&gt;Symbols &lt;strong&gt;can&amp;#x27;t be implicitly coerced&lt;/strong&gt; to a string as this will throw a type error. You &lt;em&gt;first&lt;/em&gt; need to convert it to a string with the &lt;code&gt;toString&lt;/code&gt; method.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// TypeError: can&amp;#x27;t convert symbol to string&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// &amp;quot;Symbol()&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Scroll To Element In Nested Scrollable Element]]></title><description><![CDATA[Scrolling to an element using JavaScript  isn't that difficult , no matter how nested the scrollable area is. Let's take the  following…]]></description><link>https://dilshankelsen.com/scroll-to-element-in-nested-scrollable-element/</link><guid isPermaLink="false">https://dilshankelsen.com/scroll-to-element-in-nested-scrollable-element/</guid><pubDate>Mon, 13 Sep 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Scrolling to an element using JavaScript &lt;strong&gt;isn&amp;#x27;t that difficult&lt;/strong&gt;, no matter how nested the scrollable area is. Let&amp;#x27;s take the &lt;em&gt;following layout&lt;/em&gt; as an example:&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:355px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:62.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA0UlEQVQoz5WSyQqDQBBE5///Tb2IgrgeXE5q4gZGk7xowHEcEq1D00pVV1PTwvd913XLsmya5n4adV0bhiH6vq+qap7n1xXAtyxL0D330LIVzjRNpmmKdYzMY5e2bbuuo6G2CxTOJlbcwjD0PC+OY9u20zRNksRxHPQyUy+mJ7zHgmEYqPxhEHH+F7Me+cmzqHmeH523wH6LsyxTnKHp1y6K4iYBWRRFJKc4cx2awJg67XG8ArIg1+9TXXpnPsdxDIJA43wGOH/El056BRJCRfwG8mnzCE7JOPYAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Scrollable window with a scrollable element&quot; title=&quot;Scrollable window with a scrollable element&quot; src=&quot;/static/359d3bd44593c653debb6708bc289e23/526ee/image-1.png&quot; srcSet=&quot;/static/359d3bd44593c653debb6708bc289e23/772e8/image-1.png 200w,/static/359d3bd44593c653debb6708bc289e23/526ee/image-1.png 355w&quot; sizes=&quot;(max-width: 355px) 100vw, 355px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Example Layout&lt;/figcaption&gt;&lt;p&gt;To &lt;em&gt;scroll to any element&lt;/em&gt; in a scrollable view, we need to:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Get the distance (in pixels) of the element itself from &lt;strong&gt;the top of its parent&lt;/strong&gt;, and&lt;/li&gt;&lt;li&gt;Scroll the parent view by said distance&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;How does this &lt;strong&gt;translate into code&lt;/strong&gt;? First, fetch the DOM element you wish to scroll to and get its offset position.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; elementInScrollableDiv &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;some-id&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; positionFromTopOfScrollableDiv &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; elementInScrollableDiv&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;offsetTop&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then apply the value to the scrollable parent!&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; scrollableDivElement &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;scrollable-div-id&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;scrollableDivElement&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;scrollTop&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; positionFromTopOfScrollableDiv&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[JavaScript Symbols Simply Explained]]></title><description><![CDATA[Symbols are  primitives  that serve as  unique identifiers . Comparing two Symbols that appear identical will actually equate to  false  as…]]></description><link>https://dilshankelsen.com/javascript-symbols-simply-explained/</link><guid isPermaLink="false">https://dilshankelsen.com/javascript-symbols-simply-explained/</guid><pubDate>Sat, 11 Sep 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Symbols are &lt;strong&gt;primitives&lt;/strong&gt; that serve as &lt;strong&gt;unique identifiers&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; symbol1 &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; symbol2 &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;symbol1 &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; symbol2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// false&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Comparing two Symbols that appear identical will actually equate to &lt;code&gt;false&lt;/code&gt; as they are &lt;em&gt;both different&lt;/em&gt;. No two Symbols are &lt;strong&gt;ever&lt;/strong&gt; the same.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; symbol3 &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Bananas&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;symbol2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Symbol()&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;symbol3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Symbol(&amp;quot;Bananas&amp;quot;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You may add a string between the parentheses of the constructor. However, this only &lt;strong&gt;serves as a descriptor&lt;/strong&gt; for the &lt;em&gt;developer&lt;/em&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;12 Bananas&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Uncaught TypeError: Symbol is not a constructor&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And despite being called a &amp;quot;&lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/Symbol&quot; target=&quot;_blank&quot;&gt;constructor&lt;/a&gt;&amp;quot;, it &lt;em&gt;doesn&amp;#x27;t actually work&lt;/em&gt; with the &lt;code&gt;new&lt;/code&gt; keyword.&lt;/p&gt;&lt;h2 id=&quot;use-cases&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#use-cases&quot; aria-label=&quot;use cases permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Use cases&lt;/h2&gt;&lt;p&gt;There are &lt;strong&gt;2 common use cases&lt;/strong&gt; for Symbols.&lt;/p&gt;&lt;h3 id=&quot;property-keys&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#property-keys&quot; aria-label=&quot;property keys permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Property keys&lt;/h3&gt;&lt;p&gt;Symbols are frequently &lt;em&gt;used as keys&lt;/em&gt; in objects as they ensure that the property value is &lt;strong&gt;never unintentionally overwritten&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; person &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Jack&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; name &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;name&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;person&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Donny&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;person&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Object { name: &amp;quot;Jack&amp;quot;, Symbol(&amp;quot;name&amp;quot;): &amp;quot;Donny&amp;quot; }&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Even when attempting to overwrite the &lt;code&gt;name&lt;/code&gt; property inside the &lt;code&gt;person&lt;/code&gt; object, the new value is &lt;em&gt;assigned to a new key&lt;/em&gt; instead. If you wish to add a symbol inline, make sure to surround with it &lt;strong&gt;square brackets&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;person &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Jack&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Donny&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;as-constants&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#as-constants&quot; aria-label=&quot;as constants permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;As constants&lt;/h3&gt;&lt;p&gt;Another frequent use case for Symbols are constants, i.e. unique concepts. We often identify these &lt;em&gt;through strings&lt;/em&gt;, but &lt;strong&gt;how about Symbols&lt;/strong&gt;?&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;BANANA&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Banana&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;APPLE&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;identifyFruit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;fruit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;switch&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;fruit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;BANANA&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;This whole thing is bananas!&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;APPLE&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;An apple a day keeps the doctor away.&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Not a supported fruit!&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;identifyFruit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;BANANA&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// &amp;quot;This whole thing is bananas!&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;identifyFruit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// &amp;quot;Not a supported fruit!&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;By using Symbols, we can &lt;strong&gt;avoid any mixup&lt;/strong&gt; between variables that have the same string value, but &lt;em&gt;different semantic meanings&lt;/em&gt; depending on the context. For instance, &lt;code&gt;Apple&lt;/code&gt; might refer to the company and not the actual fruit.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Change Background Opacity Without Affecting Children In CSS]]></title><description><![CDATA[The simplest way to create a transparent background,  without  affecting any child elements, is by  using the   rgba   function . This only…]]></description><link>https://dilshankelsen.com/change-background-opacity-without-affecting-children-css/</link><guid isPermaLink="false">https://dilshankelsen.com/change-background-opacity-without-affecting-children-css/</guid><pubDate>Thu, 09 Sep 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The simplest way to create a transparent background, &lt;em&gt;without&lt;/em&gt; affecting any child elements, is by &lt;strong&gt;using the&lt;/strong&gt; &lt;code&gt;rgba&lt;/code&gt; &lt;strong&gt;function&lt;/strong&gt;. This only affects the color itself and has &lt;a href=&quot;https://caniuse.com/?search=rgba&quot; target=&quot;_blank&quot;&gt;outstanding support&lt;/a&gt; among browsers.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-css&quot;&gt;CSS&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-css&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token property&quot;&gt;background-color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color function&quot;&gt;rgba&lt;/span&gt;&lt;span class=&quot;token color punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token color number&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;token color punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token color&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token color punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token color&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token color punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token color&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color number&quot;&gt;0.3&lt;/span&gt;&lt;span class=&quot;token color punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;strong&gt;4th and last argument&lt;/strong&gt; of this function determines the color&amp;#x27;s opacity, with 1 being fully opaque and 0 being completely transparent.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Adding Borders To A CSS Triangle]]></title><description><![CDATA[To add a border to a triangle that was created through  borders or a  clip-path  property , you simply have  overlay a slightly smaller…]]></description><link>https://dilshankelsen.com/adding-borders-to-css-triangle/</link><guid isPermaLink="false">https://dilshankelsen.com/adding-borders-to-css-triangle/</guid><pubDate>Mon, 06 Sep 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;To add a border to a triangle that was created through &lt;a href=&quot;/create-triangle-in-css&quot;&gt;borders or a &lt;code&gt;clip-path&lt;/code&gt; property&lt;/a&gt;, you simply have &lt;strong&gt;overlay a slightly smaller triangle&lt;/strong&gt; that gives the illusion 🧙‍♀️ of a border.&lt;/p&gt;&lt;p&gt;Take this black triangle below as an &lt;em&gt;example&lt;/em&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-css&quot;&gt;CSS&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-css&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token selector class&quot;&gt;.triangle&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;border-bottom&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; solid &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;border-top&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; solid &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color&quot;&gt;black&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;border-left&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; solid &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;border-right&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; solid &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:130px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:54.61538461538461%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA0ElEQVQoz6XROwqEMBAG4MHHJW0UBcGjWNmIaGEnKII38hwWvn43gQzqbswuOzCEkOTjT0LTNKEsS2RZhqIokOf5zy3OpWmKcRxBeFVd1yCiv9rzPMzzDNq2TZgYhoEXHceBZVnGdl1X7k+SBKpo33csyyInfd8zKg48JbJtW45xHDMmwskrn9G2bY2owqIoumCc8I52XadFnzBhcMJvkiosDMOPGIM6tGkaRtUH+L7P2LquF0xeGacyoUEQPGJvoA6tqsqYTAveUfVGuje71wG5ymPmBi/D1QAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Black triangle using borders&quot; title=&quot;Black triangle using borders&quot; src=&quot;/static/ac363421efce9c59f4af0cb38a74375d/ffd64/image-1.png&quot; srcSet=&quot;/static/ac363421efce9c59f4af0cb38a74375d/ffd64/image-1.png 130w&quot; sizes=&quot;(max-width: 130px) 100vw, 130px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Black triangle&lt;/figcaption&gt;&lt;p&gt;The simplest way to overlay another triangle is by &lt;em&gt;absolute positioning&lt;/em&gt; it as a &lt;strong&gt;pseudo-element&lt;/strong&gt;. First, set the &lt;em&gt;parent&lt;/em&gt; triangle to a &lt;em&gt;relative&lt;/em&gt; position.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;diff&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-diff&quot;&gt;DIFF&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-diff&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;.triangle {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token unchanged prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token unchanged line&quot;&gt; width: 0;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token unchanged line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token unchanged prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token unchanged line&quot;&gt; height: 0;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token unchanged line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted line&quot;&gt; position: relative;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token inserted-sign inserted line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token unchanged prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token unchanged line&quot;&gt; ...&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token unchanged line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Next, &lt;em&gt;shave off some pixels&lt;/em&gt; from the overlaying triangle and try to position it inside the parent. There&amp;#x27;s no magic formula, just some good ol&amp;#x27; 🧓 &lt;strong&gt;trial and error&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-css&quot;&gt;CSS&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-css&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token selector class&quot;&gt;.triangle&lt;/span&gt;&lt;span class=&quot;token selector pseudo-element&quot;&gt;:before&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; absolute&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;border-bottom&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; solid &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;border-top&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; solid &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color&quot;&gt;white&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;border-left&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; solid &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;border-right&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; solid &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;48&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;top&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;-49&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;-48&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:130px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:55.38461538461539%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA4UlEQVQoz5WS2QqEMAxF+/+fJr6KK774KCrivt7hlok4tc44gVDSJCdLq/q+RxAESNMUcRwjSZK/lDnMJaOqKqhlWeA4DrIsQ9M0KIoCZVk+UsbWdY08z+G6rs5XeAsrDcOAf2Xfd93hNE3aVrygbNsG3/d1FbHpsyl9lHmeEYYhuq47cpRUkQvuom3bwxa/xMjJVUVRBL7BOVaZgTxt0HOMdGbC6FcmTE4mmOMLjAVtsAN4B+VI0ukTmB75DLyDcunruupHk59gg30Av0H5gT3P+wm7AO/GZ3fjOF58JozyAjwqVjt/+1YtAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;White triangle on black triangle&quot; title=&quot;White triangle on black triangle&quot; src=&quot;/static/9c2f599cb092c4d4ad89bd33f840ebe5/ffd64/image-2.png&quot; srcSet=&quot;/static/9c2f599cb092c4d4ad89bd33f840ebe5/ffd64/image-2.png 130w&quot; sizes=&quot;(max-width: 130px) 100vw, 130px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;White overlaying triangle&lt;/figcaption&gt;</content:encoded></item><item><title><![CDATA[How To Create A Triangle In CSS]]></title><description><![CDATA[There are  2 main ways  to create a triangle in CSS: using  borders  or a  clip path . Borders This method is the most battle-tested and…]]></description><link>https://dilshankelsen.com/create-triangle-in-css/</link><guid isPermaLink="false">https://dilshankelsen.com/create-triangle-in-css/</guid><pubDate>Sat, 04 Sep 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;There are &lt;strong&gt;2 main ways&lt;/strong&gt; to create a triangle in CSS: using &lt;em&gt;borders&lt;/em&gt; or a &lt;em&gt;clip path&lt;/em&gt;.&lt;/p&gt;&lt;h2 id=&quot;borders&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#borders&quot; aria-label=&quot;borders permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Borders&lt;/h2&gt;&lt;p&gt;This method is the most battle-tested and enjoys &lt;em&gt;support&lt;/em&gt; among virtually all browsers. Here&amp;#x27;s how it&amp;#x27;s done: Pick an element,&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;triangle&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and &lt;strong&gt;apply a thick border&lt;/strong&gt; to each side using a different colour. 🎨&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-css&quot;&gt;CSS&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-css&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token selector class&quot;&gt;.triangle&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;border-bottom&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; solid &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;border-top&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; solid &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color&quot;&gt;green&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;border-left&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; solid &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;border-right&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; solid &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color&quot;&gt;yellow&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:113px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:99.11504424778761%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB7ElEQVQ4y5WVS07cQBRFX5UbiRkzJDCfhTCBNRApAmfEJEtgA0F8R3ySDHsWo6yBVURKlLQJGbAAxASVP3W5RZVpQ7uNY+n6lcvy0Sk/qywWJQzuYIoCpjT/naIyeCgM7k0Od8jNNbD0cReLw1nEF6uIv8S9s/w1xsLnFcyfz+Hq3zsPHP3hSZj3m5BTgewx+z1yEOonwfAHAVhDWZGTXVfQ4qH6QwJ1IVCHM1DHamqiEwU5Yg4ULn8NnBdhG7DWATOeVOktXZJtyLkzGPAh1mN5UfVJuKZd+tOZRcgrIWzdLznLbIC9DZ2ECWERYW683jCUAO0wbYc5s+hp3DAMgA6oPoymwPySa8NXwHaoTrYgZ66bugU2NuwA1tBx9yXZQXrrHtLIyybMG/YCaqkI9fOp7ADfaUagzTlBkIWvPYAONl5yqraeBrkQlgazvIa5OhXYApNtD9N88WGyhiJvGE42pQMmAw/jC7Xjmw1oMHz9YasumMgzFBPQhqH7sEfZG2bSMGuFRsE0GP7N2E0d7uvEqSJXM7CKLWbq+iKRa3/Ipd8cUG54w98jD/smm8FM2s26MvTbF/dqLvmGsKVdYHEWRbwKG8cAU9fOLDMLK8D8HHAVNlhYYs0dSlPAGsOxea69wu0fD8y9/wU8AllF6K2P7aooAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Borders with different colours&quot; title=&quot;Borders with different colours&quot; src=&quot;/static/b3cf0697ed8aeb1e84eca0cd73fde567/2519b/image-1.png&quot; srcSet=&quot;/static/b3cf0697ed8aeb1e84eca0cd73fde567/2519b/image-1.png 113w&quot; sizes=&quot;(max-width: 113px) 100vw, 113px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Each border resembles a triangle&lt;/figcaption&gt;&lt;p&gt;Doing this, you get 4 triangles! The &lt;em&gt;key&lt;/em&gt; is to have a &lt;strong&gt;collapsed element&lt;/strong&gt;, i.e. no width or height. Of course, you only want 1 triangle to be visible, so you simply make the others &lt;em&gt;transparent&lt;/em&gt;. Let&amp;#x27;s go for the green one! 🟢&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-css&quot;&gt;CSS&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-css&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token selector class&quot;&gt;.triangle&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  ...&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;border-bottom&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; solid &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;border-top&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; solid &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color&quot;&gt;green&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;border-left&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; solid &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;border-right&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; solid &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color&quot;&gt;transparent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:108px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:53.70370370370371%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA0UlEQVQoz52RvQqDQBCEBxF8Lhs79YnS5IdA/p4jBGJnL/gcaUIaa0khXNy4G1ckeGoyMHDH7XzMciieBaW3lPJHTtk9+8ucZQazUFYl+WefsABh23jzoznTZJnBLFCrOIkJa5B38sg5OLPMs5zhrArmZbpLlESEFcg9uoQ9Ri0zS0hGxSzoQRVeQxmUwA4fK6i9K4xn+zBpWNc1zYLOgDFLgENQWf8LaltTYQLUh6mmU826T6GerNDmo9hTsEHgEDS4BGLbmlagrWllKvFYM9Ubp3CQ1o2V18AAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Green triangle&quot; title=&quot;Green triangle&quot; src=&quot;/static/b9d7f373db1ab02d585ab3cc4648d9e6/8eb6e/image-2.png&quot; srcSet=&quot;/static/b9d7f373db1ab02d585ab3cc4648d9e6/8eb6e/image-2.png 108w&quot; sizes=&quot;(max-width: 108px) 100vw, 108px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Triangle using single border&lt;/figcaption&gt;&lt;h2 id=&quot;clip-path&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#clip-path&quot; aria-label=&quot;clip path permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Clip Path&lt;/h2&gt;&lt;p&gt;Syntactically, &lt;code&gt;clip-path&lt;/code&gt; is easier to write and maintain. However, it&amp;#x27;s &lt;strong&gt;support in older browsers&lt;/strong&gt; may be lacking.&lt;/p&gt;&lt;p&gt;The &lt;code&gt;polygon&lt;/code&gt; geometric shape takes &lt;strong&gt;any amount of arguments&lt;/strong&gt;. Each argument 💬 represents a &lt;em&gt;corner of your shape&lt;/em&gt; and takes 2 values: x and y.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;em&gt;X&lt;/em&gt;: x coordinate starting from the left to the right of the element&lt;/li&gt;&lt;li&gt;&lt;em&gt;Y&lt;/em&gt;: y coordinate starting from the top to the bottom of the element&lt;/li&gt;&lt;/ul&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-css&quot;&gt;CSS&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-css&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token selector class&quot;&gt;.triangle&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;background-color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;clip-path&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;polygon&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;/* polygon(1[x y], 2[x y], 3[x y]) */&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:150px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:101.33333333333334%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAACCUlEQVQ4y5WUvU4CQRDHV0FrSIyxM7EwoTCExEI0JsYvQrCgIbGwML6A7+Fz+AIYE7W1s7HT3hZCAncE2APn78wuh8feCbjJ3M7tzf7vN7MfCtw8z4PWGr7vx/vBAH6/D00Ev9OBDoJYnMwfDociBSVOq9UyAzHhdhuag/x6HfrtDf73t/2BI9jtdtFsNo2vOvxXGZjZCgWgVrP+aJQY0uv1EDC9koeYNOK0JsbEJGMPD6CdHdD2Nuj93Y6xaDQ2WjYljlgoiNA4PdP29oB8HsjlgMtLOyb1GseFgpJlIuFkgrSXF66yAra2gM1N639+TqW+GGFId3xsRaJ2czP10/mELt3S0q/J+8oK8PExoZxPGNKdnFiBVOqXLvQjlLMJk+jclB1KGtcymdCt3fJyXMyhDCHihFIP+fL8PJvOoSRbK4eQT4sZl8fR0d90CZRmDmc2TciH3wg+PS1GF9rqKmi8Lz0594aQ6eTFCMqpcFd2DiVdX9saso4lZDqzxo+PoLU1UDYLksBFbGMDlMmYWnq8OJq1LKGs1OEhqFgE7e/b4HQaxHVMNPkmMRcXoN1d4OoKcl8JnBK64P7eplGtAuvri9VPjEtEcs7Z915fzd2pfM776/YWARM2KhXoUgmNszNotsb5OfTpqe2T3stlDLhvHhygfXdnSqdkD8l2FhuNt85/+2Hkov0B5If9DvC3iY4AAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Triangle using Clip Path&quot; title=&quot;Triangle using Clip Path&quot; src=&quot;/static/b345e72de98b4408796d79c75c40a199/8a4e8/image-3.png&quot; srcSet=&quot;/static/b345e72de98b4408796d79c75c40a199/8a4e8/image-3.png 150w&quot; sizes=&quot;(max-width: 150px) 100vw, 150px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Triangle using Clip Path&lt;/figcaption&gt;&lt;p&gt;The coordinates may be expressed in any unit, e.g. &lt;code&gt;%&lt;/code&gt;, &lt;code&gt;px&lt;/code&gt;, &lt;code&gt;em&lt;/code&gt;, etc. I recommend checking out this &lt;strong&gt;awesome&lt;/strong&gt; &lt;a href=&quot;https://bennettfeely.com/clippy/&quot; target=&quot;_blank&quot;&gt;Clip Path generator&lt;/a&gt; whenever you wish to create a shape using &lt;code&gt;clip-path&lt;/code&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Getting Started With React Popper]]></title><description><![CDATA[I've found the React Popper documentation to  not be very comprehensive . This naturally lead to  difficulties  implementing the library…]]></description><link>https://dilshankelsen.com/getting-started-with-react-popper/</link><guid isPermaLink="false">https://dilshankelsen.com/getting-started-with-react-popper/</guid><pubDate>Thu, 02 Sep 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;I&amp;#x27;ve found the React Popper documentation to &lt;strong&gt;not be very comprehensive&lt;/strong&gt;. This naturally lead to &lt;em&gt;difficulties&lt;/em&gt; implementing the library. Here are my findings to help you get started. ⛑&lt;/p&gt;&lt;div&gt;&lt;p&gt;What you&amp;#x27;ll quickly realise is that you will have to mostly &lt;strong&gt;refer to the base PopperJS documentation&lt;/strong&gt; instead React Popper. The key is knowing how that documentation &lt;em&gt;translates&lt;/em&gt; into React.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;usepopper-hook&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#usepopper-hook&quot; aria-label=&quot;usepopper hook permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;usePopper hook&lt;/h2&gt;&lt;p&gt;This is &lt;em&gt;undoubtedly&lt;/em&gt; the most &lt;strong&gt;important&lt;/strong&gt; part of your Popper component. This is essentially a wrapper of the &lt;code&gt;createPopper&lt;/code&gt; &lt;a href=&quot;https://popper.js.org/docs/v2/constructors&quot; target=&quot;_blank&quot;&gt;constructor function&lt;/a&gt; that allows you to configure everything.&lt;/p&gt;&lt;p&gt;The 3&lt;sup&gt;rd&lt;/sup&gt; argument of this hook is an object which can hold up to 3 options, i.e. &lt;code&gt;placement&lt;/code&gt;, &lt;code&gt;modifiers&lt;/code&gt; and &lt;code&gt;strategy&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; styles&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; attributes &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;usePopper&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;referenceElement&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; popperElement&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;placement&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;right-start&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;modifiers&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;arrow&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; arrowElement &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;modifiers&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#modifiers&quot; aria-label=&quot;modifiers permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Modifiers&lt;/h3&gt;&lt;p&gt;Modifiers are the &lt;a href=&quot;https://popper.js.org/docs/v2/modifiers/&quot; target=&quot;_blank&quot;&gt;real meat&lt;/a&gt; of this library. They allow you to configure 🔧 the behavior of your Popper, with each modifier being &lt;strong&gt;its own object&lt;/strong&gt; in the &lt;code&gt;modifiers&lt;/code&gt; array.&lt;/p&gt;&lt;div&gt;&lt;p&gt;Be careful with using functions inside the configuration as they can &lt;em&gt;cause a loop&lt;/em&gt;. Check out more about this in the &lt;a href=&quot;https://popper.js.org/react-popper/v2/faq/#why-i-get-render-loop-whenever-i-put-a-function-inside-the-popper-configuration&quot; target=&quot;_blank&quot;&gt;FAQ section&lt;/a&gt;.&lt;/p&gt;&lt;/div&gt;&lt;p&gt;As you&amp;#x27;d expect, PopperJS already has &lt;strong&gt;default behavior&lt;/strong&gt; for many of these modifiers, e.g. &lt;em&gt;Flip&lt;/em&gt; and &lt;em&gt;Prevent Overflow&lt;/em&gt;.&lt;/p&gt;&lt;h2 id=&quot;arrow&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#arrow&quot; aria-label=&quot;arrow permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Arrow&lt;/h2&gt;&lt;p&gt;By default, an arrow ⬆️ is not enabled. A code example of one can be found in &lt;a href=&quot;https://popper.js.org/react-popper/v2/#example&quot; target=&quot;_blank&quot;&gt;the documentation&lt;/a&gt;. Most notably, the &lt;em&gt;styling&lt;/em&gt; of the arrow and it&amp;#x27;s placement relative to the Popper &lt;strong&gt;is your responsibility&lt;/strong&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Make HTML Element Fill Remaining Space]]></title><description><![CDATA[The best way to approach this problem is by  using Flexbox . Take this  div  container with  two child elements : It has the following CSS…]]></description><link>https://dilshankelsen.com/make-html-element-fill-remaining-space/</link><guid isPermaLink="false">https://dilshankelsen.com/make-html-element-fill-remaining-space/</guid><pubDate>Mon, 30 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The best way to approach this problem is by &lt;strong&gt;using Flexbox&lt;/strong&gt;. Take this &lt;code&gt;div&lt;/code&gt; container with &lt;em&gt;two child elements&lt;/em&gt;:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It has the following CSS applied where the first child has a &lt;em&gt;fixed width&lt;/em&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-css&quot;&gt;CSS&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-css&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token selector class&quot;&gt;.container&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; flex&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;border&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; solid&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color&quot;&gt;white&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token selector class&quot;&gt;.container&lt;/span&gt;&lt;span class=&quot;token selector&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token selector combinator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token selector&quot;&gt; div&lt;/span&gt;&lt;span class=&quot;token selector pseudo-class&quot;&gt;:first-child&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;background-color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token selector class&quot;&gt;.container&lt;/span&gt;&lt;span class=&quot;token selector&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token selector combinator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token selector&quot;&gt; div&lt;/span&gt;&lt;span class=&quot;token selector pseudo-class&quot;&gt;:last-child&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;background-color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token color&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:3.4999999999999996%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAABCAYAAADeko4lAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAGklEQVQI12N4/e3H/3dvXv9/+/bd/3fvKMcA+4NLvaJzeOcAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Parent container with 2 child elements&quot; title=&quot;Parent container with 2 child elements&quot; src=&quot;/static/f435a157feafb10b20d6b31f64290fb6/5a190/image-1.png&quot; srcSet=&quot;/static/f435a157feafb10b20d6b31f64290fb6/772e8/image-1.png 200w,/static/f435a157feafb10b20d6b31f64290fb6/e17e5/image-1.png 400w,/static/f435a157feafb10b20d6b31f64290fb6/5a190/image-1.png 800w,/static/f435a157feafb10b20d6b31f64290fb6/5d72a/image-1.png 834w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Parent container with 2 child elements&lt;/figcaption&gt;&lt;p&gt;By adding a &lt;a href=&quot;https://www.w3schools.com/cssref/css3_pr_flex.asp&quot; target=&quot;_blank&quot;&gt;flex property&lt;/a&gt; to the 2&lt;sup&gt;nd&lt;/sup&gt; child, we can make it fill up the remaining space in the parent container.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;diff&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-diff&quot;&gt;DIFF&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-diff&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;.container &amp;gt; div:last-child {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token unchanged prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token unchanged line&quot;&gt; background-color: blue;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token unchanged line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted line&quot;&gt; flex: 1;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token inserted-sign inserted line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:3.4999999999999996%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAABCAYAAADeko4lAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAEklEQVQI12NwsH/9397+DdUwAIO0MDUipgsHAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Child element filling up remaining space&quot; title=&quot;Child element filling up remaining space&quot; src=&quot;/static/1a6f280bd42ce1784cdd664f085237c7/5a190/image-2.png&quot; srcSet=&quot;/static/1a6f280bd42ce1784cdd664f085237c7/772e8/image-2.png 200w,/static/1a6f280bd42ce1784cdd664f085237c7/e17e5/image-2.png 400w,/static/1a6f280bd42ce1784cdd664f085237c7/5a190/image-2.png 800w,/static/1a6f280bd42ce1784cdd664f085237c7/27b8e/image-2.png 836w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;2nd child filling up remaining space&lt;/figcaption&gt;</content:encoded></item><item><title><![CDATA[Tuples In TypeScript Simply Explained]]></title><description><![CDATA[Tuples are arrays of a  fixed minimum  size and a specific element  type order . Its  type annotation  resembles an array with elements of…]]></description><link>https://dilshankelsen.com/tuples-in-typescript-simply-explained/</link><guid isPermaLink="false">https://dilshankelsen.com/tuples-in-typescript-simply-explained/</guid><pubDate>Sat, 28 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Tuples are arrays of a &lt;strong&gt;fixed minimum&lt;/strong&gt; size and a specific element &lt;strong&gt;type order&lt;/strong&gt;. Its &lt;em&gt;type annotation&lt;/em&gt; resembles an array with elements of types.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; personTuple&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;personTuple &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Type &amp;#x27;string&amp;#x27; is not assignable to type &amp;#x27;[string, number]&amp;#x27;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;personTuple &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Jack&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Type error!&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;personTuple &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Anna&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;27&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;personTuple &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;John&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;35&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Doe&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Works as well! 😃&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;TypeScript &lt;em&gt;throws an error&lt;/em&gt; whenever you assign an insufficiently sized array or the wrong type order. However, there is &lt;strong&gt;no size restriction&lt;/strong&gt; beyond the required minimum as the last line demonstrates!&lt;/p&gt;&lt;h2 id=&quot;adding-elements&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#adding-elements&quot; aria-label=&quot;adding elements permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Adding elements&lt;/h2&gt;&lt;p&gt;Whenever you add elements, you should be aware that &lt;strong&gt;array indexing&lt;/strong&gt; is &lt;em&gt;not permitted&lt;/em&gt; 🙅‍♀️ by the linter &lt;em&gt;beyond the defined size&lt;/em&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;personTuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Bob&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Type &amp;#x27;&amp;quot;Bob&amp;quot;&amp;#x27; is not assignable to type &amp;#x27;undefined&amp;#x27;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;personTuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;personTuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Tuple type &amp;#x27;[string, number]&amp;#x27; of length &amp;#x27;2&amp;#x27; has no element at index &amp;#x27;3&amp;#x27;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;personTuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// [&amp;quot;John&amp;quot;, 35, &amp;quot;Bob&amp;quot;, 1]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Of course, the code will still work as it&amp;#x27;s all valid JavaScript.&lt;/p&gt;&lt;h2 id=&quot;not-arrays&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#not-arrays&quot; aria-label=&quot;not arrays permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Not arrays&lt;/h2&gt;&lt;p&gt;While tuples &lt;em&gt;are&lt;/em&gt; arrays &lt;strong&gt;in JavaScript&lt;/strong&gt;, &lt;em&gt;TypeScript&lt;/em&gt; considers them as 2 ✌️ separate entities. Consequently, assigning an array to a tuple will &lt;em&gt;fail&lt;/em&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; personArray &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Mark&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;41&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;personTuple &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; personArray &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Type error!&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;spread-syntax&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#spread-syntax&quot; aria-label=&quot;spread syntax permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Spread syntax&lt;/h2&gt;&lt;p&gt;A common use case for tuples is &lt;strong&gt;spreading them as arguments&lt;/strong&gt; to a function. This can be particularly useful for a &lt;em&gt;large&lt;/em&gt; amount of parameters.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;printPerson&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; age&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; age&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;printPerson&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;personTuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// &amp;quot;Mark&amp;quot;,  41&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Define Functions In TypeScript]]></title><description><![CDATA[Here's a quick rundown on functions in TypeScript. 💁‍♂️ Parameters Parameters  should always  be followed by type annotations. The…]]></description><link>https://dilshankelsen.com/define-functions-in-typescript/</link><guid isPermaLink="false">https://dilshankelsen.com/define-functions-in-typescript/</guid><pubDate>Thu, 26 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Here&amp;#x27;s a quick rundown on functions in TypeScript. 💁‍♂️&lt;/p&gt;&lt;h2 id=&quot;parameters&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#parameters&quot; aria-label=&quot;parameters permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Parameters&lt;/h2&gt;&lt;p&gt;Parameters &lt;em&gt;should always&lt;/em&gt; be followed by type annotations. The exception to this rule are default values as TypeScript is &lt;strong&gt;able to infer the type&lt;/strong&gt; from the values themselves. Is a parameter &lt;em&gt;optional&lt;/em&gt;? Mark it with a &lt;code&gt;?&lt;/code&gt; or else your linter will throw an error when calling the function.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;printName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;firstName&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; lastName &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Smith&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; middleName&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;middleName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;firstName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; middleName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; lastName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;firstName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; lastName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;return-value&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#return-value&quot; aria-label=&quot;return value permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Return value&lt;/h2&gt;&lt;p&gt;While TypeScript can &lt;strong&gt;infer a function&amp;#x27;s return type&lt;/strong&gt;, it&amp;#x27;s good practice 👍 to &lt;em&gt;explicitly state it&lt;/em&gt; as it serves as a form of self-documentation. The type annotation is written &lt;strong&gt;after&lt;/strong&gt; the parentheses and &lt;strong&gt;before&lt;/strong&gt; the function body.&lt;/p&gt;&lt;p&gt;If the function doesn&amp;#x27;t return anything, type &lt;code&gt;void&lt;/code&gt; is used.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getNumber&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;sayHello&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Hello!&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;arrow-functions&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#arrow-functions&quot; aria-label=&quot;arrow functions permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Arrow functions&lt;/h2&gt;&lt;p&gt;In arrow functions, type annotations follow the &lt;em&gt;same syntax&lt;/em&gt; as in function declarations.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-typescript&quot;&gt;TYPESCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-typescript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; isNumber5 &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; value &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Bind In JavaScript Simply Explained]]></title><description><![CDATA[As the name implies, the  bind  method  binds an object to a function  and makes said object  accessible  in that function through the  this…]]></description><link>https://dilshankelsen.com/bind-in-javascript-simply-explained/</link><guid isPermaLink="false">https://dilshankelsen.com/bind-in-javascript-simply-explained/</guid><pubDate>Mon, 23 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;As the name implies, the &lt;code&gt;bind&lt;/code&gt; method &lt;strong&gt;binds an object to a function&lt;/strong&gt; and makes said object &lt;em&gt;accessible&lt;/em&gt; in that function through the &lt;code&gt;this&lt;/code&gt; &lt;em&gt;keyword&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;Take the example below. We define and call a function that is &lt;em&gt;not yet bound&lt;/em&gt; to the object &lt;code&gt;user&lt;/code&gt;. Consequently, &lt;code&gt;this&lt;/code&gt; refers to the &lt;strong&gt;global&lt;/strong&gt; &lt;code&gt;window&lt;/code&gt; &lt;strong&gt;object&lt;/strong&gt; instead.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; user &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;John Doe&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;printUser&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;printUser&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Window&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;After we bind &lt;code&gt;user&lt;/code&gt; to &lt;code&gt;printUser&lt;/code&gt;, we actually print out the &lt;em&gt;user object&lt;/em&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;printUser&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Object { 1: &amp;quot;John Doe&amp;quot; }&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; func &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; printUser&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Object { 1: &amp;quot;John Doe&amp;quot; }&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;2 things to &lt;strong&gt;be aware of&lt;/strong&gt;:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;The &lt;code&gt;bind&lt;/code&gt; method duplicates the original function with the new object &lt;em&gt;bound&lt;/em&gt; to it. How you call it is up to you.&lt;/li&gt;&lt;li&gt;This will &lt;strong&gt;not work on arrow functions&lt;/strong&gt;!&lt;/li&gt;&lt;/ul&gt;&lt;h2 id=&quot;adding-extra-arguments&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#adding-extra-arguments&quot; aria-label=&quot;adding extra arguments permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Adding extra arguments&lt;/h2&gt;&lt;p&gt;You may also wish to pass additional arguments to your function &lt;strong&gt;while binding&lt;/strong&gt;. Let&amp;#x27;s &lt;em&gt;modify our existing function&lt;/em&gt; to accept and print a string.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;printUserAndText&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; text&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;printUserAndText&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Banana&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Object { 1: &amp;quot;John Doe&amp;quot; } Banana&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It may be &lt;em&gt;confusing&lt;/em&gt; at first, but parameters defined in your function must be passed into &lt;code&gt;bind&lt;/code&gt; &lt;strong&gt;from the second argument&lt;/strong&gt; onwards.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Button Inside Label Not Triggering Input]]></title><description><![CDATA[When you nest a button inside a  label  tag, with the latter  targeting an input , clicking the button will  not trigger  the input. Take…]]></description><link>https://dilshankelsen.com/button-inside-label-not-triggering-input/</link><guid isPermaLink="false">https://dilshankelsen.com/button-inside-label-not-triggering-input/</guid><pubDate>Sat, 21 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;When you nest a button inside a &lt;code&gt;label&lt;/code&gt; tag, with the latter &lt;strong&gt;targeting an input&lt;/strong&gt;, clicking the button will &lt;em&gt;not trigger&lt;/em&gt; the input. Take this checkbox as an example:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;checkbox&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;potato&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;potato&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Potato&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Clicking the button will not select the checkbox. &lt;strong&gt;What can be done&lt;/strong&gt;? As far as I know, there are 2 possibilities:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Switch the button for a &lt;code&gt;div&lt;/code&gt; that is styled like one, &lt;em&gt;or&lt;/em&gt;&lt;/li&gt;&lt;li&gt;Apply the following style to the nested button:&lt;/li&gt;&lt;/ul&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-css&quot;&gt;CSS&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-css&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token selector&quot;&gt;label &lt;/span&gt;&lt;span class=&quot;token selector combinator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token selector&quot;&gt; button&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;pointer-events&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; none&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;A &lt;strong&gt;great explanation&lt;/strong&gt; on the possible cause of this issue can be found on &lt;a href=&quot;https://stackoverflow.com/a/31944858&quot; target=&quot;_blank&quot;&gt;Stack Overflow&lt;/a&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Animate HTML Accordion Opening]]></title><description><![CDATA[Animating an  HTML Accordion  is quite straightforward once you know how to  target the pertinent elements . For the animation, we target…]]></description><link>https://dilshankelsen.com/animate-html-accordion-opening/</link><guid isPermaLink="false">https://dilshankelsen.com/animate-html-accordion-opening/</guid><pubDate>Thu, 19 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Animating an &lt;a href=&quot;/building-an-accordion-without-javascript-css&quot;&gt;HTML Accordion&lt;/a&gt; is quite straightforward once you know how to &lt;strong&gt;target the pertinent elements&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-css&quot;&gt;CSS&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-css&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token selector&quot;&gt;details&lt;/span&gt;&lt;span class=&quot;token selector attribute punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token selector attribute attr-name&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;token selector attribute punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token selector&quot;&gt; summary &lt;/span&gt;&lt;span class=&quot;token selector combinator&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;token selector&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;animation&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; accordion &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;.5&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; ease-in-out&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;For the animation, we target the &lt;em&gt;opened&lt;/em&gt; state and any elements inside &lt;code&gt;details&lt;/code&gt; that follow the &lt;code&gt;summary&lt;/code&gt; tag - hence &lt;code&gt;~ *&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-css&quot;&gt;CSS&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-css&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token atrule rule&quot;&gt;@keyframes&lt;/span&gt;&lt;span class=&quot;token atrule&quot;&gt; accordion&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token selector&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;opacity&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;margin-top&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;-10&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token selector&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;opacity&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;margin-top&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;token unit&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;The keyframes&lt;/strong&gt; themselves are nothing special: some simple vertical movement and change in opacity. To see everything in action, &lt;em&gt;check out my&lt;/em&gt; &lt;a href=&quot;https://codepen.io/dkelsen/pen/dyvXJPJ&quot; target=&quot;_blank&quot;&gt;Codepen snippet&lt;/a&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Feature Flags Simply Explained]]></title><description><![CDATA[A great way to understand what feature flags are is by  contrasting it  to an alternative: feature branches 🌳.  The main purpose of feature…]]></description><link>https://dilshankelsen.com/feature-flags-simply-explained/</link><guid isPermaLink="false">https://dilshankelsen.com/feature-flags-simply-explained/</guid><pubDate>Mon, 16 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A great way to understand what feature flags are is by &lt;strong&gt;contrasting it&lt;/strong&gt; to an alternative: feature branches 🌳. &lt;/p&gt;&lt;p&gt;The main purpose of feature branches is to keep the main branch &lt;em&gt;clean and releasable&lt;/em&gt; at all times while having &lt;em&gt;incomplete and unreleasable&lt;/em&gt; features in separate branches. Of course, &lt;em&gt;this approach&lt;/em&gt; has its own &lt;strong&gt;pros and cons&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;And then there are feature flags. 🚩&lt;/p&gt;&lt;h2 id=&quot;working-in-master&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#working-in-master&quot; aria-label=&quot;working in master permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Working in master&lt;/h2&gt;&lt;p&gt;In a way, they are the opposite of feature branches: every developer is working in the master branch. However, incomplete features are &lt;strong&gt;&amp;quot;toggled&amp;quot; off&lt;/strong&gt; so that they don&amp;#x27;t appear in production. This toggle is what&amp;#x27;s often referred to as a &lt;em&gt;feature flag&lt;/em&gt; or &lt;em&gt;feature toggle&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;As an example, imagine you&amp;#x27;re working on some sort of widget for a webpage. 💻 Its feature flag is nothing more than an &lt;code&gt;if&lt;/code&gt; statement that one can configure using an &lt;strong&gt;environment variable&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-text&quot;&gt;TEXT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-text&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;if env.WIDGET_ACTIVE: showWidget() # Pseudocode&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This allows you to &lt;em&gt;dynamically switch on&lt;/em&gt; 💡 the feature at a later date once it&amp;#x27;s ready &lt;strong&gt;without having to redeploy&lt;/strong&gt; the entire application.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Style Arrow Of HTML Accordion]]></title><description><![CDATA[To change the arrow of an  HTML accordion , we need to use  the pseudo-element   ::marker  on the  summary  tag. To change the default, all…]]></description><link>https://dilshankelsen.com/style-arrow-of-html-accordion/</link><guid isPermaLink="false">https://dilshankelsen.com/style-arrow-of-html-accordion/</guid><pubDate>Sat, 14 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;To change the arrow of an &lt;a href=&quot;/building-an-accordion-without-javascript-css&quot;&gt;HTML accordion&lt;/a&gt;, we need to use &lt;strong&gt;the pseudo-element&lt;/strong&gt; &lt;code&gt;::marker&lt;/code&gt; on the &lt;code&gt;summary&lt;/code&gt; tag.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-css&quot;&gt;CSS&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-css&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token selector&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;token selector pseudo-element&quot;&gt;::marker&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;\f138&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27; &amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;/* Required for FA icons */&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;font-family&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Font Awesome 5 Free&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;font-weight&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;700&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To change the default, all you need to do is &lt;strong&gt;add a&lt;/strong&gt; &lt;code&gt;content&lt;/code&gt; &lt;strong&gt;property&lt;/strong&gt; and an appropriate value. In this example, I&amp;#x27;m using &lt;a href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css&quot; target=&quot;_blank&quot;&gt;Font Awesome&lt;/a&gt; to get a &lt;a href=&quot;https://fontawesome.com/cheatsheet&quot; target=&quot;_blank&quot;&gt;Chevron icon&lt;/a&gt;. &lt;em&gt;Notice&lt;/em&gt; how I&amp;#x27;ve added a 2&lt;sup&gt;nd&lt;/sup&gt; empty string to create some right-hand spacing.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:166px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:18.674698795180724%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAuElEQVQI132OuwqEQBRD54NsFfxMBQXB0lIcFAs7CytBsdEPUPD9wkHBQmY2CLvlniKEkFwuqes6z3MhBOdc/IV/+SVEVVVFUZIkadu2qqrruuaXruuaponjGHnf98MwTNPkum5RFGmalmWJJpEkSZZl3/ezLNN1HYZS6lEP3nEcwzCglmWZprnvexRFQRDYtq1pGvYEP4dhiB/O81zXFee3bTuOgzEGHccRGxjo8zzsBYVlWe77/gCGxcdM+lDH3AAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Chevron Icon Before Summary Title&quot; title=&quot;Chevron Icon Before Summary Title&quot; src=&quot;/static/de56a6f5d57b144d6069948e9a54313f/e72ac/image-1.png&quot; srcSet=&quot;/static/de56a6f5d57b144d6069948e9a54313f/e72ac/image-1.png 166w&quot; sizes=&quot;(max-width: 166px) 100vw, 166px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Custom Chevron icon&lt;/figcaption&gt;&lt;h2 id=&quot;opened-accordion&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#opened-accordion&quot; aria-label=&quot;opened accordion permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Opened accordion&lt;/h2&gt;&lt;p&gt;To change the icon of an opened accordion, we must take advantage of the &lt;em&gt;dynamically&lt;/em&gt; generated &lt;code&gt;open&lt;/code&gt; &lt;strong&gt;attribute&lt;/strong&gt; on the &lt;code&gt;details&lt;/code&gt; tag.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-css&quot;&gt;CSS&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-css&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token selector&quot;&gt;details&lt;/span&gt;&lt;span class=&quot;token selector attribute punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token selector attribute attr-name&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;token selector attribute punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token selector&quot;&gt; summary&lt;/span&gt;&lt;span class=&quot;token selector pseudo-element&quot;&gt;::marker&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;\f13a&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27; &amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;p&gt;Check out the code snippets above in an &lt;a href=&quot;https://codepen.io/dkelsen/pen/yLMJJBG&quot; target=&quot;_blank&quot;&gt;interactive Codepen editor&lt;/a&gt;.&lt;/p&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Redirect Your WordPress Site To A New Domain]]></title><description><![CDATA[For whatever reason, you've decided to redirect your WordPress traffic on  domainA.com  to  domainB.com . Here's how you do it! Install…]]></description><link>https://dilshankelsen.com/redirect-wordpress-site-to-new-domain/</link><guid isPermaLink="false">https://dilshankelsen.com/redirect-wordpress-site-to-new-domain/</guid><pubDate>Thu, 12 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;For whatever reason, you&amp;#x27;ve decided to redirect your WordPress traffic on &lt;code&gt;domainA.com&lt;/code&gt; to &lt;code&gt;domainB.com&lt;/code&gt;. Here&amp;#x27;s how you do it!&lt;/p&gt;&lt;h2 id=&quot;install-redirection&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#install-redirection&quot; aria-label=&quot;install redirection permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Install Redirection&lt;/h2&gt;&lt;p&gt;Go to &lt;strong&gt;Plugins &amp;gt; Add New&lt;/strong&gt;, search for the &lt;em&gt;Redirection&lt;/em&gt; plugin, then install and activate it.&lt;/p&gt;&lt;h2 id=&quot;enter-a-new-domain&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#enter-a-new-domain&quot; aria-label=&quot;enter a new domain permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Enter a new domain&lt;/h2&gt;&lt;p&gt;Access the plugin under &lt;strong&gt;Tools &amp;gt; Redirection&lt;/strong&gt; and go to the tab &lt;strong&gt;Site&lt;/strong&gt;. There you should see an input field for the domain you&amp;#x27;d like to redirect to. Enter your new domain, &lt;em&gt;click update&lt;/em&gt;, et voilà! &lt;/p&gt;&lt;p&gt;You&amp;#x27;re successfully redirecting your WordPress site.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:27%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAsklEQVQY033QbQuDIBQFYP//f9w+aPlalnpNiOBMbYwGax8e9IgeLjIhNey8YPKNx7wGuGX9EmMEpYT01vIdti4CUyC42ffCqRZY34pCLY+QS4KOBRMVxET90b7vOI7jJ2YUh5SqXyyloGxbt2XqiAghnYhyzznnW0yIAY/nE0IIDOOIlsdRwjkHW7VVaw1jGtP3Zzaf9XrOlFLgnEMMtUhK8FrcGGsRQugTtenT5Q//eQFVFnvq2AtL6wAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Site option of the Redirection WP plugin&quot; title=&quot;Site option of the Redirection WP plugin&quot; src=&quot;/static/d7335b57d591676caf55a59827e9476f/5a190/image-1.png&quot; srcSet=&quot;/static/d7335b57d591676caf55a59827e9476f/772e8/image-1.png 200w,/static/d7335b57d591676caf55a59827e9476f/e17e5/image-1.png 400w,/static/d7335b57d591676caf55a59827e9476f/5a190/image-1.png 800w,/static/d7335b57d591676caf55a59827e9476f/96e86/image-1.png 1089w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Sorting An Array Of Numbers In JavaScript]]></title><description><![CDATA[JavaScript offers a built-in function called  sort  that makes sorting  incredibly easy . Take  numbers  as an example. By default , the…]]></description><link>https://dilshankelsen.com/sorting-an-array-of-numbers-in-javascript/</link><guid isPermaLink="false">https://dilshankelsen.com/sorting-an-array-of-numbers-in-javascript/</guid><pubDate>Mon, 09 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;JavaScript offers a built-in function called &lt;code&gt;sort&lt;/code&gt; that makes sorting &lt;em&gt;incredibly easy&lt;/em&gt;. Take &lt;code&gt;numbers&lt;/code&gt; as an example.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; numbers &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;150&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;38&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Array(4) [ -4, 150, 2000, 38 ]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;By default&lt;/strong&gt;, the function will treat any array elements as strings, and consequently sort them alphabetically. 🔤 &lt;em&gt;To sort numerically&lt;/em&gt;, we need to pass in a callback function that &lt;em&gt;compares&lt;/em&gt; two numbers at a time.&lt;/p&gt;&lt;div&gt;&lt;p&gt;The &lt;code&gt;sort&lt;/code&gt; method modifies the original array.&lt;/p&gt;&lt;/div&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Ascending sort&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; b &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Descending sort&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The comparison happens by subtracting one number from the other, which will result in &lt;strong&gt;3 possible outcomes&lt;/strong&gt;:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;If the calculation returns &lt;em&gt;less than&lt;/em&gt; &lt;code&gt;0&lt;/code&gt;, nothing happens,&lt;/li&gt;&lt;li&gt;If &lt;em&gt;greater than&lt;/em&gt; &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; switch places, and&lt;/li&gt;&lt;li&gt;If &lt;em&gt;equals to&lt;/em&gt; &lt;code&gt;0&lt;/code&gt;, the order of &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; with respect to each other remains unchanged, but changes in relation to the rest of the array.&lt;/li&gt;&lt;/ul&gt;</content:encoded></item><item><title><![CDATA[Building An Accordion Without JavaScript And CSS]]></title><description><![CDATA[Gone are the days where you need to use JavaScript or an HTML and CSS hack to create an accordion. Why? Accordions are now  native to HTML…]]></description><link>https://dilshankelsen.com/building-an-accordion-without-javascript-css/</link><guid isPermaLink="false">https://dilshankelsen.com/building-an-accordion-without-javascript-css/</guid><pubDate>Sat, 07 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Gone are the days where you need to use JavaScript or an HTML and CSS hack to create an accordion. Why? Accordions are now &lt;strong&gt;native to HTML&lt;/strong&gt; with solid &lt;a href=&quot;https://caniuse.com/?search=details&quot; target=&quot;_blank&quot;&gt;browser support&lt;/a&gt;!&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;details&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Accordion Title&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;This will be in the accordion body.&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;And this too!&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;details&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;details&gt;&lt;summary&gt;Accordion Title&lt;/summary&gt;&lt;p&gt;This will be in the accordion body.&lt;/p&gt;&lt;p&gt;And this too!&lt;/p&gt;&lt;/details&gt;&lt;figcaption&gt;Native HTML accordion&lt;/figcaption&gt;&lt;p&gt;The &lt;code&gt;details&lt;/code&gt; tag contains the &lt;em&gt;content&lt;/em&gt; of your accordion, i.e. the expanded body. By default, it&amp;#x27;s closed. Inside of that, the &lt;code&gt;summary&lt;/code&gt; tag shows the &lt;em&gt;heading&lt;/em&gt; of your accordion.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Conditional Importing In JavaScript]]></title><description><![CDATA[To conditionally import a module or whatnot, you got to make use of  dynamic imports . As an example: Pay attention to the  import  keyword…]]></description><link>https://dilshankelsen.com/conditional-importing-in-javascript/</link><guid isPermaLink="false">https://dilshankelsen.com/conditional-importing-in-javascript/</guid><pubDate>Thu, 05 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;To conditionally import a module or whatnot, you got to make use of &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#dynamic_imports&quot; target=&quot;_blank&quot;&gt;dynamic imports&lt;/a&gt;. As an example:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// In async function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; module &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;./someModule&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Pay attention to the &lt;code&gt;import&lt;/code&gt; keyword. If &lt;strong&gt;used as a function&lt;/strong&gt;, it returns a promise. &lt;/p&gt;&lt;p&gt;Knowing that, conditionally importing is &lt;em&gt;then&lt;/em&gt; a walk in the park. 🌳&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// In async function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; someModule &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;someCondition&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; someModule &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;./someModule&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Create A Custom Key In Firebase Database]]></title><description><![CDATA[In the realtime database of Firebase, whenever you  push()  a new node, a key/id is  automatically generated  for it. However, you can…]]></description><link>https://dilshankelsen.com/create-custom-key-in-firebase-database/</link><guid isPermaLink="false">https://dilshankelsen.com/create-custom-key-in-firebase-database/</guid><pubDate>Mon, 02 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In the realtime database of Firebase, whenever you &lt;code&gt;push()&lt;/code&gt; a new node, a key/id is &lt;em&gt;automatically generated&lt;/em&gt; for it. However, you can easily &lt;strong&gt;create a custom key&lt;/strong&gt; the following way:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;firebase&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;database&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;ref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;some/path/&lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token template-string interpolation&quot;&gt;customKey&lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you&amp;#x27;ve got &lt;em&gt;no data&lt;/em&gt; to add, it&amp;#x27;s important to still &lt;strong&gt;push an empty object&lt;/strong&gt; or else the node with the custom key will not be created.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Call Child Function From Parent Component In React]]></title><description><![CDATA[One way to call a child component's function from its parent is with the  help of the   useRef   hook . Here's  the gist  of it: We pass a…]]></description><link>https://dilshankelsen.com/call-child-function-from-parent-component-in-react/</link><guid isPermaLink="false">https://dilshankelsen.com/call-child-function-from-parent-component-in-react/</guid><pubDate>Sat, 31 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;One way to call a child component&amp;#x27;s function from its parent is with the &lt;strong&gt;help of the&lt;/strong&gt; &lt;a href=&quot;/useref-hook-in-react-simply-explained&quot;&gt;useRef&lt;/a&gt; &lt;strong&gt;hook&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;Here&amp;#x27;s &lt;strong&gt;the gist&lt;/strong&gt; of it: We pass a Ref (e.g. &lt;code&gt;childFunc&lt;/code&gt;) to a child 🧒 component and &lt;em&gt;assign the desired function&lt;/em&gt; to the Ref&amp;#x27;s &lt;code&gt;current&lt;/code&gt; key. This then gives you access to that very function in the parent component.&lt;/p&gt;&lt;p&gt;Take a look at this &lt;strong&gt;interactive example&lt;/strong&gt;:&lt;/p&gt;&lt;div code=&quot;const Parent = () =&amp;gt; {
  const childFunc = React.useRef(null)

  return (
    &amp;lt;&amp;gt;
      &amp;lt;Child childFunc={childFunc} /&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; childFunc.current()}&amp;gt;Click me&amp;lt;/button&amp;gt;
    &amp;lt;/&amp;gt;
  )
}

const Child = ({ childFunc }) =&amp;gt; {
  React.useEffect(() =&amp;gt; {
    childFunc.current = alertUser
  }, [])

  function alertUser() {
    alert(&amp;#x27;You clicked!&amp;#x27;)
  }

  return null
}

render(&amp;lt;Parent /&amp;gt;)&quot; language=&quot;jsx&quot; class=&quot;live-highlight&quot; style=&quot;position:relative;text-align:left;box-sizing:border-box;padding:0;overflow:hidden;white-space:pre;font-family:monospace&quot;&gt;&lt;textarea style=&quot;margin:0;border:0;background:none;box-sizing:inherit;display:inherit;font-family:inherit;font-size:inherit;font-style:inherit;font-variant-ligatures:inherit;font-weight:inherit;letter-spacing:inherit;line-height:inherit;tab-size:inherit;text-indent:inherit;text-rendering:inherit;text-transform:inherit;white-space:pre-wrap;word-break:keep-all;overflow-wrap:break-word;position:absolute;top:0;left:0;height:100%;width:100%;resize:none;color:inherit;overflow:hidden;-moz-osx-font-smoothing:grayscale;-webkit-font-smoothing:antialiased;-webkit-text-fill-color:transparent;padding-top:10px;padding-right:10px;padding-bottom:10px;padding-left:10px&quot; class=&quot;npm__react-simple-code-editor__textarea&quot; autoCapitalize=&quot;off&quot; autoComplete=&quot;off&quot; autoCorrect=&quot;off&quot; spellcheck=&quot;false&quot; data-gramm=&quot;false&quot;&gt;const Parent = () =&amp;gt; {
  const childFunc = React.useRef(null)

  return (
    &amp;lt;&amp;gt;
      &amp;lt;Child childFunc={childFunc} /&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; childFunc.current()}&amp;gt;Click me&amp;lt;/button&amp;gt;
    &amp;lt;/&amp;gt;
  )
}

const Child = ({ childFunc }) =&amp;gt; {
  React.useEffect(() =&amp;gt; {
    childFunc.current = alertUser
  }, [])

  function alertUser() {
    alert(&amp;#x27;You clicked!&amp;#x27;)
  }

  return null
}

render(&amp;lt;Parent /&amp;gt;)&lt;/textarea&gt;&lt;pre aria-hidden=&quot;true&quot; style=&quot;margin:0;border:0;background:none;box-sizing:inherit;display:inherit;font-family:inherit;font-size:inherit;font-style:inherit;font-variant-ligatures:inherit;font-weight:inherit;letter-spacing:inherit;line-height:inherit;tab-size:inherit;text-indent:inherit;text-rendering:inherit;text-transform:inherit;white-space:pre-wrap;word-break:keep-all;overflow-wrap:break-word;position:relative;pointer-events:none;padding-top:10px;padding-right:10px;padding-bottom:10px;padding-left:10px&quot;&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token keyword&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function maybe-class-name&quot;&gt;Parent&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; childFunc &lt;/span&gt;&lt;span class=&quot;token operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;useRef&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword null nil&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot; style=&quot;display:inline-block&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;Child&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot; style=&quot;color:hsl(75, 70%, 60%)&quot;&gt;childFunc&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;childFunc&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot; style=&quot;color:hsl(75, 70%, 60%)&quot;&gt;onClick&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript arrow operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt; childFunc&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript method function property-access&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Click me&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot; style=&quot;display:inline-block&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function maybe-class-name&quot;&gt;Child&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot; style=&quot;opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; childFunc &lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot; style=&quot;opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;useEffect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    childFunc&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; alertUser&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot; style=&quot;display:inline-block&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;alertUser&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;&amp;#x27;You clicked!&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot; style=&quot;display:inline-block&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword null nil&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot; style=&quot;display:inline-block&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;Parent&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;style type=&quot;text/css&quot;&gt;
/**
 * Reset the text fill color so that placeholder is visible
 */
.npm__react-simple-code-editor__textarea:empty {
  -webkit-text-fill-color: inherit !important;
}

/**
 * Hack to apply on some CSS on IE10 and IE11
 */
@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
  /**
    * IE doesn&apos;t support &apos;-webkit-text-fill-color&apos;
    * So we use &apos;color: transparent&apos; to make the text transparent on IE
    * Unlike other browsers, it doesn&apos;t affect caret color in IE
    */
  .npm__react-simple-code-editor__textarea {
    color: transparent !important;
  }

  .npm__react-simple-code-editor__textarea::selection {
    background-color: #accef7 !important;
    color: transparent !important;
  }
}
&lt;/style&gt;&lt;/div&gt;&lt;div class=&quot;live-preview&quot;&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Pay attention&lt;/strong&gt; to how &lt;code&gt;childFunc&lt;/code&gt; is passed down: a &lt;em&gt;custom props&lt;/em&gt; is used instead of &lt;code&gt;ref&lt;/code&gt; as the latter would otherwise return a DOM element.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Check If User Is Offline Using JavaScript]]></title><description><![CDATA[The browser provides  an API  that allows us to check its connection  to a network :  navigator.onLine . Be aware that I said to a network…]]></description><link>https://dilshankelsen.com/check-if-user-offline-using-javascript/</link><guid isPermaLink="false">https://dilshankelsen.com/check-if-user-offline-using-javascript/</guid><pubDate>Thu, 29 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The browser provides &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine&quot; target=&quot;_blank&quot;&gt;an API&lt;/a&gt; that allows us to check its connection &lt;em&gt;to a network&lt;/em&gt;: &lt;code&gt;navigator.onLine&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;navigator&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;onLine&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Online&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Offline&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Be aware that I said to a network, &lt;strong&gt;not the internet&lt;/strong&gt;! 🚨 To avoid a false positive, I therefore recommend adding &lt;em&gt;a second check&lt;/em&gt;:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;hasInternet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;https://google.com&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;catch&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This second check is done by carrying out a &lt;strong&gt;simple GET request&lt;/strong&gt; to any website. In our example it&amp;#x27;s &lt;code&gt;google.com&lt;/code&gt;, but in general the response should contain as little data as possible. &lt;/p&gt;&lt;p&gt;If we are unable ❌ to reach the site, the request &lt;em&gt;throws an error&lt;/em&gt;, which means &lt;strong&gt;we&amp;#x27;re not connected&lt;/strong&gt; to the internet.&lt;/p&gt;&lt;p&gt;Lastly, let&amp;#x27;s integrate our check into the IF statement:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Inside async function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;navigator&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;onLine&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;hasInternet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Online&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Offline&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Changing Array Values Using ForEach Loop]]></title><description><![CDATA[Take this array as an example, and let's add  1  to each element. The  forEach  method makes this really easy. The trick is to use a  3 rd…]]></description><link>https://dilshankelsen.com/changing-array-values-using-foreach-loop/</link><guid isPermaLink="false">https://dilshankelsen.com/changing-array-values-using-foreach-loop/</guid><pubDate>Mon, 26 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Take this array as an example,&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; numbers &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and let&amp;#x27;s add &lt;code&gt;1&lt;/code&gt; to each element. The &lt;code&gt;forEach&lt;/code&gt; method makes this really easy.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; index&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; array&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  array&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; number &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Array(5) [ 2, 3, 4, 5, 6 ]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The trick is to use a &lt;strong&gt;3&lt;sup&gt;rd&lt;/sup&gt; argument&lt;/strong&gt; in the callback function that gives you access to the array. Be aware that this operation &lt;em&gt;mutates&lt;/em&gt; the original array.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Automatically Refreshing Google Sheets Formulas]]></title><description><![CDATA[To automatically refresh your Google Sheets and any included formulas, open  File > Spreadsheet settings . From there, select the…]]></description><link>https://dilshankelsen.com/automatically-refreshing-google-sheets-formulas/</link><guid isPermaLink="false">https://dilshankelsen.com/automatically-refreshing-google-sheets-formulas/</guid><pubDate>Sat, 24 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;To automatically refresh your Google Sheets and any included formulas, open &lt;strong&gt;File &amp;gt; Spreadsheet settings&lt;/strong&gt;. From there, select the &lt;em&gt;Calculation&lt;/em&gt; tab.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:496px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:64%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAAAsTAAALEwEAmpwYAAABYUlEQVQoz5VSvU7DMBDOE7LyDgyMvAEjD8GIEBITAwywFYkNJEoVWoXmt3Ec27Fjp/VfL0SUpUHlG06nu/vuvjs7oJTFcVL2QHmJygoXZZlkOePce+/8fjjnjDGBlLJcoSIvuGhpGlfzMA9nefjRFimU+HFYawPjLZFskURYcZtF9uuTTt9EOPXxwnXK/4mg6zohxKDQjevcT95sNlVV1TVRSg3LgCWELuMYghXGCNIYQ4RQWtc1xjX4oLknQzXQWNNI9SsSIpxzqHDj6Mlaa1BujPX/R7Ber7MsRxWmjMHy9rslTIYnBJ2gCCQwBg/HG/AF56JH27YwvJcNnuCiYY2U7bAMIWQZJ2maJWDSFCxCSP1ASgUjezIIhtWGa4N2d/C5ezJ8EjgnNC4xy2utjdvldodxIy2Dw6b4lcDamqfo5ejq9OTu/Pjm7GJyeSi56YQ2OqLZ9fvD/XxyO3t8Tl63VBjuMDHXygsAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Highlighted Calculation Tab&quot; title=&quot;Highlighted Calculation Tab&quot; src=&quot;/static/5d7a4f87b0933cf9277e36da2a637dd0/bb630/image-1.png&quot; srcSet=&quot;/static/5d7a4f87b0933cf9277e36da2a637dd0/772e8/image-1.png 200w,/static/5d7a4f87b0933cf9277e36da2a637dd0/e17e5/image-1.png 400w,/static/5d7a4f87b0933cf9277e36da2a637dd0/bb630/image-1.png 496w&quot; sizes=&quot;(max-width: 496px) 100vw, 496px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;Once selected, you will then see a &lt;em&gt;Recalculation&lt;/em&gt; option which determines how often your formulas are refreshed.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:498px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:61%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAACXBIWXMAAAsTAAALEwEAmpwYAAABU0lEQVQoz5WRu0/DMBDG/X8jRibGsrGwIAZmJgYGJAaEBFKFREmbVEnsksav+JEXscMlmYqg0J+ST5eTL767DxljGGNFoeCR8CrFheCc103T/44bQUVRxHGCCaGUfWQZISRN0jTF1tr9xd57BFHnnNLadQ5iadUax7lkHj58vx9Ut622tqrr8bBvfFd+tnXXut71f4HKJEqen5L5C18t+PLdhEFfCCYk9C+l5FwwxhnnsBdQyth2u81zCvlhZi+oXa9kFKgo4MGbDgNvVKEH/Ij7jp8GHm5u2rYErAXpDwRVVUUp1dpA+fjLIauUBhfAt1F1ocyoWkJKG9DJCwQFTdP63cWCZynGZLPBOE0zGWcmJCrcqCWRAZYrInRpYbto6n4a7+C2f8z6Xbquo0aAk9evtyf357PHK9AlT9B/boDRcisguJzfHN3NTh8uju/OFjT6Al/usVtjbuIEAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Recalculation Option&quot; title=&quot;Recalculation Option&quot; src=&quot;/static/e11fea7d5ee59cb3640e90a039bf277c/79e1b/image-2.png&quot; srcSet=&quot;/static/e11fea7d5ee59cb3640e90a039bf277c/772e8/image-2.png 200w,/static/e11fea7d5ee59cb3640e90a039bf277c/e17e5/image-2.png 400w,/static/e11fea7d5ee59cb3640e90a039bf277c/79e1b/image-2.png 498w&quot; sizes=&quot;(max-width: 498px) 100vw, 498px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;You can currently &lt;em&gt;change it to&lt;/em&gt;:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;On change and every minute, or&lt;/li&gt;&lt;li&gt;On change and every hour&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Both will trigger an update &lt;strong&gt;even if nobody is viewing the sheet&lt;/strong&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[4 Tools To Debug Cumulative Layout Shift On Your Website]]></title><description><![CDATA[Let me guess, you've analysed your website's performance and you received a bad score for  Cumulative Layout Shift  (CLS). This means that…]]></description><link>https://dilshankelsen.com/4-tools-to-debug-cumulative-layout-shift/</link><guid isPermaLink="false">https://dilshankelsen.com/4-tools-to-debug-cumulative-layout-shift/</guid><pubDate>Thu, 22 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Let me guess, you&amp;#x27;ve analysed your website&amp;#x27;s performance and you received a bad score for &lt;em&gt;Cumulative Layout Shift&lt;/em&gt; (CLS). This means that during the initial page load, 1 or more HTML elements have visibly &lt;strong&gt;moved around more than the tolerated amount&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;If you wish to read more about CLS and how it&amp;#x27;s calculated, I recommend &lt;a href=&quot;https://web.dev/cls/&quot; target=&quot;_blank&quot;&gt;this article&lt;/a&gt;. For now, let me show you &lt;strong&gt;a few tools&lt;/strong&gt; 🛠 you can use to identify the culprit(s)!&lt;/p&gt;&lt;div&gt;&lt;p&gt;The suggested tools are only available in a Chromium-based browser.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;lighthouse&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#lighthouse&quot; aria-label=&quot;lighthouse permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Lighthouse&lt;/h2&gt;&lt;p&gt;To start off, Lighthouse gives some great pointers as to which elements are causing trouble. In your browser, &lt;a href=&quot;https://developer.chrome.com/docs/devtools/open/&quot; target=&quot;_blank&quot;&gt;open DevTools&lt;/a&gt;. Then select the &lt;em&gt;Lighthouse&lt;/em&gt;  tab and generate a report with &lt;em&gt;Performance&lt;/em&gt; selected.&lt;/p&gt;&lt;p&gt;Once done, under &lt;em&gt;Diagnostics&lt;/em&gt;, there should be a dropdown called &lt;strong&gt;Avoid large layout shifts&lt;/strong&gt; that lays out (&lt;em&gt;pun intended&lt;/em&gt; 😆) the issues.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:356px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:100.49999999999999%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAACXBIWXMAAAsTAAALEwEAmpwYAAACeElEQVQ4y5VUz2sTQRSev6rgX6AU8aB4sQcPehBEa0XxoPUg9NK0qT00SW0KbSQEtdikQWySkhClYFERb3pRsWmSZn/M7uzO7szu7MS3u1WJpSF+PGYeM/u99+abN4scx+lFUBTFsizbtgkh4HieJ4SQUvZPAGyhIAiATyl1IoDjRgAfAsFu/2QgCO/7fv8/ARTOOYKp0+lgrOlY1TTV870hpcaAcqDAkNzpdJ+sLNWqpZ1qOZNe/LnfYRFdBDIIpIxGqB1G3w/N8/8GR63WQfLx7KOH07nVlcTcTOtAwVZfMzzN9BUMo4eJrxq+aQnqCIsKYgsIFOWXyDTJ+lp2bn4mX1jbeFHA2GBcuA5nzGfMY4yD6rAiBCgnYwsRViWRriulzcJuvbSRX2I2ZQRTrFBDcYgOI/icuR6cD6INAsRCrf1vzXrl49vGvWsXqUM581z3zwc8MuYO0iCSaZrQC0hVDms75d0324W1Re5xFqc4nmgQ0AWh2hAgt55dXc1svnz2/sOehDVKmG1yanHHCh3Hik/4D0K1saFdOD9+amwsu5yqVEsSlBE+mAyE/O0cv2pIAf0Lgml371y/euVyNp1sNisjdlgQAdm2Vau+qje2y1vPa7WKTWms5HDEbwZRmzQaW6WtfLn0dGFh9vuPVk+1expVsaPqFBxFd2zq8kG1QSnQDMFjvH1r6tz42dNnxicuTbTbbRDHdd3hah+RYSoWi5nl5Uwms57LaYoSvpdIjyGAysMzg2DJZHJ+PplIJFKpNMF6nzE5kmASdbvdKah7avLm5I0H0/cNw4j/EsPJR1d12D18XS1+/rr36cu75m6VmGQUcoxfXIhMVdGEVQsAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Lighthouse report for CLS&quot; title=&quot;Lighthouse report for CLS&quot; src=&quot;/static/10e5ea723131844a3dd6ab92b8db3199/50ac3/image-1.png&quot; srcSet=&quot;/static/10e5ea723131844a3dd6ab92b8db3199/772e8/image-1.png 200w,/static/10e5ea723131844a3dd6ab92b8db3199/50ac3/image-1.png 356w&quot; sizes=&quot;(max-width: 356px) 100vw, 356px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Lighthouse report for CLS&lt;/figcaption&gt;&lt;p&gt;I recommend using this over a third-party tool like &lt;em&gt;GTmetrix&lt;/em&gt; solely for the reason that &lt;strong&gt;you can test your localhost&lt;/strong&gt; as you try to improve your CLS score.&lt;/p&gt;&lt;h2 id=&quot;visual-feedback&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#visual-feedback&quot; aria-label=&quot;visual feedback permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Visual feedback&lt;/h2&gt;&lt;p&gt;Another neat way to debug CLS is &lt;strong&gt;through visual feedback&lt;/strong&gt; 👀. You&amp;#x27;ve got 2 options: either see it &lt;em&gt;as it&amp;#x27;s happening&lt;/em&gt; or get a historical view of it.&lt;/p&gt;&lt;h3 id=&quot;live&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#live&quot; aria-label=&quot;live permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Live&lt;/h3&gt;&lt;p&gt;In DevTools, click on the 3 dots in the top right corner. Go to &lt;strong&gt;More tools &amp;gt; Rendering&lt;/strong&gt; and then select the &lt;em&gt;Layout Shift Regions&lt;/em&gt; option in the bottom window.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:553px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:20.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAbklEQVQI14WOyQ6AIBBD+f//ZDEhCIJsAlo1we3gy6SXaadDpJRcCGut0hMGhA8xxuVJKQURMkjDGDVm8vPsA2THOYddV1jXG601KGxk1JoyrpQ6rSklWGut6x9XWIgBHfjQH+BEzrkrwKuvMDo2BPLoEvd3HgwAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Rendering options in DevTools&quot; title=&quot;Rendering options in DevTools&quot; src=&quot;/static/55605a9d19fbe04259aa26b3d7264ce6/74cfa/image-2.png&quot; srcSet=&quot;/static/55605a9d19fbe04259aa26b3d7264ce6/772e8/image-2.png 200w,/static/55605a9d19fbe04259aa26b3d7264ce6/e17e5/image-2.png 400w,/static/55605a9d19fbe04259aa26b3d7264ce6/74cfa/image-2.png 553w&quot; sizes=&quot;(max-width: 553px) 100vw, 553px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Rendering options in DevTools&lt;/figcaption&gt;&lt;p&gt;Refresh your page and see the magic 💁‍♂️ happen.&lt;/p&gt;&lt;h3 id=&quot;history&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#history&quot; aria-label=&quot;history permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;History&lt;/h3&gt;&lt;p&gt;In the &lt;em&gt;Performance&lt;/em&gt; tab, you&amp;#x27;ll first need to &lt;strong&gt;record the performance&lt;/strong&gt; of the page load. Click on the round grey circle ⃝ in the top left corner, refresh the page and then press &lt;em&gt;Stop&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;You end up seeing a bunch of strips and I want you to pay attention to the one name &lt;em&gt;Experience&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:490px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:49%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAABMUlEQVQoz52Sy07EMAxF8/+fg8SOHUsk1ixggBn6bvNo82yTXpwZCgwrZizd2HGiE8sO2+/3WJaIdQVSooWsEi1uH+9wc/+Ap8ofc5HOEl3Kd9I0QvIJo3KYw4rgE4JdEJcExoXCZOeTTIDxEXVZYfdxwHvN0UoP6xZoOjduJh+gn1/gKHYhHnMmey4J7MF2hwptU0MpASk4pFLgXMC7gBgjQqBHrDtXIIi1X9ryp5jx1zcUBYcYLIbeYOx66K4ldRibBhPFaZqwav1Lf/c/YrJpUVY9VTVCjZZ64UjZG3hzUppnrP8UG+k1YzSkFDQcStIAcvOvNaapzGEY0Pc9QeW3z7YS+FIdgRlSliUa6llRFMi5DXhVhRmYYRvQe39W4UVAQ03nnKOjqW7g/FWuBX4CZd8KIVrygjsAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Summary of a layout shift&quot; title=&quot;Summary of a layout shift&quot; src=&quot;/static/5bab19341f50dcca8c3ffe265cd2ff1b/41d3c/image-3.png&quot; srcSet=&quot;/static/5bab19341f50dcca8c3ffe265cd2ff1b/772e8/image-3.png 200w,/static/5bab19341f50dcca8c3ffe265cd2ff1b/e17e5/image-3.png 400w,/static/5bab19341f50dcca8c3ffe265cd2ff1b/41d3c/image-3.png 490w&quot; sizes=&quot;(max-width: 490px) 100vw, 490px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Summary of a layout shift&lt;/figcaption&gt;&lt;p&gt;Click on a &lt;strong&gt;red block&lt;/strong&gt;, which represents a layout shift that happened.&lt;/p&gt;&lt;p&gt;You should then &lt;strong&gt;see more details&lt;/strong&gt; about the shift in the &lt;em&gt;Summary&lt;/em&gt; window below. If you hover over the &lt;em&gt;Location&lt;/em&gt; coordinates, you will see red overlays appear on your site.&lt;/p&gt;&lt;h2 id=&quot;layout-instability-api&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#layout-instability-api&quot; aria-label=&quot;layout instability api permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Layout Instability API&lt;/h2&gt;&lt;p&gt;For a detailed examination 🧐 of the shifted elements, I recommend using the &lt;em&gt;Layout Instability API&lt;/em&gt;. The easiest way to do so is through the &lt;a href=&quot;https://chrome.google.com/webstore/detail/web-vitals/ahfhijdlegdabablpippeagghigmibma&quot; target=&quot;_blank&quot;&gt;Web Vitals extension&lt;/a&gt;. After you&amp;#x27;ve installed it, enable logging under &lt;strong&gt;Options &amp;gt; Console Logging&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;Next, open your browser&amp;#x27;s console and refresh the page. You should then see something similar to below. &lt;strong&gt;Look out for the word CLS&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:541px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:17%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAADCAIAAAAcOLh5AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAb0lEQVQI142KOxbCMAzAcv9DlgVa0k+cOI6xjWkozB2qp0GDAtaWIsCC8YnzJAWMSIg4g8GmtWprxMxq5iJeq7cmKVHO7h7cPnjPyxDjMG23B62rqP5veQmrytHH188Irl7GMo8I8Fbpl9j3n71/AYqArgsuwJmoAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Log from Web Vitals extension&quot; title=&quot;Log from Web Vitals extension&quot; src=&quot;/static/db2bfc46a478708a88ce50178c057fc7/9d576/image-4.png&quot; srcSet=&quot;/static/db2bfc46a478708a88ce50178c057fc7/772e8/image-4.png 200w,/static/db2bfc46a478708a88ce50178c057fc7/e17e5/image-4.png 400w,/static/db2bfc46a478708a88ce50178c057fc7/9d576/image-4.png 541w&quot; sizes=&quot;(max-width: 541px) 100vw, 541px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Log from Web Vitals extension&lt;/figcaption&gt;&lt;p&gt;Dig a bit deeper in the &lt;em&gt;log tree&lt;/em&gt; and you will come across the following properties of the element:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;json&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-json&quot;&gt;JSON&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-json&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;previousRect&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;bottom&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;46&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;height&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;left&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;373&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;right&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;491&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;top&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;width&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;118&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;x&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;373&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;y&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;currentRect&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;bottom&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;46&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;height&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;left&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;358&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;right&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;476&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;top&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;width&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;118&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;x&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;358&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;y&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here are some things you should know about:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; correspond to the x and y coordinates of the elements &lt;strong&gt;top-left corner&lt;/strong&gt;.&lt;/li&gt;&lt;li&gt;Similarly, &lt;code&gt;top&lt;/code&gt;, &lt;code&gt;left&lt;/code&gt;, &lt;code&gt;bottom&lt;/code&gt; and &lt;code&gt;right&lt;/code&gt; use the top-left corner as their &lt;em&gt;point of origin&lt;/em&gt;. For instance, &lt;code&gt;bottom&lt;/code&gt; is calculated by adding &lt;code&gt;y&lt;/code&gt; and &lt;code&gt;height&lt;/code&gt;.&lt;/li&gt;&lt;li&gt;If &lt;code&gt;previousRect&lt;/code&gt; contains only &lt;code&gt;0&lt;/code&gt;, this means that the &lt;strong&gt;element has shifted into view&lt;/strong&gt;. If that is the case for &lt;code&gt;currentRect&lt;/code&gt;, the &lt;strong&gt;element has shifted out of view&lt;/strong&gt;.&lt;/li&gt;&lt;/ul&gt;</content:encoded></item><item><title><![CDATA[Get Width And Height Of HTML Element Using JavaScript]]></title><description><![CDATA[There are a few ways you can retrieve the width and height of an HTML element on your page. However, it's  important  to know  what these…]]></description><link>https://dilshankelsen.com/get-width-height-of-html-element-using-javascript/</link><guid isPermaLink="false">https://dilshankelsen.com/get-width-height-of-html-element-using-javascript/</guid><pubDate>Mon, 19 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;There are a few ways you can retrieve the width and height of an HTML element on your page. However, it&amp;#x27;s &lt;em&gt;important&lt;/em&gt; to know &lt;strong&gt;what these measurements are made up of&lt;/strong&gt;: the content, padding, border and margin.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:365px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:38.99999999999999%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAIAAAB2/0i6AAAACXBIWXMAAAsTAAALEwEAmpwYAAABVklEQVQY013Ra0/CMBQG4P3/P2Ki34whISZG5SKMDcoGu7bbsIyL6wa7yE3WYwdCiMnzoX3fNjnJkdYZzgvveFwCMIDlyZdQlovdPgSIrsmlFaKSzzkUEvHb9ed7pL3YTtcPNEwGxEOCafXe3mpEXAk6J38IcrG6PbgAmRSGeu2p8XBXqz/K1mCN9QJreWDu8KjovmNvvPXGmyo8cbUssA6dhr1ODYBvSR83lLav6kwmMaI5jsGNoefGHXPRsZYKSTRauAk4rHQYtxmnFOSm6QVtEGMbVmukzhcRBCsexOBF4Meg+3nfSYYk1UhuTnci8ViFRHwxA+XDmdBO9fmTIrkVWGhlKMxQhViwh2ujz+QGtoep2U/O4QlztKz5Okozsxq72NgT2qchujWdIeL3mu0aDQf/KjoVBvsfh0Mq8WoZyUl8OVQ4Z4eD2Mrqpr0+WJV8yWHzC8B7rCP7rbU+AAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Parts of an HTML element&quot; title=&quot;Parts of an HTML element&quot; src=&quot;/static/3ca130024220045276bbee8881aaf93f/2e8d1/image-1.png&quot; srcSet=&quot;/static/3ca130024220045276bbee8881aaf93f/772e8/image-1.png 200w,/static/3ca130024220045276bbee8881aaf93f/2e8d1/image-1.png 365w&quot; sizes=&quot;(max-width: 365px) 100vw, 365px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Box model in HTML&lt;/figcaption&gt;&lt;p&gt;In all the following methods, the &lt;strong&gt;margin is always excluded&lt;/strong&gt; from and the &lt;em&gt;content is always included&lt;/em&gt; in the height and width calculations.&lt;/p&gt;&lt;h2 id=&quot;method-1&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#method-1&quot; aria-label=&quot;method 1 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Method 1&lt;/h2&gt;&lt;p&gt;The &lt;code&gt;offsetHeight&lt;/code&gt; and &lt;code&gt;offsetWidth&lt;/code&gt; &lt;em&gt;read-only&lt;/em&gt; properties return a value that &lt;strong&gt;includes the border, padding and scroll bar&lt;/strong&gt;. If the element is hidden, then both properties will return &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; htmlElement &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;some-id&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;htmlElement&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;offsetHeight&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;htmlElement&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;offsetWidth&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:411px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:60%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAACXBIWXMAAAsTAAALEwEAmpwYAAACrElEQVQoz0WSS0wTURSGuyExrFgIKyVoNLFQo0tUIkILKhEjRlvEFmGDJu6FtEADCxATfISIJj7A6EJZGCOJMSIStWBpKy0dOk/aEmfaTpnObTt92dbO9baGePLnJPf898s5N/coPJ4lSbLlcu5MhsznyUKBymZpnv/B0B/8voUA9xVCJp0mZZnK5cl0Gk+lnfy2heX8EEJFOGyVpKWJiZuDg91Go95oNJhMhpGRGyZTn9l8fWCg918RaXjo2uBQz8d3kzDyhXc5/oCkQhTtBDFXVlZWX39CozmtVrc2NDS2tbUbDD0Gfbf2sq6xUdPU1KJubj15qkWh2NXbcw5CN03gFB5TAODA8ddVVZU8z6NJZFlGmWV/EQTBMAzHcehYKBSLgQQ82qTr1msgpJDldO3AFRUVwWAwEAhYrVaGpkmCpmkGwzCaoh12+/q6O5fLIf7ipas6bTOEJIqNDek/jJps+X2W5ZWfDgfmxtbW1lZXbS7nute7yXJcNotgWau9oi3BNE3h+A5cWblbEAQQEQic8Hp9HMv6/H6CwNH8AIB8Pg9L0dVl6NSpISRQZ48HwaIdJ96Ul5dPTj4YH7vdf6t/2Dw6NfVoevrxvbv3nz55NmIeHRu/Mzv7cmbmRa3yiN5wFr2ZZGg3huCog/XPHz92WKmsrqurUan2qVT7289rtNr2jo4znZ0X1M31h5R7a2urVaqaAwf3jA/1wd+rHObMhCUFtflZ8r+PCYuJ1HIqbUmlvicSFpfz1cKnKevK88XFhx7PXCZjLVnLcembxMyHyLcBeqO4JLEEABQR49l4lI+LISQAwiAqSJIIgBCPRwDYjoOSBfhYJCh5qQjKyWQRLv5tIl0IRgo8KIREKETZraTNJjqdoCTRjcXy2zE5JBYvBCNyGMDSPiD9BRIzCQdT2w23AAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Offset property&quot; title=&quot;Offset property&quot; src=&quot;/static/1ae926c6d97381a7072f568ee24b3873/2a432/image-2.png&quot; srcSet=&quot;/static/1ae926c6d97381a7072f568ee24b3873/772e8/image-2.png 200w,/static/1ae926c6d97381a7072f568ee24b3873/e17e5/image-2.png 400w,/static/1ae926c6d97381a7072f568ee24b3873/2a432/image-2.png 411w&quot; sizes=&quot;(max-width: 411px) 100vw, 411px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Sourced from &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetWidth#example&quot; target=&quot;_blank&quot;&gt;MDN Web Docs&lt;/a&gt;&lt;/figcaption&gt;&lt;h2 id=&quot;method-2&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#method-2&quot; aria-label=&quot;method 2 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Method 2&lt;/h2&gt;&lt;p&gt;Similarly, &lt;code&gt;clientHeight&lt;/code&gt; and &lt;code&gt;clientWidth&lt;/code&gt; are &lt;em&gt;read-only&lt;/em&gt; properties. However, the returned value &lt;strong&gt;solely includes the padding&lt;/strong&gt;. Take notice that for inline elements, these properties return &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;htmlElement&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;clientHeight&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;htmlElement&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;clientWidth&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:411px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:60%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAACXBIWXMAAAsTAAALEwEAmpwYAAACrElEQVQoz02R3UtTcRjHR+SNEXiRXpWYEBmuoOhilDDdpoZgmNS2zGlBVBB0mbKJS6FMLzKYWSBlYIH4B5gtRHKp061t7u28uS13zrazt9/OzjbHdtz5daaBPTz84Hm+fJ63n8jjWWHZrWLRmc+jHIeWSlihgNP0BoEvBvw/QtRPCIm9PZTnsSKH5veQXNYWCa+SQT+EUBSNmll2ZXz86dBQn1bbq9VqdDrNyMgTne6RXv94cPDBYVLw4eF+3VD/d+MUhLZIJFCGk0kLgixUVFRIJNcVina5vK2pSdrR0anR3Nf09invqKRSRUtLq1zWJm1uPX6s8qGqHTImfNOCYRkRAFavd76mppqmaaEYz/PCS5JBBEEIgqBClBCWSuVkiIWXZer+HgWEXhxD7XbmH1xVVRUOh0OhkNlsJnAcRTAcJ1wuN47hVotle9tZLBYFvvv2PZVKBiGGYajbzR7BFEX9Cfh/ra3/tlrdTpfNZtvc3HLYHT7fDkmRhYIA80rlXaVSgFEcx47g6upTsXgcJOKIF/H5/BRJ+gMBBPEK8wMAOI6DB9bTo1Gr5RAiQmcULe9s8XjnT1aemJo0vHr5euD5wLB+1GB4Pz39YfLN25mZjy/0o2NjE7Of577Ozl0VX+nrboHsms/mIIhsuXOQWmyWXJKcq20Q110UnxWL6ztvKpTKzltdN9TqLrlMcqHhzPnG2msNdY31p8cmnkHoISm/cAQRhhlz+VUmamR3v+UK67msKZMxOexfjEaDef3T8vI7j2chn98QpCy1lKaNmZyJjixR1E75n1OpOJtNMDQJUG+aiaUT4RSIppgEyyYBiKfTCQBiaUALUorAUqFdNpdMxMMsy5bhw0vwHLdPxUo0gDEQCWZdbsZuBweedLpSXJThI0lB5ff34X/2Fy5wCokzeTOYAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Client property&quot; title=&quot;Client property&quot; src=&quot;/static/f840ae3614c1fa331ae322ef337bc722/2a432/image-3.png&quot; srcSet=&quot;/static/f840ae3614c1fa331ae322ef337bc722/772e8/image-3.png 200w,/static/f840ae3614c1fa331ae322ef337bc722/e17e5/image-3.png 400w,/static/f840ae3614c1fa331ae322ef337bc722/2a432/image-3.png 411w&quot; sizes=&quot;(max-width: 411px) 100vw, 411px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Sourced from &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/Element/clientWidth#example&quot; target=&quot;_blank&quot;&gt;MDN Web Docs&lt;/a&gt;&lt;/figcaption&gt;&lt;h2 id=&quot;method-3&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#method-3&quot; aria-label=&quot;method 3 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Method 3&lt;/h2&gt;&lt;p&gt;Another way to measure an element&amp;#x27;s dimensions is using &lt;code&gt;getBoundingClientRect()&lt;/code&gt;. It&amp;#x27;s main advantage over the other 2 methods is that it &lt;strong&gt;gives you fractional values&lt;/strong&gt; instead of rounding them to the nearest integer.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; elementDimensions &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; htmlElement&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getBoundingClientRect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;elementDimensions&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;elementDimensions&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;width&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It returns an object of type &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/DOMRect&quot; target=&quot;_blank&quot;&gt;DOMRect&lt;/a&gt; that contains the height and width of the element. And like &lt;code&gt;offset...&lt;/code&gt;, &lt;strong&gt;the border, padding and scroll bar are included&lt;/strong&gt; in the measurements.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[UseRef Hook In React Simply Explained]]></title><description><![CDATA[The  useRef  hook returns nothing more than a  JavaScript object  with a  current  key. And just like any other object, we can  assign a new…]]></description><link>https://dilshankelsen.com/useref-hook-in-react-simply-explained/</link><guid isPermaLink="false">https://dilshankelsen.com/useref-hook-in-react-simply-explained/</guid><pubDate>Sat, 17 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The &lt;code&gt;useRef&lt;/code&gt; hook returns nothing more than a &lt;strong&gt;JavaScript object&lt;/strong&gt; with a &lt;code&gt;current&lt;/code&gt; key. And just like any other object, we can &lt;em&gt;assign a new value&lt;/em&gt; to that key as we please.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; refObject &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useRef&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;initialValue&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useEffect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;refObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;refObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// initialValue&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  refObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;newValue&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;refObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// newValue&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;no-rerenders-with-updates&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#no-rerenders-with-updates&quot; aria-label=&quot;no rerenders with updates permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;No rerenders with updates&lt;/h2&gt;&lt;p&gt;Whenever you assign a new value to &lt;code&gt;current&lt;/code&gt;, your component is &lt;strong&gt;not rerendered&lt;/strong&gt;. Compare this in contrast to &lt;code&gt;useState&lt;/code&gt; where any updates will trigger a rerender of your React component.&lt;/p&gt;&lt;p&gt;The reason for this is that Refs exist &lt;strong&gt;outside of a component&amp;#x27;s rendering cycle&lt;/strong&gt;. This allows you to store and persist data across renders. Consequently, Refs are suited &lt;em&gt;for avoiding&lt;/em&gt; &lt;a href=&quot;/understanding-stale-closures-in-javascript&quot;&gt;stale closures&lt;/a&gt;.&lt;/p&gt;&lt;h2 id=&quot;accessing-html-elements&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#accessing-html-elements&quot; aria-label=&quot;accessing html elements permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Accessing HTML elements&lt;/h2&gt;&lt;p&gt;Another frequent use case of this hook is gaining &lt;strong&gt;access to the DOM&lt;/strong&gt;. This is done by passing in the Ref to a React element, which consequently assigns the DOM node to the &lt;code&gt;current&lt;/code&gt; key.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;useEffect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;refObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;refObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// &amp;lt;div&amp;gt;Some HTML element&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  refObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;red&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;ref&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;refObject&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Some HTML element&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Set Up A Local AWS Lambda Function]]></title><description><![CDATA[This is a quick,  no frills guide  to setting up your own Lambda function  locally . Step 1 - Install the SAM CLI First off, you need the…]]></description><link>https://dilshankelsen.com/set-up-local-aws-lambda-function/</link><guid isPermaLink="false">https://dilshankelsen.com/set-up-local-aws-lambda-function/</guid><pubDate>Thu, 15 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;This is a quick, &lt;strong&gt;no frills guide&lt;/strong&gt; to setting up your own Lambda function &lt;em&gt;locally&lt;/em&gt;.&lt;/p&gt;&lt;h2 id=&quot;step-1---install-the-sam-cli&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-1---install-the-sam-cli&quot; aria-label=&quot;step 1   install the sam cli permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 1 - Install the SAM CLI&lt;/h2&gt;&lt;p&gt;First off, you need the AWS SAM CLI to even get started. &lt;strong&gt;Instructions on how to install&lt;/strong&gt; it for your respective operating system can be found &lt;a href=&quot;https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Grab a drink ☕️ as this might take a while. &lt;/p&gt;&lt;h2 id=&quot;step-2---create-a-new-sam-application&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-2---create-a-new-sam-application&quot; aria-label=&quot;step 2   create a new sam application permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 2 - Create a new SAM application&lt;/h2&gt;&lt;div&gt;&lt;p&gt;Some of you might be wondering, &lt;em&gt;what the hell&lt;/em&gt; is a SAM application? Also called &lt;strong&gt;Serverless Application Model&lt;/strong&gt;, it&amp;#x27;s basically a framework that hosts your Lambda function and any associated functionality (e.g. APIs, events and databases).&lt;/p&gt;&lt;/div&gt;&lt;p&gt;Once the CLI is installed, you can then create a &lt;strong&gt;new project with&lt;/strong&gt; &lt;code&gt;sam init&lt;/code&gt;. You&amp;#x27;ll be guided through the process. And in my case, I&amp;#x27;ve chosen the &lt;em&gt;Hello World&lt;/em&gt; application template with Node.js as a runtime. &lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;-----------------------&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Generating application:&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;-----------------------&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Name: sam-tutorial&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Runtime: nodejs12.x&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Dependency Manager: &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Application Template: hello-world&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Output Directory: &lt;/span&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;step-3---understand-the-project-structure&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-3---understand-the-project-structure&quot; aria-label=&quot;step 3   understand the project structure permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 3 - Understand the project structure&lt;/h2&gt;&lt;p&gt;Open up your project in an IDE of your choice and you&amp;#x27;ll be greeted with the following structure. 🌳 Of course, &lt;strong&gt;file names may slightly differ&lt;/strong&gt; depending on your configuration.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-text&quot;&gt;TEXT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-text&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;sam-tutorial/&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  ├── events/&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  │   └── event.json&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  ├── hello_world/         # Contains the Lambda function&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  │   ├── tests/&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  │   ├── .npmignore&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  │   ├── app.js           # Contains the Lambda handler logic&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  │   └── package.json&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  ├── .gitignore&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  ├── README.md&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  └── template.yaml        # Configuration file of your SAM application&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;step-4---run-the-lambda-function&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-4---run-the-lambda-function&quot; aria-label=&quot;step 4   run the lambda function permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 4 - Run the Lambda function&lt;/h2&gt;&lt;p&gt;In your terminal, change the directory to your SAM application and run &lt;code&gt;sam build&lt;/code&gt;. &lt;strong&gt;Be aware&lt;/strong&gt; that for any changes that you make in the Lambda function, you will need to re-run this command.&lt;/p&gt;&lt;p&gt;Next on the list, Docker! 🐳 Boot up that bad boy. Now, you&amp;#x27;ve got &lt;em&gt;2 options&lt;/em&gt; regarding how to run your function.&lt;/p&gt;&lt;h3 id=&quot;invoke-it-with-an-event&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#invoke-it-with-an-event&quot; aria-label=&quot;invoke it with an event permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Invoke it with an event&lt;/h3&gt;&lt;p&gt;You can invoke the function using an event specified in &lt;code&gt;events/event.json&lt;/code&gt;. To do that, simply &lt;strong&gt;run&lt;/strong&gt; &lt;code&gt;sam local invoke&lt;/code&gt;.&lt;/p&gt;&lt;h3 id=&quot;run-an-api-server&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#run-an-api-server&quot; aria-label=&quot;run an api server permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Run an API server&lt;/h3&gt;&lt;p&gt;Or perhaps you prefer to host your Lambda function with an API server that listens to any incoming calls. For that, &lt;strong&gt;run&lt;/strong&gt; &lt;code&gt;sam local start-api&lt;/code&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Remove Word From String In JavaScript]]></title><description><![CDATA[You've got a string, e.g.  Hello World! , and you want to remove a word from it, e.g.  World! . This job can be done with  replace()  as…]]></description><link>https://dilshankelsen.com/remove-word-from-string-in-javascript/</link><guid isPermaLink="false">https://dilshankelsen.com/remove-word-from-string-in-javascript/</guid><pubDate>Mon, 12 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;You&amp;#x27;ve got a string, e.g. &lt;code&gt;Hello World!&lt;/code&gt;, and you want to remove a word from it, e.g. &lt;code&gt;World!&lt;/code&gt;. This job can be done with &lt;code&gt;replace()&lt;/code&gt; as follows:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; newString &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Hello World!&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27; World!&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;newString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// &amp;#x27;Hello&amp;#x27;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The 1&lt;sup&gt;st&lt;/sup&gt; parameter of this function takes the &lt;strong&gt;value you want to replace&lt;/strong&gt;, and the 2&lt;sup&gt;nd&lt;/sup&gt; parameter the &lt;strong&gt;value you want to replace it with&lt;/strong&gt;. In our case, the latter is an empty string.&lt;/p&gt;&lt;h2 id=&quot;multiple-occurrences&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#multiple-occurrences&quot; aria-label=&quot;multiple occurrences permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Multiple occurrences&lt;/h2&gt;&lt;p&gt;How about removing a string that appears multiple times? Instead of a string value, &lt;code&gt;replace&lt;/code&gt; also &lt;em&gt;accepts regular expressions&lt;/em&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; newString &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Hello World and outer World!&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token regex regex-delimiter&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token regex regex-source language-regex&quot;&gt; World&lt;/span&gt;&lt;span class=&quot;token regex regex-delimiter&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token regex regex-flags&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;newString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// &amp;#x27;Hello and outer!&amp;#x27;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In the regular expression, we can pass in the word as is, including any special characters like a space. The &lt;code&gt;g&lt;/code&gt; &lt;strong&gt;means global search&lt;/strong&gt;, i.e. watch out for multiple occurrences.&lt;/p&gt;&lt;p&gt;What if the words have &lt;em&gt;different cases&lt;/em&gt;? Simply add an &lt;code&gt;i&lt;/code&gt; after &lt;code&gt;g&lt;/code&gt;, and the search won&amp;#x27;t be case-sensitive.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[JavaScript Closures Simply Explained]]></title><description><![CDATA[They key to understanding closures is  scope , because closure is nothing more than the  outer scope  of a function. Let's look at an…]]></description><link>https://dilshankelsen.com/javascript-closures-simply-explained/</link><guid isPermaLink="false">https://dilshankelsen.com/javascript-closures-simply-explained/</guid><pubDate>Sat, 10 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;They key to understanding closures is &lt;a href=&quot;https://www.w3schools.com/js/js_scope.asp&quot; target=&quot;_blank&quot;&gt;scope&lt;/a&gt;, because closure is nothing more than the &lt;strong&gt;outer scope&lt;/strong&gt; of a function.&lt;/p&gt;&lt;p&gt;Let&amp;#x27;s look at an example:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;/* Outer scope */&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; someVariable &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;someValue&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;printVariable&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;/* Inner scope */&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;someVariable&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;printVariable&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// someValue&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The function &lt;code&gt;printVariable&lt;/code&gt; has &lt;em&gt;access to any variables defined outside&lt;/em&gt; of it, e.g. &lt;code&gt;someVariable&lt;/code&gt;. The successful log of this variable proves so.&lt;/p&gt;&lt;h2 id=&quot;nested-functions&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#nested-functions&quot; aria-label=&quot;nested functions permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Nested functions&lt;/h2&gt;&lt;p&gt;Closures are often discussed in the context of nested functions, using the following &lt;strong&gt;typical example&lt;/strong&gt;:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;outerFunction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;outerVariable&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;innerFunction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;innerVariable&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;outerVariable&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; innerVariable&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; anotherFunction &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;outerFunction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;outside&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;anotherFunction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;inside&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// outside, inside&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As you can see, &lt;code&gt;innerFunction&lt;/code&gt; still has &lt;em&gt;access to any variables&lt;/em&gt; from its parent function, even after the parent has been called.&lt;/p&gt;&lt;p&gt;However, whenever you work with closures, you should &lt;strong&gt;be acutely aware&lt;/strong&gt; ⚠️ of the concept of &lt;a href=&quot;/understanding-stale-closures-in-javascript&quot;&gt;stale closures&lt;/a&gt;!&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Difference Between :last-of-type And :last-child]]></title><description><![CDATA[Starting off with the  simpler   :last-of-type  pseudo-class, imagine this example. In plain English, this rule simply means:  select and…]]></description><link>https://dilshankelsen.com/difference-between-last-of-type-and-last-child/</link><guid isPermaLink="false">https://dilshankelsen.com/difference-between-last-of-type-and-last-child/</guid><pubDate>Thu, 08 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Starting off with the &lt;strong&gt;simpler&lt;/strong&gt; &lt;code&gt;:last-of-type&lt;/code&gt; pseudo-class, imagine this example.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token style language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token style language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token style language-css selector&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token style language-css selector pseudo-class&quot;&gt;:last-of-type&lt;/span&gt;&lt;span class=&quot;token style language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token style language-css punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token style language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token style language-css&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token style language-css property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token style language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token style language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token style language-css color&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;token style language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token style language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token style language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token style language-css punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token style language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token style language-css&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;ul&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;&amp;lt;!-- Red --&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;ul&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In plain English, this rule simply means: &lt;em&gt;select and color red the last element of type &lt;code&gt;li&lt;/code&gt; inside a parent element&lt;/em&gt;. You can mix the group with as many other tags as you&amp;#x27;d like and still &lt;strong&gt;get the same result&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;ul&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Bus&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;&amp;lt;!-- Red --&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;ul&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;gotcha&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#gotcha&quot; aria-label=&quot;gotcha permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Gotcha&lt;/h2&gt;&lt;p&gt;&lt;code&gt;:last-child&lt;/code&gt; is not as straightforward and can cause quite some &lt;em&gt;confusion&lt;/em&gt; if not understood properly.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token style language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token style language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token style language-css selector&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token style language-css selector pseudo-class&quot;&gt;:last-child&lt;/span&gt;&lt;span class=&quot;token style language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token style language-css punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token style language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token style language-css&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token style language-css property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token style language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token style language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token style language-css color&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;token style language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token style language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token style language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token style language-css punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token style language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token style language-css&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In plain English, this rule goes a bit differently: &lt;em&gt;select and color red the last element of a parent if it&amp;#x27;s of type &lt;code&gt;li&lt;/code&gt;&lt;/em&gt;. As opposed to &lt;code&gt;:last-of-type&lt;/code&gt;, this pseudo-class actually contains a condition.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;ul&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Bus&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;&amp;lt;!-- Not red --&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;ul&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you were to apply it to our last example, &lt;code&gt;4&lt;/code&gt; would not be colored red. &lt;em&gt;The reason&lt;/em&gt; is that the last element of our &lt;code&gt;ul&lt;/code&gt; parent &lt;strong&gt;is not of type&lt;/strong&gt; &lt;code&gt;li&lt;/code&gt;, but of type &lt;code&gt;p&lt;/code&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Finding SQL Rows With Similar Values]]></title><description><![CDATA[Let me first clarify what I mean by  similar values . Imagine the following  Users  table containing emails and names: Id Email Name 1 jack…]]></description><link>https://dilshankelsen.com/finding-sql-rows-with-similar-values/</link><guid isPermaLink="false">https://dilshankelsen.com/finding-sql-rows-with-similar-values/</guid><pubDate>Mon, 05 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Let me first clarify what I mean by &lt;em&gt;similar values&lt;/em&gt;. Imagine the following &lt;code&gt;Users&lt;/code&gt; table containing emails and names:&lt;/p&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Email&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;mailto:jack@gmail.com&quot;&gt;jack@gmail.com&lt;/a&gt;&lt;/td&gt;&lt;td&gt;Jack&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;mailto:mike@gmail.com&quot;&gt;mike@gmail.com&lt;/a&gt;&lt;/td&gt;&lt;td&gt;Mike&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;mailto:anna@yahoo.com&quot;&gt;anna@yahoo.com&lt;/a&gt;&lt;/td&gt;&lt;td&gt;Anna&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;mailto:stan@qq.com&quot;&gt;stan@qq.com&lt;/a&gt;&lt;/td&gt;&lt;td&gt;Stan&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;mailto:sara@gmail.com&quot;&gt;sara@gmail.com&lt;/a&gt;&lt;/td&gt;&lt;td&gt;Sara&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;How would we go about &lt;strong&gt;retrieving all Gmail emails&lt;/strong&gt;? Allow me to introduce you to &lt;code&gt;LIKE&lt;/code&gt;. 🤗 This command enables you to perform some basic pattern matching.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;sql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-sql&quot;&gt;SQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-sql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Users &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;WHERE&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Email &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;LIKE&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;%gmail%&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Outputs:&lt;/p&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Email&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;mailto:jack@gmail.com&quot;&gt;jack@gmail.com&lt;/a&gt;&lt;/td&gt;&lt;td&gt;Jack&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;mailto:mike@gmail.com&quot;&gt;mike@gmail.com&lt;/a&gt;&lt;/td&gt;&lt;td&gt;Mike&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;mailto:sara@gmail.com&quot;&gt;sara@gmail.com&lt;/a&gt;&lt;/td&gt;&lt;td&gt;Sara&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;After &lt;code&gt;LIKE&lt;/code&gt;, simply enter the &lt;em&gt;common&lt;/em&gt; value you&amp;#x27;d like to find. &lt;/p&gt;&lt;p&gt;&lt;code&gt;%&lt;/code&gt; &lt;strong&gt;serve as a catch-all symbol&lt;/strong&gt;, meaning it will match any character. For instance, &lt;code&gt;%gmail&lt;/code&gt; &lt;em&gt;will only match&lt;/em&gt; with values that has any kind and any amount of characters preceding the word &lt;em&gt;gmail&lt;/em&gt;, but none following it.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Understanding Stale Closures In JavaScript]]></title><description><![CDATA[Stale closures are difficult to avoid, therefore it's important to  recognize its occurrence . In another article, I mentioned that closures…]]></description><link>https://dilshankelsen.com/understanding-stale-closures-in-javascript/</link><guid isPermaLink="false">https://dilshankelsen.com/understanding-stale-closures-in-javascript/</guid><pubDate>Sat, 03 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Stale closures are difficult to avoid, therefore it&amp;#x27;s important to &lt;strong&gt;recognize its occurrence&lt;/strong&gt;. In another article, I mentioned that closures are nothing more than the &lt;em&gt;outer scope&lt;/em&gt; of a function.&lt;/p&gt;&lt;p&gt;However, there&amp;#x27;s one fine detail I left out: a closure resembles more a &lt;strong&gt;snapshot&lt;/strong&gt; 📸 of the scope&amp;#x27;s data when the function is created. This means that there&amp;#x27;s a risk of accessing &lt;em&gt;outdated data&lt;/em&gt; given the right circumstances.&lt;/p&gt;&lt;h2 id=&quot;an-example&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#an-example&quot; aria-label=&quot;an example permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;An example&lt;/h2&gt;&lt;p&gt;Things are always best explained through an example, aren&amp;#x27;t they? &lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; number &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  number &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; message &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;Incremented to &lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token template-string interpolation&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;Number: &lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token template-string interpolation&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; log &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Incremented to 1; Number: 3&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In this example, we have a function that &lt;strong&gt;increments&lt;/strong&gt; a variable &lt;code&gt;number&lt;/code&gt; and at the same time &lt;strong&gt;returns an anonymous function&lt;/strong&gt; that prints out 📝 the value of said variable.&lt;/p&gt;&lt;p&gt;Yet, something strange happens on the final line where &lt;code&gt;log&lt;/code&gt; is called: &lt;code&gt;number&lt;/code&gt; is printed out with &lt;strong&gt;2 different values&lt;/strong&gt; (i.e. &lt;code&gt;1&lt;/code&gt; &lt;em&gt;and&lt;/em&gt; &lt;code&gt;3&lt;/code&gt;). One would instinctively think that it should be &lt;code&gt;3&lt;/code&gt; in &lt;em&gt;both cases&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;What happened? In short: we are actually printing out and looking at &lt;strong&gt;two different&lt;/strong&gt; &lt;code&gt;number&lt;/code&gt; &lt;strong&gt;variables&lt;/strong&gt;. In long: &lt;em&gt;read on&lt;/em&gt;. 😛&lt;/p&gt;&lt;h2 id=&quot;understanding-in-steps&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#understanding-in-steps&quot; aria-label=&quot;understanding in steps permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Understanding in steps&lt;/h2&gt;&lt;p&gt;It&amp;#x27;s critical to understand what&amp;#x27;s happening in the following lines of code.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; log &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;step-1&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-1&quot; aria-label=&quot;step 1 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 1&lt;/h3&gt;&lt;p&gt;First off, the function &lt;code&gt;increment&lt;/code&gt; and variable &lt;code&gt;number&lt;/code&gt; are only ever &lt;em&gt;created once&lt;/em&gt; in the entire program. However, anything declared inside of &lt;code&gt;increment&lt;/code&gt; is actually &lt;strong&gt;created every time 🔁 the function is run&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;When &lt;code&gt;increment&lt;/code&gt; &lt;strong&gt;is first called&lt;/strong&gt;, it: &lt;/p&gt;&lt;ul&gt;&lt;li&gt;Updates &lt;code&gt;number&lt;/code&gt; to 1,&lt;/li&gt;&lt;li&gt;Creates a new variable called &lt;code&gt;message&lt;/code&gt;. This last variable &lt;em&gt;copies&lt;/em&gt; the value of 1 from &lt;code&gt;number&lt;/code&gt; and stores the string &amp;quot;&lt;strong&gt;Number: 1&lt;/strong&gt;&amp;quot; in a certain location in memory, and&lt;/li&gt;&lt;li&gt;Creates and returns an anonymous function. During the former, said function &lt;strong&gt;establishes a reference&lt;/strong&gt; to each variable outside its scope. This &lt;strong&gt;reference is maintained&lt;/strong&gt; even after the parent function is called. This &lt;em&gt;is closure&lt;/em&gt;!&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Suppose we now call &lt;code&gt;log&lt;/code&gt;. This is what we get:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; log &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Incremented to 1; Number: 1&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Nothing unexpected &lt;em&gt;so far&lt;/em&gt;. 😉&lt;/p&gt;&lt;h3 id=&quot;step-2&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-2&quot; aria-label=&quot;step 2 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 2&lt;/h3&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; log &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;increment&lt;/code&gt; &lt;strong&gt;is called 2&lt;sup&gt;nd&lt;/sup&gt; time&lt;/strong&gt;, it: &lt;/p&gt;&lt;ul&gt;&lt;li&gt;Updates &lt;code&gt;number&lt;/code&gt; to 2,&lt;/li&gt;&lt;li&gt;Creates a new variable called &lt;code&gt;message&lt;/code&gt;. This variable &lt;em&gt;copies&lt;/em&gt; the value of 2 from &lt;code&gt;number&lt;/code&gt; and stores the string &amp;quot;&lt;strong&gt;Number: 2&lt;/strong&gt;&amp;quot; in a &lt;em&gt;new&lt;/em&gt; location in memory, and&lt;/li&gt;&lt;li&gt;Creates and returns a &lt;em&gt;new&lt;/em&gt; anonymous function which references the &lt;strong&gt;location in memory&lt;/strong&gt; of the newly created &lt;code&gt;message&lt;/code&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;By now, you hopefully understood that our anonymous 👤 function &lt;strong&gt;is referencing a different&lt;/strong&gt; &lt;code&gt;message&lt;/code&gt; variable in memory whenever it&amp;#x27;s created.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; log &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Incremented to 1; Number: 1&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; log2 &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Incremented to 2; Number: 2&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solving-stale-closure&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#solving-stale-closure&quot; aria-label=&quot;solving stale closure permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Solving stale closure&lt;/h2&gt;&lt;p&gt;So how would we go about solving this issue? For this example, there are &lt;strong&gt;2 possibilities.&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Either &lt;strong&gt;move&lt;/strong&gt; &lt;code&gt;message&lt;/code&gt; &lt;strong&gt;to the root of the program,&lt;/strong&gt; so that only one copy of it ever exists. It is then simply updated on every &lt;code&gt;increment&lt;/code&gt; call. Or... 🤔&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; number &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; message &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  number &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  message &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;Incremented to &lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token template-string interpolation&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;Number: &lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token template-string interpolation&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; log &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Incremented to 3; Number: 3&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Move &lt;code&gt;message&lt;/code&gt; &lt;strong&gt;into the anonymous function&lt;/strong&gt;. Sure, it will still be created on every function call. However, the difference is that it is created when &lt;code&gt;log&lt;/code&gt; is called, not &lt;code&gt;increment&lt;/code&gt;. Thus, it &amp;quot;&lt;em&gt;sees&lt;/em&gt;&amp;quot; the latest value of &lt;code&gt;number&lt;/code&gt; as we intend it to.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; number &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  number &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; message &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;Incremented to &lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token template-string interpolation&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;Number: &lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token template-string interpolation&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; log &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Incremented to 3; Number: 3&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Make Styled Components Class Names Readable]]></title><description><![CDATA[As the class names of your styled components are  generated , even attempting to debug them can be a nightmare. Fortunately, there is a way…]]></description><link>https://dilshankelsen.com/make-styled-components-class-names-readable/</link><guid isPermaLink="false">https://dilshankelsen.com/make-styled-components-class-names-readable/</guid><pubDate>Thu, 01 Jul 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;As the class names of your styled components are &lt;em&gt;generated&lt;/em&gt;, even attempting to debug them can be a nightmare. Fortunately, there is a way to make the names  &lt;strong&gt;legible and more meaningful&lt;/strong&gt;, for example from &lt;code&gt;sc-AxgML&lt;/code&gt; to &lt;code&gt;Header__StyledMainHeader-AxgML&lt;/code&gt;.&lt;/p&gt;&lt;h2 id=&quot;add-a-plugin&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#add-a-plugin&quot; aria-label=&quot;add a plugin permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Add a plugin&lt;/h2&gt;&lt;p&gt;If you&amp;#x27;re reading this article, you likely don&amp;#x27;t have the babel plugin installed - do so first.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; --save-dev babel-plugin-styled-components&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In your &lt;code&gt;.babelrc&lt;/code&gt; file, add the plugin.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;json&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-json&quot;&gt;JSON&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-json&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;plugins&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;babel-plugin-styled-components&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And there you go! 🥳 &lt;em&gt;By default&lt;/em&gt;, the plugin will generate more legible class names by using your &lt;strong&gt;component&amp;#x27;s name&lt;/strong&gt; and the &lt;strong&gt;name of the file&lt;/strong&gt; it&amp;#x27;s in.&lt;/p&gt;&lt;h3 id=&quot;additional-configuration&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#additional-configuration&quot; aria-label=&quot;additional configuration permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Additional configuration&lt;/h3&gt;&lt;p&gt;You &lt;em&gt;can&lt;/em&gt; exclude file names from the generated class names by configuring your &lt;code&gt;.babelrc&lt;/code&gt; file accordingly.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;json&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-json&quot;&gt;JSON&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-json&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;plugins&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;babel-plugin-styled-components&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;fileName&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;However, if for whatever reason you wish to revert to unreadable 😛 class names, simple set &lt;code&gt;displayName&lt;/code&gt; to &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;json&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-json&quot;&gt;JSON&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-json&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;plugins&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;babel-plugin-styled-components&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;displayName&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Adding Path Aliases In Webpack Projects]]></title><description><![CDATA[I generally prefer to  use absolute paths over relative paths  as the latter can be a nightmare 😱 to use at times. And Webpack offers a…]]></description><link>https://dilshankelsen.com/adding-path-aliases-in-webpack-projects/</link><guid isPermaLink="false">https://dilshankelsen.com/adding-path-aliases-in-webpack-projects/</guid><pubDate>Mon, 28 Jun 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;I generally prefer to &lt;strong&gt;use absolute paths over relative paths&lt;/strong&gt; as the latter can be a nightmare 😱 to use at times. And Webpack offers a straightforward way to implement this with path aliases.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;../../some/file&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// From this,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;~some/file&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// To that!&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;path-prefixes&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#path-prefixes&quot; aria-label=&quot;path prefixes permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Path prefixes&lt;/h2&gt;&lt;p&gt;I &lt;em&gt;haven&amp;#x27;t seen much discussion&lt;/em&gt; about whether you should prefix a symbol to a path or even which symbol it should be, for that matter (e.g. &lt;code&gt;~&lt;/code&gt; or &lt;code&gt;@&lt;/code&gt;). So let me briefly give my 2 ✌️ cents on this matter.&lt;/p&gt;&lt;p&gt;I prefer &lt;strong&gt;prefixing my paths with&lt;/strong&gt; &lt;code&gt;~&lt;/code&gt;, aka a tilde. Why? Well, I want to &lt;em&gt;make it clear&lt;/em&gt; that the module I&amp;#x27;m importing is from the project itself. An &lt;code&gt;@&lt;/code&gt; gives me the impression I&amp;#x27;m importing an &lt;em&gt;external&lt;/em&gt; npm/Yarn package. Adding no prefix at all conveys the same thing.&lt;/p&gt;&lt;p&gt;What&amp;#x27;s left? The tilde &lt;strong&gt;isn&amp;#x27;t associated with anything in the JavaScript ecosystem&lt;/strong&gt;. Fun fact, it&amp;#x27;s actually used in Unix/Linux to represent your home directory! 😆&lt;/p&gt;&lt;h2 id=&quot;configuration&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#configuration&quot; aria-label=&quot;configuration permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Configuration&lt;/h2&gt;&lt;p&gt;Enough talk. In your &lt;em&gt;webpack configuration file&lt;/em&gt;, look out for the &lt;code&gt;resolve&lt;/code&gt; property. In it, you can add as many aliases as you desire.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;exports&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Add aliases here&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;alias&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token string-property property&quot;&gt;&amp;#x27;~components&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; path&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;__dirname&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;src/components/&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;pages&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; path&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;__dirname&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;src/pages/&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;utils&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; path&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;__dirname&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;src/utils.js&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;alias&lt;/code&gt; property takes an object, where&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;The key&lt;/strong&gt; represents the path alias, and&lt;/li&gt;&lt;li&gt;&lt;strong&gt;The value&lt;/strong&gt; the absolute system path to the folder or file.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Fortunately, &lt;code&gt;__dirname&lt;/code&gt; returns the root directory of the project. Therefore, you only need to &lt;em&gt;know the remaining path&lt;/em&gt; to your desired folder or file.&lt;/p&gt;&lt;div&gt;&lt;p&gt;If you want to prefix a symbol to a path alias, make sure to wrap it in quotes.&lt;/p&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Create An HTML5 Drawing App From Scratch]]></title><description><![CDATA[In this mini-series, you'll learn how to create a drawing app 🎨 with an HTML5 Canvas and JavaScript.  The topics  range from an…]]></description><link>https://dilshankelsen.com/create-html5-drawing-app-from-scratch/</link><guid isPermaLink="false">https://dilshankelsen.com/create-html5-drawing-app-from-scratch/</guid><pubDate>Sat, 26 Jun 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In this mini-series, you&amp;#x27;ll learn how to create a drawing app 🎨 with an HTML5 Canvas and JavaScript. &lt;strong&gt;The topics&lt;/strong&gt; range from an introduction to the canvas element up to the addition of a spray tool.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;/introduction-to-html5-canvas/&quot;&gt;An Introduction To HTML5 Canvas&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;/drawing-on-html5-canvas-using-mouse&quot;&gt;Drawing On A Canvas Using A Mouse&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;/dragging-mouse-outside-html5-canvas&quot;&gt;Dragging A Mouse Outside Of The Canvas&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;/change-drawing-colors-in-html5-canvas&quot;&gt;Changing Drawing Colors&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;/change-line-width-using-slider-in-html5-canvas&quot;&gt;Changing Line Width Using A Slider&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;/how-to-clear-html5-canvas&quot;&gt;Clearing An HTML5 Canvas&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;/creating-an-eraser-for-html5-canvas&quot;&gt;Creating An Eraser&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;/adding-spray-tool-to-html5-canvas&quot;&gt;Adding A Spray Tool&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Each article is accompanied by an interactive Codepen snippet ✏️ that includes the discussed code. &lt;em&gt;Enjoy&lt;/em&gt;!&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Create Global Styles With Styled Components]]></title><description><![CDATA[There are times where you want to style your React application  globally  and not each component  individually . For this, we can make use…]]></description><link>https://dilshankelsen.com/create-global-styles-with-styled-components/</link><guid isPermaLink="false">https://dilshankelsen.com/create-global-styles-with-styled-components/</guid><pubDate>Thu, 24 Jun 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;There are times where you want to style your React application &lt;em&gt;globally&lt;/em&gt; and not each component &lt;em&gt;individually&lt;/em&gt;. For this, we can make use of the &lt;code&gt;createGlobalStyle&lt;/code&gt; API.&lt;/p&gt;&lt;div&gt;&lt;p&gt;&lt;code&gt;createGlobalStyle&lt;/code&gt; is a web-only API, i.e. it won&amp;#x27;t work in React Native.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;step-1-create-a-global-styles-file&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-1-create-a-global-styles-file&quot; aria-label=&quot;step 1 create a global styles file permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 1: Create a global styles file&lt;/h2&gt;&lt;p&gt;Create a file that contains your global styles, e.g. &lt;code&gt;global.css.js&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; createGlobalStyle &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;styled-components&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; createGlobalStyle&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css selector&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;margin&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation parameter&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation property-access&quot;&gt;darkMode&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation string&quot;&gt;&amp;#x27;white&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation string&quot;&gt;&amp;#x27;black&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can apply string interpolations as expected. However, be aware that &lt;code&gt;createGlobalStyle&lt;/code&gt; returns a component that doesn&amp;#x27;t accept any children. 🧒&lt;/p&gt;&lt;h2 id=&quot;step-2-add-it-to-your-react-tree&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-2-add-it-to-your-react-tree&quot; aria-label=&quot;step 2 add it to your react tree permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 2: Add it to your React tree&lt;/h2&gt;&lt;p&gt;At the root of your application, add the global styles component at the same level or deeper than your content (&lt;em&gt;in this example&lt;/em&gt; &lt;code&gt;App&lt;/code&gt;).&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;react&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;ReactDOM&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;react-dom&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;ThemeProvider&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;styled-components&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;./App&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; theme &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;./theme&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;GlobalCSS&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;./global.css&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;ReactDOM&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;ThemeProvider&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;theme&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;theme&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;GlobalCSS&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;ThemeProvider&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;root&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Since &lt;code&gt;GlobalCSS&lt;/code&gt; is a styled component 💅, it can also take advantage of &lt;code&gt;ThemeProvider&lt;/code&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Find Value In Array Of Objects In JavaScript]]></title><description><![CDATA[Assume you've got an array of objects containing fruits. And let's suppose you want to  find the element containing   Banana  🍌. There are…]]></description><link>https://dilshankelsen.com/find-value-in-array-of-objects-in-javascript/</link><guid isPermaLink="false">https://dilshankelsen.com/find-value-in-array-of-objects-in-javascript/</guid><pubDate>Mon, 21 Jun 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Assume you&amp;#x27;ve got an array of objects containing fruits.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; fruits &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Orange&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Banana&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Apple&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And let&amp;#x27;s suppose you want to &lt;strong&gt;find the element containing&lt;/strong&gt; &lt;code&gt;Banana&lt;/code&gt; 🍌. There are 2 ways you can go about this problem, that I recommend.&lt;/p&gt;&lt;h2 id=&quot;arrayfind&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#arrayfind&quot; aria-label=&quot;arrayfind permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Array.find()&lt;/h2&gt;&lt;p&gt;The &lt;code&gt;find&lt;/code&gt; method iterates through every element of an array (similar to &lt;code&gt;map&lt;/code&gt; or &lt;code&gt;forEach&lt;/code&gt;) and evaluates a statement you defined. If the statement is ever true, the iteration is stopped and &lt;em&gt;that exact element&lt;/em&gt; is returned from the function.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; bananaObject &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; fruits&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; element&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Banana&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;bananaObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Object { id: 2, title: &amp;quot;Banana&amp;quot; }&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;arrayfindindex&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#arrayfindindex&quot; aria-label=&quot;arrayfindindex permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Array.findIndex()&lt;/h2&gt;&lt;p&gt;If you aren&amp;#x27;t interested in retrieving the element, but rather knowing if it exists, &lt;code&gt;findIndex&lt;/code&gt; is your friend. 😋 It &lt;strong&gt;behaves the same way as&lt;/strong&gt; &lt;code&gt;find&lt;/code&gt;, but returns the index of the matching element (&lt;em&gt;or&lt;/em&gt; &lt;code&gt;-1&lt;/code&gt; &lt;em&gt;if not found&lt;/em&gt;).&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; bananaIndex &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; fruits&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;findIndex&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; element&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Banana&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;bananaIndex&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// 1&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Adding A Spray Tool To An HTML5 Canvas]]></title><description><![CDATA[Adding a spray tool can seem  complicated  at first, but the logic behind it is relatively simple. A spray tool  behaves very much like a…]]></description><link>https://dilshankelsen.com/adding-spray-tool-to-html5-canvas/</link><guid isPermaLink="false">https://dilshankelsen.com/adding-spray-tool-to-html5-canvas/</guid><pubDate>Sat, 19 Jun 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Adding a spray tool can seem &lt;em&gt;complicated&lt;/em&gt; at first, but the logic behind it is relatively simple. A spray tool &lt;strong&gt;behaves very much like a pencil tool&lt;/strong&gt;, but it differs in one simple way: 💁‍♂️ it adds &lt;em&gt;additional random points&lt;/em&gt; around every point drawn.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:164px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:89.63414634146343%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAIAAADUsmlHAAAACXBIWXMAAAsTAAALEwEAmpwYAAABvElEQVQ4y6VTv6tBcRS/ZJDF4g+Q3YLE7g9QNotMNsoki39At4wGVsqgRDKJZFMUmSwShWKQX9fz3uf56Ou++26p985wOufc8zk/Pud7pc9/iCSs+/1OI5PJeDyeZrMJ++MhiqKIr/rg2+0GPRgMpIe4XC6CCXsDRh70drt1Op0Ax+Nxxne73XQ6RXMxIDN/gEX55XLZ6/VOpxPs/X7v9/tRK51OczoiqSVaykOIZwm6/X6fW3i9XgLO5/Nms2GO9HsfuOgggslk0m63VyoV2BgnHA5brdZ8Pv8Er9frVCpVLBY1tNdqtVKpRJd0YhCTyYRBcI7r9foNjsVinK3VasG9XC7QnU7HaDQiWCgURPB4PIZCIYvFksvlnp0TiQSSDAZDu92Gi5JsYjabES+Xy1yVLAC/WCxeO4PSbDZbrVY1+6N5o9EgT+LaWrY1hOGDIIxJ4/E4Eolgf/HmXndWn0rdBBGu4PP5MD+omkwmoqL+I5nNZvV6/XA4iLxgMAiwzWabz+f6YIZWq5XD4UBqNBoVF8ILxWFHo5EaqfNjDIdDns3tdqsf3PtfknmyLAcCgW63K/pAi1Ppg/8gX9GMmfQmRITXAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Random dots around drawn dot&quot; title=&quot;Random dots around drawn dot&quot; src=&quot;/static/0d05e20a13b5bd0e2bd4bd792f1ec878/2001c/image-1.png&quot; srcSet=&quot;/static/0d05e20a13b5bd0e2bd4bd792f1ec878/2001c/image-1.png 164w&quot; sizes=&quot;(max-width: 164px) 100vw, 164px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Drawn dot (with gray border) &lt;br/&gt; surrounded by randomly generated dots&lt;/figcaption&gt;&lt;h2 id=&quot;change-draw-style&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#change-draw-style&quot; aria-label=&quot;change draw style permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Change draw style&lt;/h2&gt;&lt;p&gt;Before we work on the logic of the spray distribution, let&amp;#x27;s first create a dropdown that enables a user 👩‍💻 to &lt;strong&gt;switch between drawing styles&lt;/strong&gt;: &lt;/p&gt;&lt;ul&gt;&lt;li&gt;Spray&lt;/li&gt;&lt;li&gt;Pencil&lt;/li&gt;&lt;/ul&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-name&quot;&gt;onchange&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript function&quot;&gt;changeDrawStyle&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript property-access&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;pencil&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Pencil&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;spray&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Spray&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I&amp;#x27;ve added a function called &lt;code&gt;changeDrawStyle&lt;/code&gt; that &lt;strong&gt;listens to any changes in the dropdown&lt;/strong&gt;. And it has &lt;em&gt;access&lt;/em&gt; to these changes through the argument &lt;code&gt;this.value&lt;/code&gt;.&lt;/p&gt;&lt;h3 id=&quot;the-function&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-function&quot; aria-label=&quot;the function permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The function&lt;/h3&gt;&lt;p&gt;What does the function do? Simple really! 🤓 We first track the current drawing style with a variable called &lt;code&gt;drawStyle&lt;/code&gt;. The function then simply &lt;strong&gt;changes the value&lt;/strong&gt; whenever a new style is selected.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; drawStyle &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;pencil&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;changeDrawStyle&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;newStyle&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; drawStyle &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; newStyle&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;spray-logic&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#spray-logic&quot; aria-label=&quot;spray logic permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Spray logic&lt;/h2&gt;&lt;p&gt;The &lt;strong&gt;backbone of the spray logic&lt;/strong&gt; is called &lt;code&gt;getRandomOffset&lt;/code&gt;. Now don&amp;#x27;t worry! 😰 You don&amp;#x27;t really have to understand the function in depth if you don&amp;#x27;t want to. If you &lt;em&gt;google&lt;/em&gt; &amp;quot;Random points generator&amp;quot;, you&amp;#x27;ll find plenty of code snippets accomplishing the same thing.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getRandomOffset&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;radius&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; randomAngle &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;PI&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; randomRadius &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; radius&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;cos&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;randomAngle&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; randomRadius&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;sin&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;randomRadius&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; randomAngle&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The only thing you should know is that it randomly generates a coordinate &lt;strong&gt;inside the shape of a circle&lt;/strong&gt; ⭕️.&lt;/p&gt;&lt;h3 id=&quot;applying-the-random-points&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#applying-the-random-points&quot; aria-label=&quot;applying the random points permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Applying the random points&lt;/h3&gt;&lt;p&gt;For every point that we &amp;quot;&lt;em&gt;draw&lt;/em&gt;&amp;quot; (which we are actually not), let&amp;#x27;s &lt;strong&gt;add 20 other points&lt;/strong&gt; around it. We add the random coordinates to the &amp;quot;&lt;em&gt;drawn&lt;/em&gt;&amp;quot; central point and then apply every random point to the canvas with &lt;code&gt;fillRect&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;generateSprayPoints&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; amountOfPoints &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; i &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; i &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; amountOfPoints&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; i&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; offset &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getRandomOffset&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;lineWidth&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; x &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; event&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;clientX&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; offset&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; y &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; event&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;clientY&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; offset&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;fillStyle&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;strokeStyle&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;fillRect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; y&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Yes, by using &lt;code&gt;fillRect&lt;/code&gt;, each point is &lt;em&gt;technically&lt;/em&gt; 😛 a square. However, it&amp;#x27;s so small you cannot tell. Furthermore, the color of the &amp;quot;&lt;em&gt;square&lt;/em&gt;&amp;quot; should always be equals to that of the app state, i.e. &lt;code&gt;context.strokeStyle&lt;/code&gt;. &lt;/p&gt;&lt;p&gt;Another point I should mention is that &lt;code&gt;getRandomOffset&lt;/code&gt; &lt;strong&gt;is dependent on our&lt;/strong&gt; &lt;a href=&quot;/change-line-width-using-slider-in-html5-canvas&quot;&gt;custom line width&lt;/a&gt; so that the spray &amp;quot;&lt;em&gt;grows&lt;/em&gt;&amp;quot; with a larger line width. You could &lt;strong&gt;also apply this&lt;/strong&gt; to the amount of generated points so that smaller sprays don&amp;#x27;t look as concentrated.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; amountOfPoints &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;lineWidth&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;checking-for-draw-style&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#checking-for-draw-style&quot; aria-label=&quot;checking for draw style permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Checking for draw style&lt;/h2&gt;&lt;p&gt;To wrap it up, in our &lt;code&gt;draw&lt;/code&gt; function, we always check if we want to use the spray tool. If yes, quit the function and generate the points. As mentioned previously, we don&amp;#x27;t draw the center line with a spray, hence we skip the &lt;code&gt;stroke&lt;/code&gt; method.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;draw&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;isDrawing&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;lineTo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;clientX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; event&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;clientY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;drawStyle &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;spray&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;generateSprayPoints&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;stroke&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As usual, you can check out the demonstrated code in an actual example &lt;a href=&quot;https://codepen.io/dkelsen/pen/yLgmyRa&quot; target=&quot;_blank&quot;&gt;right here&lt;/a&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Adding Search Functionality To A Gatsby Site]]></title><description><![CDATA[In this article I'll show you how I added search functionality to my Gatsby site. I decided to implement this using an  API-based search…]]></description><link>https://dilshankelsen.com/adding-search-functionality-to-gatsby-site/</link><guid isPermaLink="false">https://dilshankelsen.com/adding-search-functionality-to-gatsby-site/</guid><pubDate>Thu, 17 Jun 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In this article I&amp;#x27;ll show you how I added search functionality to my Gatsby site. I decided to implement this using an &lt;strong&gt;API-based search engine&lt;/strong&gt;, namely &lt;a href=&quot;https://www.algolia.com/&quot; target=&quot;_blank&quot;&gt;Algolia&lt;/a&gt;, for several reasons.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Opting for a client-side solution will &lt;em&gt;eventually&lt;/em&gt; bloat the bundle size of your site as content increases,&lt;/li&gt;&lt;li&gt;Algolia offers a &lt;em&gt;generous&lt;/em&gt; free tier and has great documentation for ReactJS/Gatsby, and&lt;/li&gt;&lt;li&gt;&lt;strong&gt;If I ever use up the free tier&lt;/strong&gt; (which is a great problem to have 🤩), I can easily opt for a more affordable solution, such as a self-hosted Elasticsearch instance.&lt;/li&gt;&lt;/ul&gt;&lt;h2 id=&quot;adding-algolia&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#adding-algolia&quot; aria-label=&quot;adding algolia permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Adding Algolia&lt;/h2&gt;&lt;p&gt;There&amp;#x27;s a handy-dandy &lt;a href=&quot;https://github.com/algolia/gatsby-plugin-algolia&quot; target=&quot;_blank&quot;&gt;Gatsby plugin for Algolia&lt;/a&gt; that will take care of the indexing of your pages. Indexing simple means creating a search-friendly list.&lt;/p&gt;&lt;div&gt;&lt;p&gt;By default, the plugin uses the frontmatter field &lt;code&gt;title&lt;/code&gt; from your Markdown files. If it&amp;#x27;s named differently in your site, you will need to &lt;em&gt;configure the &lt;a href=&quot;#configure-the-query&quot;&gt;Algolia query&lt;/a&gt;&lt;/em&gt;, accordingly.&lt;/p&gt;&lt;/div&gt;&lt;h3 id=&quot;account&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#account&quot; aria-label=&quot;account permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Account&lt;/h3&gt;&lt;p&gt;First of all, make sure you have created an &lt;a href=&quot;https://www.algolia.com/users/sign_up&quot; target=&quot;_blank&quot;&gt;Algolia account&lt;/a&gt;.&lt;/p&gt;&lt;h3 id=&quot;installation&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#installation&quot; aria-label=&quot;installation permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Installation&lt;/h3&gt;&lt;p&gt;Then, install the plugin in your project.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; gatsby-plugin-algolia&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;connect-the-api&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#connect-the-api&quot; aria-label=&quot;connect the api permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Connect the API&lt;/h3&gt;&lt;p&gt;Next on the list, connect 🔌 the plugin to your Algolia account using your &lt;a href=&quot;https://www.algolia.com/api-keys&quot; target=&quot;_blank&quot;&gt;API keys&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:40.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAAsTAAALEwEAmpwYAAABBUlEQVQoz3VSW26EMBDj/odBohJ32Io77A8PqR+ohZCQJ26cbBDb3Y5kjSYzOPaEqh++cL8P0NrCuRDhX+B9+BfsG2NhrUt1tQqDTVrsu40Ndw75B9G7C54RUIJ1pbXBsoh0w1XhSRzzcRxPCIE9l7IxBm3b4nb7jLMHKhLRrrU+gSQ8I6iYdQiZhJkX/I1t2yClypbpXwgZDyV2na379HFG2VVRxuj7HnVdo2maiI+Uu67LhFS3LBLrqqAeeyy2rSuqMxFJGUopjOOIaZoShmHAPM/JQbXvBt8/IkrWUaU+Ca675GAhy0qPF9tsp0ehIhIlRFIpTVRgIIlY59/Jv32YK8rZL7Lybq0JOGvWAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;API Keys dashboard in Algolia&quot; title=&quot;API Keys dashboard in Algolia&quot; src=&quot;/static/e62041a1ffa300bcf8ac8eb12291d107/5a190/image-1.png&quot; srcSet=&quot;/static/e62041a1ffa300bcf8ac8eb12291d107/772e8/image-1.png 200w,/static/e62041a1ffa300bcf8ac8eb12291d107/e17e5/image-1.png 400w,/static/e62041a1ffa300bcf8ac8eb12291d107/5a190/image-1.png 800w,/static/e62041a1ffa300bcf8ac8eb12291d107/c1b63/image-1.png 1200w,/static/e62041a1ffa300bcf8ac8eb12291d107/d93d9/image-1.png 1243w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;API Keys dashboard in Algolia&lt;/figcaption&gt;&lt;p&gt;Copy the values of &lt;em&gt;Application ID&lt;/em&gt;, &lt;em&gt;Search-Only API Key&lt;/em&gt; and &lt;em&gt;Admin API Key&lt;/em&gt;, and paste them into a &lt;code&gt;.env&lt;/code&gt; file at the root of your project. Of course, ⚠️ &lt;strong&gt;don&amp;#x27;t commit this file&lt;/strong&gt; to your repository.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;.env&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-.env&quot;&gt;.ENV&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-.env&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;GATSBY_ALGOLIA_APP_ID=&amp;lt;App ID&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;GATSBY_ALGOLIA_SEARCH_KEY=&amp;lt;Search-Only API Key&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;ALGOLIA_ADMIN_KEY=&amp;lt;Admin API Key&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;configure-the-plugin&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#configure-the-plugin&quot; aria-label=&quot;configure the plugin permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Configure the plugin&lt;/h3&gt;&lt;p&gt;While we have installed the plugin, Gatsby isn&amp;#x27;t aware of it yet. Therefore, in &lt;code&gt;gatsby-config.js&lt;/code&gt;, make the following changes.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Add this to the top&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;dotenv&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;plugins&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// ... existing plugins&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;gatsby-plugin-algolia&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;appId&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; process&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;GATSBY_ALGOLIA_APP_ID&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;apiKey&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; process&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;ALGOLIA_ADMIN_KEY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;queries&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;./src/utils/algolia-queries&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;dotenv&lt;/code&gt; library, which is &lt;strong&gt;already installed&lt;/strong&gt; in Gatsby, will make the variables from our &lt;code&gt;.env&lt;/code&gt; file accessible in our configuration file.&lt;/p&gt;&lt;h3 id=&quot;configure-the-query&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#configure-the-query&quot; aria-label=&quot;configure the query permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Configure the query&lt;/h3&gt;&lt;p&gt;The plugin also needs to know what content on your site is &amp;quot;&lt;em&gt;searchable&lt;/em&gt;&amp;quot;, which will be done using a GraphQL query. I&amp;#x27;ve decided to define it in a separate file &lt;strong&gt;to avoid clutter&lt;/strong&gt; 😌, though you may do so directly in the &lt;code&gt;queries&lt;/code&gt; property if you wish to.&lt;/p&gt;&lt;p&gt;Most likely, you will also need to &lt;em&gt;adjust the query&lt;/em&gt; and change &lt;code&gt;pagePath&lt;/code&gt; (i.e. location of your Markdown files).&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; escapeStringRegexp &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;escape-string-regexp&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; pagePath &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;_posts&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; indexName &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;Pages&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; pageQuery &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;  pages: allMarkdownRemark(&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;    filter: {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;      fileAbsolutePath: { regex: &amp;quot;/&lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token template-string interpolation function&quot;&gt;escapeStringRegexp&lt;/span&gt;&lt;span class=&quot;token template-string interpolation punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string interpolation&quot;&gt;pagePath&lt;/span&gt;&lt;span class=&quot;token template-string interpolation punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;/&amp;quot; },&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;    }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;  ) {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;    edges {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;      node {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;        id&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;        frontmatter {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;          title&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;          tags&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;        }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;        fields {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;          slug&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;        }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;        excerpt(pruneLength: 5000)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;      }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;    }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;  }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;pageToAlgoliaRecord&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token parameter literal-property property&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;token parameter operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; id&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; frontmatter&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; fields&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token parameter spread operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;rest &lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;objectID&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token spread operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;frontmatter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token spread operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;fields&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token spread operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;rest&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; queries &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; pageQuery&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;transformer&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; data &lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; data&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;pages&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;edges&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;pageToAlgoliaRecord&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    indexName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;settings&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;attributesToSnippet&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;excerpt:20&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;exports&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; queries&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;After you run &lt;code&gt;gatsby build&lt;/code&gt;, you should then see your pages indexed in your Algolia account under &amp;quot;&lt;em&gt;Indices&lt;/em&gt;&amp;quot;. 🥳&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:44.99999999999999%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA5klEQVQoz5VRyxLDIAj0//+yl54bUQMaQ1lS01faTpnZhCi7sCScTmcFYpw1kqFkrdJ0WbqjteUvhFJYgRizUip6IdJpIhUTnefqRUP8E54Ep6mYoGjObAJiedWU2M8Gfk31KBxSFp8GH3Kz2veC0b2/wev66m/m5qjVBJlFU94mgsUBXH6zyQwnRcHPxo1U3GXABN7hNiXymSHYdhuvYphMpJpAceHWmgsjDyg4CqJkTUAS3ynRBv9RBxysofeuAQ/YG/sbgXNg6ceWEeu67rX7DnGJBBZhbwjBFgDSEe51aLDxq/Gvy6DDiIUjfFUAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Indices page of an Algolia app&quot; title=&quot;Indices page of an Algolia app&quot; src=&quot;/static/a69ccb35cd281815a50268e607b4a12a/5a190/image-2.png&quot; srcSet=&quot;/static/a69ccb35cd281815a50268e607b4a12a/772e8/image-2.png 200w,/static/a69ccb35cd281815a50268e607b4a12a/e17e5/image-2.png 400w,/static/a69ccb35cd281815a50268e607b4a12a/5a190/image-2.png 800w,/static/a69ccb35cd281815a50268e607b4a12a/264eb/image-2.png 867w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Indices page in Algolia&lt;/figcaption&gt;&lt;h2 id=&quot;adding-the-ui&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#adding-the-ui&quot; aria-label=&quot;adding the ui permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Adding the UI&lt;/h2&gt;&lt;p&gt;As the user interface heavily depends on your website and design preferences, I &lt;strong&gt;won&amp;#x27;t go into too much detail&lt;/strong&gt; about it. Rather, I&amp;#x27;ll try to provide an &lt;em&gt;overview of the logic&lt;/em&gt; behind it.&lt;/p&gt;&lt;h3 id=&quot;dependencies&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#dependencies&quot; aria-label=&quot;dependencies permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Dependencies&lt;/h3&gt;&lt;p&gt;To interact  with the Algolia API from the UI, I&amp;#x27;ll be using 🙌&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;https://www.npmjs.com/package/algoliasearch&quot; target=&quot;_blank&quot;&gt;Algolia Search&lt;/a&gt; - provides the API client, and&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/react/&quot; target=&quot;_blank&quot;&gt;React InstantSearch&lt;/a&gt; - a React component library from Algolia to easily build search interfaces.&lt;/li&gt;&lt;/ul&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; react-instantsearch-dom algoliasearch&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;the-input-field&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-input-field&quot; aria-label=&quot;the input field permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The input field&lt;/h3&gt;&lt;p&gt;The &amp;quot;&lt;em&gt;search box&lt;/em&gt;&amp;quot;, as Algolia calls it, is nothing more than a &lt;code&gt;form&lt;/code&gt; with an &lt;code&gt;input&lt;/code&gt; field. The &lt;code&gt;connectSearchBox&lt;/code&gt; function &lt;strong&gt;takes care of all the interaction&lt;/strong&gt; between the search query and on your indices.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;react&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; connectSearchBox &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;react-instantsearch-dom&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;connectSearchBox&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; refine&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; currentRefinement&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; className&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; onFocus &lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;SearchInput&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;Search&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;aria-label&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;Search&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;onChange&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript parameter&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript function&quot;&gt;refine&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript property-access&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript property-access&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;currentRefinement&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;onFocus&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;onFocus&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The input value is exposed by the variable &lt;code&gt;currentRefinement&lt;/code&gt;, while the &lt;code&gt;refine&lt;/code&gt; function allows you to modify it.&lt;/p&gt;&lt;div&gt;&lt;p&gt;Instead of creating a custom &lt;em&gt;SearchBox&lt;/em&gt;, you can alternatively use a pre-made &lt;a href=&quot;https://www.algolia.com/doc/api-reference/widgets/search-box/react/&quot; target=&quot;_blank&quot;&gt;&lt;em&gt;widget&lt;/em&gt;&lt;/a&gt; from the library.&lt;/p&gt;&lt;/div&gt;&lt;h3 id=&quot;search-results&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#search-results&quot; aria-label=&quot;search results permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Search results&lt;/h3&gt;&lt;p&gt;The search results are displayed as a &lt;strong&gt;simple list 📃 of articles&lt;/strong&gt; that feature a title and a snippet. Again, &lt;code&gt;react-instantsearch-dom&lt;/code&gt; helps us out with most of the heavy lifting.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;react&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;Link&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;gatsby&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token imports&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;Highlight&lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token imports&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;Hits&lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token imports&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;Index&lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token imports&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;Snippet&lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token imports&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;PoweredBy&lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token imports&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;react-instantsearch-dom&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function maybe-class-name&quot;&gt;PageHit&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; hit &lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Link&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;hit&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript property-access&quot;&gt;slug&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;h4&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Highlight&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;attribute&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;hit&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;hit&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;tagName&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;mark&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;h4&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Link&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Snippet&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;attribute&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;excerpt&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;hit&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;hit&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;tagName&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;mark&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function maybe-class-name&quot;&gt;HitsInIndex&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; index &lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Index&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;indexName&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript property-access&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Hits&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;Hits&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;hitComponent&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript maybe-class-name&quot;&gt;PageHit&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Index&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function maybe-class-name&quot;&gt;SearchResults&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; indices&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; className&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; show &lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;show &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript literal-property property&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript string&quot;&gt;&amp;#x27;none&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;indices&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;HitsInIndex&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript property-access&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;PoweredBy&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;SearchResults&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;By default, your entire search index will be shown, i.e. &lt;em&gt;all your indexed pages&lt;/em&gt;. Thus, we hide everything using &lt;code&gt;show&lt;/code&gt; until the user types in something, which in turn filters the results.&lt;/p&gt;&lt;div&gt;&lt;p&gt;If you&amp;#x27;re using the free tier of Algolia, you&amp;#x27;re required to use the PoweredBy widget.&lt;/p&gt;&lt;/div&gt;&lt;div&gt;&lt;p&gt;Similar to before, you can take advantage of a pre-made &lt;a href=&quot;https://www.algolia.com/doc/api-reference/widgets/hits/react/&quot; target=&quot;_blank&quot;&gt;&lt;em&gt;widget&lt;/em&gt;&lt;/a&gt; instead of creating a custom &lt;em&gt;SearchResults&lt;/em&gt; component.&lt;/p&gt;&lt;/div&gt;&lt;h3 id=&quot;creating-a-search-component&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#creating-a-search-component&quot; aria-label=&quot;creating a search component permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Creating a search component&lt;/h3&gt;&lt;p&gt;Almost there, I promise! 😝 After creating the search box and search results, &lt;strong&gt;place them in a common&lt;/strong&gt; &lt;code&gt;Search&lt;/code&gt; &lt;strong&gt;component&lt;/strong&gt;. &lt;/p&gt;&lt;p&gt;It will keep track of the search value, whether the input field is in focus as well as &lt;em&gt;hook up everything to the client API&lt;/em&gt; of Algolia.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; useState &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;react&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt;algoliasearch&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;algoliasearch/lite&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;InstantSearch&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;react-instantsearch-dom&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;SearchBox&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;./SearchBox&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;SearchResults&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;./SearchResults&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function maybe-class-name&quot;&gt;Search&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; indices &lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; setQuery&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;hasFocus&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; setFocus&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; searchClient &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;algoliasearch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    process&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;GATSBY_ALGOLIA_APP_ID&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    process&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;GATSBY_ALGOLIA_SEARCH_KEY&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;InstantSearch&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;searchClient&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;searchClient&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;indexName&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;indices&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript property-access&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;onSearchStateChange&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript parameter punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript parameter&quot;&gt; query &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript parameter punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript function&quot;&gt;setQuery&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;SearchBox&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;onFocus&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript function&quot;&gt;setFocus&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;hasFocus&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;hasFocus&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;SearchResults&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;query &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; query&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript property-access&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; hasFocus&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;indices&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;indices&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;InstantSearch&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;search-in-action&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#search-in-action&quot; aria-label=&quot;search in action permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Search in action&lt;/h3&gt;&lt;p&gt;To finally use the search functionality, in a page of your choice, insert the following lines:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;diff&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-diff&quot;&gt;DIFF&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-diff&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token inserted-sign inserted prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted line&quot;&gt; import Search from &amp;quot;./search&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token inserted-sign inserted line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted line&quot;&gt; const searchIndices = [{ name: `Pages`, title: `Pages` }]&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token inserted-sign inserted line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;const SomePage = () =&amp;gt; (&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted line&quot;&gt; &amp;lt;Search indices={searchIndices} /&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token inserted-sign inserted line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;searchIndices&lt;/code&gt; specifies which indices Algolia should use from your account. If you followed my earlier instructions &lt;em&gt;to the teeth&lt;/em&gt; 🦷, you can leave it as is.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[What Are Docker Images, -Files And Containers]]></title><description><![CDATA[The best way to understand what Docker images, -files and containers are, is to  understand how to they relate to each other . For that, I…]]></description><link>https://dilshankelsen.com/what-are-docker-images-and-files/</link><guid isPermaLink="false">https://dilshankelsen.com/what-are-docker-images-and-files/</guid><pubDate>Mon, 14 Jun 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The best way to understand what Docker images, -files and containers are, is to &lt;strong&gt;understand how to they relate to each other&lt;/strong&gt;. For that, I found an image (&lt;em&gt;pun intended&lt;/em&gt; 😝) that does a great job of demonstrating this.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:483px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:34.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAIAAACHqfpvAAAACXBIWXMAAAsTAAALEwEAmpwYAAABVElEQVQY03VQvU7DMBjMMzEhMSMGRngAGJkZGBETA0gsPAFIwFDBABJMFSoDCBUkUAUspVWT2E7a2Els13F+uagzN3yWTv7uuztHKRnHcZpKoCiKpmnqus7zwlqMAqMsy+YfOJ7nEUqlVFrPjTGgIFGW2LMBo3mrUFR1DX4i1NNEvNPkw+OfYwZVh1IaRVGWZTiC+1rr1kJdu1H87dKsaVJtpFK1kReD6dr5cPuOrBz3lvc6QmonDMOI8yRJMmsF56Px2GjV+w1Wjy43T68Pu4PhLBFQ16pP4t3bn50Hb+ngfn2/k+XWcV03nE5hGA4XtoHOF9m6etk4ez55812do4eqNd6wgHX7HzePryMStpl9n2CfMRYEAfZBYQZcsFnUTpHyVIJErCRJEc2aeWmNzQySOkopn7TAclVVi8IYmiAkFsJzJ4T4IGEK1eJnOJ3hQTrO+R8LcnyeoBoN3wAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Process to create a Docker container&quot; title=&quot;Process to create a Docker container&quot; src=&quot;/static/2a3a1df0be72a9a95b0ed1e4eec43eac/77a9e/image-1.png&quot; srcSet=&quot;/static/2a3a1df0be72a9a95b0ed1e4eec43eac/772e8/image-1.png 200w,/static/2a3a1df0be72a9a95b0ed1e4eec43eac/e17e5/image-1.png 400w,/static/2a3a1df0be72a9a95b0ed1e4eec43eac/77a9e/image-1.png 483w&quot; sizes=&quot;(max-width: 483px) 100vw, 483px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Process of creating a container&lt;/figcaption&gt;&lt;h2 id=&quot;dockerfile&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#dockerfile&quot; aria-label=&quot;dockerfile permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Dockerfile&lt;/h2&gt;&lt;p&gt;A Dockerfile is simply a text file that contains &lt;strong&gt;instructions on how to build an image&lt;/strong&gt;. In it, you specify the things you&amp;#x27;d like to use, such as software dependencies and your custom code. The instructions look something like this:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-text&quot;&gt;TEXT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-text&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;FROM ubuntu:18.04&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;COPY . /app&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;RUN make /app&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;CMD python /app/app.py&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The software dependencies that you specify (e.g. Ubuntu in this example) will be Docker images themselves pulled from the &lt;a href=&quot;https://hub.docker.com/&quot; target=&quot;_blank&quot;&gt;official repository&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Afterwards, you run the &lt;code&gt;docker build&lt;/code&gt; command to &lt;strong&gt;create an image from the file&lt;/strong&gt;.&lt;/p&gt;&lt;h2 id=&quot;docker-image&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#docker-image&quot; aria-label=&quot;docker image permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Docker image&lt;/h2&gt;&lt;p&gt;In my mind, 🧠 images are simply &lt;em&gt;idle containers&lt;/em&gt; that have everything installed (e.g. Node.js and my custom code) and include instructions on how to launch the entire application.&lt;/p&gt;&lt;p&gt;It&amp;#x27;s much like your laptop, which has a bunch of tools installed and contains your programming project, but &lt;strong&gt;it isn&amp;#x27;t running&lt;/strong&gt; &lt;em&gt;yet&lt;/em&gt;!&lt;/p&gt;&lt;p&gt;From an image, you then create a Docker container with &lt;code&gt;docker run&lt;/code&gt;.&lt;/p&gt;&lt;h2 id=&quot;docker-container&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#docker-container&quot; aria-label=&quot;docker container permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Docker container&lt;/h2&gt;&lt;p&gt;With a better understanding of Docker images, containers are nothing more than a &lt;strong&gt;running instance of your image&lt;/strong&gt;. Things are actually happening inside the container. &lt;/p&gt;&lt;p&gt;You can stop, restart, move or delete the container, and even create a new image based on the current state of your container.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Creating An Eraser For HTML5 Canvas]]></title><description><![CDATA[There are different ways to creating an eraser. ⌫ The  simplest solution  you will come across is an "eraser" that matches the background…]]></description><link>https://dilshankelsen.com/creating-an-eraser-for-html5-canvas/</link><guid isPermaLink="false">https://dilshankelsen.com/creating-an-eraser-for-html5-canvas/</guid><pubDate>Sat, 12 Jun 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;There are different ways to creating an eraser. ⌫ The &lt;strong&gt;simplest solution&lt;/strong&gt; you will come across is an &amp;quot;eraser&amp;quot; that matches the background color. This might &lt;em&gt;give you the impression&lt;/em&gt; that you are erasing the content, when in fact you are simply &lt;strong&gt;drawing on top&lt;/strong&gt; of it.&lt;/p&gt;&lt;p&gt;Of course, this method &lt;em&gt;won&amp;#x27;t work&lt;/em&gt; with a more complex background, such as a picture. 🏙 Therefore, let me show you the &lt;strong&gt;recommended solution&lt;/strong&gt;.&lt;/p&gt;&lt;h2 id=&quot;removing-pixels&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#removing-pixels&quot; aria-label=&quot;removing pixels permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Removing pixels&lt;/h2&gt;&lt;p&gt;The key to all this is the context property &lt;code&gt;globalCompositeOperation&lt;/code&gt;. &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation&quot; target=&quot;_blank&quot;&gt;This property&lt;/a&gt; dictates how the canvas behaves when new shapes are drawn. For our objective, 🎯 we are interested in setting it to &lt;code&gt;destination-out&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; context &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; canvas&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getContext&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;2d&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;globalCompositeOperation&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;destination-out&amp;#x27;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;What this does is make way for any new shapes by &lt;em&gt;removing existing pixels&lt;/em&gt; that overlap with it. More importantly, however - this &lt;strong&gt;new shape isn&amp;#x27;t actually drawn&lt;/strong&gt; even if you have a color 👩‍🎨 set.&lt;/p&gt;&lt;h2 id=&quot;creating-a-button&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#creating-a-button&quot; aria-label=&quot;creating a button permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Creating a button&lt;/h2&gt;&lt;p&gt;To trigger this eraser functionality at will, let&amp;#x27;s set the property through a function.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;erase&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;globalCompositeOperation&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;destination-out&amp;#x27;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Afterwards, we simply attach it to an &amp;quot;&lt;em&gt;Eraser&lt;/em&gt;&amp;quot; button&amp;#x27;s &lt;strong&gt;click event&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;button onclick&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;erase()&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;Eraser&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;drawing-again&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#drawing-again&quot; aria-label=&quot;drawing again permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Drawing again&lt;/h2&gt;&lt;p&gt;But wait! ✋ Once &lt;code&gt;erase&lt;/code&gt; is triggered, the &lt;em&gt;&amp;quot;eraser&amp;quot; mode&lt;/em&gt; will stay on until it is removed. To do that, we need to &lt;strong&gt;revert the property back to its default&lt;/strong&gt; value, i.e. &lt;code&gt;source-over&lt;/code&gt;. The perfect time to do so is when a user &lt;a href=&quot;/change-drawing-colors-in-html5-canvas&quot;&gt;changes the drawing color&lt;/a&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;changeColor&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;strokeStyle&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; color&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;globalCompositeOperation&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;source-over&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Check out &lt;a href=&quot;https://codepen.io/dkelsen/pen/MWJNgPG&quot; target=&quot;_blank&quot;&gt;my Codepen&lt;/a&gt; for the eraser in action!&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Learn How To Use The UseCallback Hook]]></title><description><![CDATA[The  useCallback  hook in React is used whenever you want to prevent an inline function (i.e. a function  inside  a React component) from…]]></description><link>https://dilshankelsen.com/learn-usecallback-hook/</link><guid isPermaLink="false">https://dilshankelsen.com/learn-usecallback-hook/</guid><pubDate>Thu, 10 Jun 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The &lt;code&gt;useCallback&lt;/code&gt; hook in React is used whenever you want to prevent an inline function (i.e. a function &lt;em&gt;inside&lt;/em&gt; a React component) from being created on each render. Let me show you what I mean by that.&lt;/p&gt;&lt;h2 id=&quot;reference-equality&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#reference-equality&quot; aria-label=&quot;reference equality permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Reference equality&lt;/h2&gt;&lt;p&gt;How does React know whether a function is newly created or is memoized from a previous render. Reference equality is the &lt;em&gt;name of the game&lt;/em&gt;. ✊&lt;/p&gt;&lt;p&gt;Take a look at the example below where we call a factory function that returns another function, much like &lt;code&gt;useCallback&lt;/code&gt;! We store the returned functions and then compare them.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;factory&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Woah...&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; funcA &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;factory&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; funcB &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;factory&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;funcA&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Woah...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;funcB&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Woah...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;funcA &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; funcA&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// true&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;funcA &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; funcB&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// false&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You might (wrongly ❌) presume that &lt;code&gt;funcA&lt;/code&gt; and &lt;code&gt;funcB&lt;/code&gt; are identical as they were returned by the same function. However, they are both stored in different places in memory, which is why the comparison equates to &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Thus, &lt;em&gt;comparing by reference&lt;/em&gt; simply means comparing the storage location of the function.&lt;/p&gt;&lt;h2 id=&quot;the-issue&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-issue&quot; aria-label=&quot;the issue permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The issue&lt;/h2&gt;&lt;p&gt;I created an interactive (&lt;em&gt;yes, interactive!&lt;/em&gt;) example, made up of a &lt;code&gt;Parent&lt;/code&gt; and &lt;code&gt;Child&lt;/code&gt; component.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;The parent&lt;/strong&gt; contains a counter, and an &lt;code&gt;increment&lt;/code&gt; function which is passed down to the child, while&lt;/li&gt;&lt;li&gt;&lt;strong&gt;The child&lt;/strong&gt; has the function attached to a button&amp;#x27;s &lt;code&gt;onClick&lt;/code&gt; event.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Pay attention 🚨 in particular to the &lt;code&gt;useEffect&lt;/code&gt; hook that gets triggered whenever it detects a different copy of &lt;code&gt;increment&lt;/code&gt;.&lt;/p&gt;&lt;div code=&quot;const Parent = () =&amp;gt; {
  console.log(&amp;#x27;Parent component rendered.&amp;#x27;)
  const [count, setCount] = React.useState(0)
  const increment = () =&amp;gt; setCount(prevCount =&amp;gt; prevCount + 1)

  return (
    &amp;lt;&amp;gt;
      &amp;lt;div&amp;gt;Count: {count}&amp;lt;/div&amp;gt;
      &amp;lt;Child increment={increment} /&amp;gt;
    &amp;lt;/&amp;gt;
  )
}

const Child = ({ increment }) =&amp;gt; {
  
  React.useEffect(() =&amp;gt; {
    console.log(&amp;#x27;New &amp;quot;increment&amp;quot; function found.&amp;#x27;)
  }, [increment])

  return (
    &amp;lt;button onClick={increment}&amp;gt;Increment&amp;lt;/button&amp;gt;
  )
}

render(&amp;lt;Parent /&amp;gt;)&quot; language=&quot;jsx&quot; class=&quot;live-highlight&quot; style=&quot;position:relative;text-align:left;box-sizing:border-box;padding:0;overflow:hidden;white-space:pre;font-family:monospace&quot;&gt;&lt;textarea style=&quot;margin:0;border:0;background:none;box-sizing:inherit;display:inherit;font-family:inherit;font-size:inherit;font-style:inherit;font-variant-ligatures:inherit;font-weight:inherit;letter-spacing:inherit;line-height:inherit;tab-size:inherit;text-indent:inherit;text-rendering:inherit;text-transform:inherit;white-space:pre-wrap;word-break:keep-all;overflow-wrap:break-word;position:absolute;top:0;left:0;height:100%;width:100%;resize:none;color:inherit;overflow:hidden;-moz-osx-font-smoothing:grayscale;-webkit-font-smoothing:antialiased;-webkit-text-fill-color:transparent;padding-top:10px;padding-right:10px;padding-bottom:10px;padding-left:10px&quot; class=&quot;npm__react-simple-code-editor__textarea&quot; autoCapitalize=&quot;off&quot; autoComplete=&quot;off&quot; autoCorrect=&quot;off&quot; spellcheck=&quot;false&quot; data-gramm=&quot;false&quot;&gt;const Parent = () =&amp;gt; {
  console.log(&amp;#x27;Parent component rendered.&amp;#x27;)
  const [count, setCount] = React.useState(0)
  const increment = () =&amp;gt; setCount(prevCount =&amp;gt; prevCount + 1)

  return (
    &amp;lt;&amp;gt;
      &amp;lt;div&amp;gt;Count: {count}&amp;lt;/div&amp;gt;
      &amp;lt;Child increment={increment} /&amp;gt;
    &amp;lt;/&amp;gt;
  )
}

const Child = ({ increment }) =&amp;gt; {
  
  React.useEffect(() =&amp;gt; {
    console.log(&amp;#x27;New &amp;quot;increment&amp;quot; function found.&amp;#x27;)
  }, [increment])

  return (
    &amp;lt;button onClick={increment}&amp;gt;Increment&amp;lt;/button&amp;gt;
  )
}

render(&amp;lt;Parent /&amp;gt;)&lt;/textarea&gt;&lt;pre aria-hidden=&quot;true&quot; style=&quot;margin:0;border:0;background:none;box-sizing:inherit;display:inherit;font-family:inherit;font-size:inherit;font-style:inherit;font-variant-ligatures:inherit;font-weight:inherit;letter-spacing:inherit;line-height:inherit;tab-size:inherit;text-indent:inherit;text-rendering:inherit;text-transform:inherit;white-space:pre-wrap;word-break:keep-all;overflow-wrap:break-word;position:relative;pointer-events:none;padding-top:10px;padding-right:10px;padding-bottom:10px;padding-left:10px&quot;&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token keyword&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function maybe-class-name&quot;&gt;Parent&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;&amp;#x27;Parent component rendered.&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; setCount&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setCount&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;prevCount&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; prevCount &lt;/span&gt;&lt;span class=&quot;token operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot; style=&quot;display:inline-block&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Count: &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;Child&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot; style=&quot;color:hsl(75, 70%, 60%)&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot; style=&quot;display:inline-block&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function maybe-class-name&quot;&gt;Child&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot; style=&quot;opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; increment &lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot; style=&quot;opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;useEffect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot; style=&quot;color:hsl(40, 90%, 60%)&quot;&gt;&amp;#x27;New &amp;quot;increment&amp;quot; function found.&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot; style=&quot;display:inline-block&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot; style=&quot;color:hsl(75, 70%, 60%)&quot;&gt;onClick&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Increment&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot; style=&quot;display:inline-block&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot; style=&quot;color:#C5C8C6&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt;Parent&lt;/span&gt;&lt;span class=&quot;token tag&quot; style=&quot;color:hsl(350, 40%, 70%)&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot; style=&quot;color:hsl(350, 40%, 70%);opacity:0.7&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot; style=&quot;opacity:0.7&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;style type=&quot;text/css&quot;&gt;
/**
 * Reset the text fill color so that placeholder is visible
 */
.npm__react-simple-code-editor__textarea:empty {
  -webkit-text-fill-color: inherit !important;
}

/**
 * Hack to apply on some CSS on IE10 and IE11
 */
@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
  /**
    * IE doesn&apos;t support &apos;-webkit-text-fill-color&apos;
    * So we use &apos;color: transparent&apos; to make the text transparent on IE
    * Unlike other browsers, it doesn&apos;t affect caret color in IE
    */
  .npm__react-simple-code-editor__textarea {
    color: transparent !important;
  }

  .npm__react-simple-code-editor__textarea::selection {
    background-color: #accef7 !important;
    color: transparent !important;
  }
}
&lt;/style&gt;&lt;/div&gt;&lt;div class=&quot;live-preview&quot;&gt;&lt;/div&gt;&lt;p&gt;Whenever you click on the &amp;quot;Increment&amp;quot; button, you&amp;#x27;ll see that our &lt;strong&gt;function gets created on every render&lt;/strong&gt; of the parent.&lt;/p&gt;&lt;div&gt;&lt;p&gt;Be sure to have your browser&amp;#x27;s console open so you can see the logs.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;the-solution&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-solution&quot; aria-label=&quot;the solution permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The solution&lt;/h2&gt;&lt;p&gt;To prevent this from happening, simply wrap the function in a &lt;code&gt;useCallback&lt;/code&gt; hook.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;diff&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-diff&quot;&gt;DIFF&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-diff&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token deleted-sign deleted prefix deleted&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token deleted-sign deleted line&quot;&gt; const increment = () =&amp;gt; setCount(prevCount =&amp;gt; prevCount + 1)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token deleted-sign deleted line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted line&quot;&gt; const increment = React.useCallback(() =&amp;gt; setCount(prevCount =&amp;gt; prevCount + 1), [setCount])&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The 1&lt;sup&gt;st&lt;/sup&gt; function argument simply takes in our increment function from earlier, while the 2&lt;sup&gt;nd&lt;/sup&gt; argument accepts a dependency array, much like &lt;code&gt;useEffect&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Once you apply these changes to &lt;code&gt;increment&lt;/code&gt;, &lt;em&gt;which I encourage you do in the live editor&lt;/em&gt;, you&amp;#x27;ll notice &lt;strong&gt;the function only gets created once&lt;/strong&gt; (on page load).&lt;/p&gt;&lt;h2 id=&quot;when-to-use&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#when-to-use&quot; aria-label=&quot;when to use permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;When to use&lt;/h2&gt;&lt;p&gt;If you aren&amp;#x27;t familiar with memoization in JavaScript, I recommend you check out 👉 &lt;a href=&quot;/memoization-in-javascript-explained&quot;&gt;my article&lt;/a&gt;. I point out how you shouldn&amp;#x27;t prematurely optimize as it might worsen performance, not improve it. The same principle applies to &lt;code&gt;useCallback&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;So when should you use this hook? In my opinion, when&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Your inline function becomes &lt;em&gt;noticeably&lt;/em&gt; expensive, or&lt;/li&gt;&lt;li&gt;If your function &lt;em&gt;frequently&lt;/em&gt; or &lt;em&gt;unintentionally&lt;/em&gt; triggers a re-render of other hooks and/or components&lt;/li&gt;&lt;/ul&gt;</content:encoded></item><item><title><![CDATA[Memoization In JavaScript Explained]]></title><description><![CDATA[Memoization is a word you'll hear from time to time in JavaScript, especially if you use a library like React. It  simply means memorizing…]]></description><link>https://dilshankelsen.com/memoization-in-javascript-explained/</link><guid isPermaLink="false">https://dilshankelsen.com/memoization-in-javascript-explained/</guid><pubDate>Mon, 07 Jun 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Memoization is a word you&amp;#x27;ll hear from time to time in JavaScript, especially if you use a library like React. It &lt;strong&gt;simply means memorizing&lt;/strong&gt;, aka &lt;em&gt;caching&lt;/em&gt;, the result of a function - the same way your browser 🧭 might cache the results of a GET request.&lt;/p&gt;&lt;p&gt;The benefit is quite straightforward: you improve performance by not running the same process twice. However, as you&amp;#x27;ll soon see, &lt;strong&gt;premature optimization&lt;/strong&gt; isn&amp;#x27;t recommended.&lt;/p&gt;&lt;h2 id=&quot;memoizing-a-function&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#memoizing-a-function&quot; aria-label=&quot;memoizing a function permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Memoizing a function&lt;/h2&gt;&lt;p&gt;Imagine you want to retrieve the &lt;code&gt;h1&lt;/code&gt; title of a page.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getTitle&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;heading&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It could be that you &lt;em&gt;plan on calling&lt;/em&gt; this function many times, so you get the idea to cache the return value.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; cache &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getMemoizedTitle&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;heading&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; cache&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;heading&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; titleValue &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;heading&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  cache&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;heading&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; titleValue&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; titleValue&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The memoized function first checks whether we already computed the result once before. If that is the case, we retrieve it and skip the rest of the function. If not, we proceed to retrieve the &lt;code&gt;h1&lt;/code&gt; title and store it in our &lt;code&gt;cache&lt;/code&gt; object.&lt;/p&gt;&lt;h2 id=&quot;premature-optimization&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#premature-optimization&quot; aria-label=&quot;premature optimization permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Premature optimization&lt;/h2&gt;&lt;p&gt;Just because you &lt;em&gt;can&lt;/em&gt;, doesn&amp;#x27;t mean you &lt;em&gt;should&lt;/em&gt; memoize a function. In fact, &lt;strong&gt;our memoized function is less performant&lt;/strong&gt; than the regular function! 😱&lt;/p&gt;&lt;p&gt;I&amp;#x27;ve even prepared a &lt;a href=&quot;https://codepen.io/dkelsen/pen/zYNwzzr&quot; target=&quot;_blank&quot;&gt;Codepen snippet&lt;/a&gt; which tests the performance of both functions when run twice. What you&amp;#x27;ll notice is that most of the time, our memoized function is slower.&lt;/p&gt;&lt;div&gt;&lt;p&gt;Be sure to perform the Codepen test in a Chromium-based browser as other browsers (to my knowledge) don&amp;#x27;t return time in fractions of a millisecond.&lt;/p&gt;&lt;/div&gt;&lt;h3 id=&quot;the-reason&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-reason&quot; aria-label=&quot;the reason permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The reason&lt;/h3&gt;&lt;p&gt;There&amp;#x27;s always a cost associated with memoization, which stems from the extra lines of code. In our case, the &lt;code&gt;getTitle&lt;/code&gt; function wasn&amp;#x27;t computationally heavy to begin with. Therefore, the extra cost 💸 of optimization was greater than the potential amount of resources saved.&lt;/p&gt;&lt;h2 id=&quot;when-to-use&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#when-to-use&quot; aria-label=&quot;when to use permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;When To Use&lt;/h2&gt;&lt;p&gt;&lt;strong&gt;As a rule of thumb&lt;/strong&gt;, you should only apply memoization on a function that has become &lt;em&gt;noticeably&lt;/em&gt; expensive to perform. Regex calls and recursions are part of that as well.&lt;/p&gt;&lt;p&gt;Likewise, this principle can be applied in JavaScript libraries/frameworks where memoization is prevalent (e.g. React).&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Clear An HTML5 Canvas]]></title><description><![CDATA[How do you clear an entire canvas of its drawings? For that, there's a  context method  called  clearRect . Let me show you: clearRect…]]></description><link>https://dilshankelsen.com/how-to-clear-html5-canvas/</link><guid isPermaLink="false">https://dilshankelsen.com/how-to-clear-html5-canvas/</guid><pubDate>Sat, 05 Jun 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;How do you clear an entire canvas of its drawings? For that, there&amp;#x27;s a &lt;strong&gt;context method&lt;/strong&gt; called &lt;code&gt;clearRect&lt;/code&gt;. Let me show you:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; context &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; canvas&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getContext&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;2d&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;clearRect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; canvas&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; canvas&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;clearRect&lt;/code&gt; takes in 4 arguments:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;The 1&lt;sup&gt;st&lt;/sup&gt; two are the &lt;strong&gt;x and y coordinates&lt;/strong&gt;, respectively. Above, we specified &lt;em&gt;the origin of the canvas&lt;/em&gt;, i.e. the top-left corner.&lt;/li&gt;&lt;li&gt;The last 2 are &lt;strong&gt;width and height&lt;/strong&gt;, respectively, which start from said coordinates. Here, &lt;code&gt;canvas&lt;/code&gt; represents the &lt;em&gt;canvas element&lt;/em&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Given those 4 parameters, &lt;em&gt;anything inside those dimensions&lt;/em&gt; are removed from the canvas.&lt;/p&gt;&lt;h2 id=&quot;trigger-with-button&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#trigger-with-button&quot; aria-label=&quot;trigger with button permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Trigger with button&lt;/h2&gt;&lt;p&gt;Most drawing apps will implement this exact functionality using some sort of &lt;strong&gt;reset button&lt;/strong&gt;. To mimic this, start off by wrapping &lt;code&gt;clearRect&lt;/code&gt; in a function.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;resetCanvas&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;clearRect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; canvas&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; canvas&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then simply attach it to a &lt;em&gt;button&amp;#x27;s&lt;/em&gt; &lt;code&gt;onclick&lt;/code&gt; &lt;em&gt;event&lt;/em&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-name&quot;&gt;onclick&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript function&quot;&gt;resetCanvas&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Reset&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;A working version of this can be found in &lt;a href=&quot;https://codepen.io/dkelsen/pen/WNRqgvq&quot; target=&quot;_blank&quot;&gt;my Codepen&lt;/a&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Cherry-Pick In Git Explained]]></title><description><![CDATA[The command  cherry-pick  allows you to copy a commit from one branch to another. Assume you have the following commit history in branch B…]]></description><link>https://dilshankelsen.com/cherry-pick-in-git-explained/</link><guid isPermaLink="false">https://dilshankelsen.com/cherry-pick-in-git-explained/</guid><pubDate>Thu, 03 Jun 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The command &lt;code&gt;cherry-pick&lt;/code&gt; allows you to copy a commit from one branch to another. Assume you have the following commit history in branch B, which you can retrieve with &lt;code&gt;git log --oneline&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;e061ddd Some commit message&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;e215bb2 Another commit message&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;e0401f2 Bananas&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Back in branch A, you can then copy the 1&lt;sup&gt;st&lt;/sup&gt; commit from branch B with &lt;code&gt;git cherry-pick e061ddd&lt;/code&gt;. This will apply the cherry picked commit as a &lt;em&gt;new commit&lt;/em&gt; to the current branch head. 👤&lt;/p&gt;&lt;h2 id=&quot;options&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#options&quot; aria-label=&quot;options permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Options&lt;/h2&gt;&lt;p&gt;On top of this, &lt;code&gt;cherry-pick&lt;/code&gt; offers a few neat options you might fancy to use.&lt;/p&gt;&lt;h3 id=&quot;multiple-copies&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#multiple-copies&quot; aria-label=&quot;multiple copies permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Multiple copies&lt;/h3&gt;&lt;p&gt;To copy multiple commits, simply append more hashes at the end of the command.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; cherry-pick e061ddd &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;another-commit-hash&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Each cherry-picked commit will be added as a &lt;em&gt;new separate commit&lt;/em&gt; to your branch.&lt;/p&gt;&lt;h3 id=&quot;stage-instead-of-commit&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#stage-instead-of-commit&quot; aria-label=&quot;stage instead of commit permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Stage instead of commit&lt;/h3&gt;&lt;p&gt;Perhaps you want to copy a commit without directly committing it to the current branch? Don&amp;#x27;t worry, we got you covered. 😜&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; cherry-pick e061ddd -n&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;rename-it&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#rename-it&quot; aria-label=&quot;rename it permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Rename it&lt;/h3&gt;&lt;p&gt;Likewise, you can rename a cherry-picked commit with&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; cherry-pick e061ddd -e&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[What is Docker and why use it]]></title><description><![CDATA[Docker is an open-source platform that allows you to package an application along with its technology stack and dependencies into  an…]]></description><link>https://dilshankelsen.com/what-is-docker-and-why-use-it/</link><guid isPermaLink="false">https://dilshankelsen.com/what-is-docker-and-why-use-it/</guid><pubDate>Mon, 31 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Docker is an open-source platform that allows you to package an application along with its technology stack and dependencies into &lt;strong&gt;an isolated container&lt;/strong&gt; that can be deployed and replicated in practically any environment.&lt;/p&gt;&lt;p&gt;Bit of a mouthful, isn&amp;#x27;t it? Don&amp;#x27;t worry, you&amp;#x27;ll get there.&lt;/p&gt;&lt;h2 id=&quot;the-problem-it-solves&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-problem-it-solves&quot; aria-label=&quot;the problem it solves permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The problem it solves&lt;/h2&gt;&lt;p&gt;To properly understand Docker, you need to know why it exists, i.e. what problem it solves. The problem is called &lt;em&gt;Dependency Hell&lt;/em&gt;. 😈&lt;/p&gt;&lt;p&gt;Imagine you&amp;#x27;ve completed your backend web application and you deploy it to a server. Yet, for some reason &lt;strong&gt;it&amp;#x27;s not working as expected&lt;/strong&gt;. Maybe your API is malfunctioning or suddenly you&amp;#x27;re missing a dependency.&lt;/p&gt;&lt;p&gt;&lt;em&gt;But wait... I&amp;#x27;ve installed and configured everything the same way as on my local development environment!&lt;/em&gt;, you proclaim.&lt;/p&gt;&lt;p&gt;Exactly, this is an issue that many developers have and will continuously encounter throughout their career.&lt;/p&gt;&lt;h2 id=&quot;the-difference-to-a-vm&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-difference-to-a-vm&quot; aria-label=&quot;the difference to a vm permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The difference to a VM&lt;/h2&gt;&lt;p&gt;In many ways, Docker is like a virtual machine (VM). However, there&amp;#x27;s one major way in which differ from one another. Let me explain... 😋&lt;/p&gt;&lt;h3 id=&quot;emulation-is-key&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#emulation-is-key&quot; aria-label=&quot;emulation is key permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Emulation is key&lt;/h3&gt;&lt;p&gt;So they both emulate an environment, but the question you should ask yourself is &lt;strong&gt;what environment do they emulate&lt;/strong&gt;?&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:360px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:78.99999999999999%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAAsTAAALEwEAmpwYAAAEH0lEQVQ4y12PXUxTZxjHT7LdLVtiXJbFAibqLiZMsxuS7cLNbRkKGsCEKRYKpbRFWnooBUpLsfTjDLD0g1H5aumHCgH5kA5kYYS52+1ixIyvntKCMBZBGHMObM9pz7P3tDKNF788z/v8f89534MxX2dx4GoxH7glhQyXz2NBPS92tZhH5xchinnReJ+oFJqxGeu88AuhAPVXeDzmq/OHMSgQiKGxBUB3438YHTobWgEIC4DRjHpzorKwM30rcl76oDcBozECk1+chTG8UsGL8DmqFAtapNYU9dTDcjk1J1FQpKyWWqyophak1dQ8YruukUIXUgc+Y2gNQwMBwOWfw2I80bUYekHMaIGYgYV9iRW2VAb4TaKABbwOQgoNkHIVLFfVo6qGbbUROQk3DmGF2PVmiHEFFzC4fDkHlKoNqFMvIcg4SoRaQ4JGS4JW79+W4qGdCjwEWp0f6q8nMuUrrqp+CRQ165CX9wkmzhpNbub6FDcKfJUtBT45C+rlzdwxOZE/IrcUTckEZ3VaFrYn8kdRlnAOfBOCyB/D+RkjhzGJPCiyuwA63AA3XwfNHX0A5o5nYL75LN6zs9edTg+AtSsKZRX+TAyvWhZ098YAEUFEEXQChu5y0rRW/zutM85HieblqLEpQHc56Hj20otFe1xMxN4ZASlOnsPw6mBZN7qhx4t4pToQ3R4GmiyPgDAFodmyBqa2P6HbzcSzA5fdddwCsDtokFaSF7ByYSDnOzOzYfr2b1JZPbtqt4K/zUwtW9ufb9qskdUeO/iNjX+EDI3roe528NtskTU2azPTAeSSSsXsamvTU9LcQq2LSxY/xdIHLckXp/rlX3psNadryg25D4bwjCG38MRHXs4XDmdu9k8DVelEbUM6oWzIeTBY9Vm7M+Pk2VtHM4Zd0pyZu7LTteUGtFubOXm74uP+lnexSwGXsAomQE77QLThBSVMwbX9kTB2J++QaG94WgUzINkdiFMH0yD4Z9B57OdqTgXyVegsXPeAAsZBDpOQveTMxPKCXkENMwnCoCtc8IuNljzuB+ne2O7JaTvnymzHD4W/2qA00BspWewJi0Ju4Ppdjs/nbh8veNgBJXOdtGjFQ5f6nRG+3wmXSPd5LC/kFdeir0se94F41QP47l2QhX37KSMGTtGKZ0YYcELZmhdEIVc855JOz5kF93F+EJ2DvSB+5AHxihtKUZ676MzCshd7v5Hs3QvL6PEtPDrxBKcnnpb+NRTAdKJDJTtDfehXKBk9sYXH7m+iPFa0OWA6dqf+SNm/Yzso20HZdiVz/4kk/P3+xXnHGextW+WbaeNE0imfMSl1RMc5NU4kfzhmfE8NP2JHvep3UocbU9JG9Rw2S72nT0ly1b01BQHsRJ/m/bRRXTI7R3nSB/2aI5hW+MZ/FOM5FO5IFqUAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Layers of an Operation System&quot; title=&quot;Layers of an Operation System&quot; src=&quot;/static/37a54516e4e14e12aedebe71773f997a/f21e7/image-1.png&quot; srcSet=&quot;/static/37a54516e4e14e12aedebe71773f997a/772e8/image-1.png 200w,/static/37a54516e4e14e12aedebe71773f997a/f21e7/image-1.png 360w&quot; sizes=&quot;(max-width: 360px) 100vw, 360px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Sourced from &lt;a href=&quot;https://en.wikipedia.org/wiki/Kernel_(operating_system)&quot; target=&quot;_blank&quot;&gt;Wikipedia&lt;/a&gt;&lt;/figcaption&gt;&lt;p&gt;The image above shows the different layers of a computer.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;The &lt;strong&gt;bottom&lt;/strong&gt; layer represents the hardware, e.g. RAM, CPU and Hard Drive.&lt;/li&gt;&lt;li&gt;The &lt;strong&gt;middle&lt;/strong&gt; represents the core of the Operation System (OS), 🍎 also known as the Kernel. It communicates between your hardware and applications layer.&lt;/li&gt;&lt;li&gt;The &lt;strong&gt;top&lt;/strong&gt; layer represents the software of your computer. This can be &amp;quot;simple&amp;quot; things like a text editor or something more complicated like a file system.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;So what&amp;#x27;s the difference in term of emulation?&lt;/p&gt;&lt;p&gt;Docker only emulates the applications layer of your computer, while a virtual machine emulates the OS Kernel &lt;em&gt;AND&lt;/em&gt; applications layer. Consequently, &lt;strong&gt;Docker containers are much more lightweight&lt;/strong&gt; and more compact than a VM.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Change Line Width Using Slider In HTML5 Canvas]]></title><description><![CDATA[A slider is one of those  cool must-have  features of any drawing app. And it's surprisingly easy to implement! Let's start off with a…]]></description><link>https://dilshankelsen.com/change-line-width-using-slider-in-html5-canvas/</link><guid isPermaLink="false">https://dilshankelsen.com/change-line-width-using-slider-in-html5-canvas/</guid><pubDate>Sat, 29 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A slider is one of those &lt;em&gt;cool must-have&lt;/em&gt; features of any drawing app. And it&amp;#x27;s surprisingly easy to implement! Let&amp;#x27;s start off with a &lt;strong&gt;simple slider&lt;/strong&gt; and some configuration.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;p&gt;Be sure to set the default value of &lt;code&gt;lineWidth&lt;/code&gt; equals to that of the slider. In our example, that would be &lt;code&gt;2&lt;/code&gt;.&lt;/p&gt;&lt;/div&gt;&lt;p&gt;&lt;em&gt;Whenever&lt;/em&gt; the user moves the slider, we want to update the canvas property &lt;code&gt;lineWidth&lt;/code&gt;. For this, we attach a custom function to the &lt;code&gt;onchange&lt;/code&gt; &lt;strong&gt;event attribute&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-name&quot;&gt;onchange&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript function&quot;&gt;changeWidth&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript property-access&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We get the new value by &lt;em&gt;explicitly&lt;/em&gt; passing in &lt;code&gt;this.value&lt;/code&gt;. The function itself is &lt;strong&gt;pretty straightforward&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;changeWidth&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;lineWidth&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; value&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Once again, check out &lt;a href=&quot;https://codepen.io/dkelsen/pen/VwPNKQo&quot; target=&quot;_blank&quot;&gt;my Codepen&lt;/a&gt; for the whole thing in action!&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Different SQL Joins Explained]]></title><description><![CDATA[In a previous blog post, I covered the basics of an  SQL  JOIN  statement . I'd like to further elaborate on the 4 different types of Joins…]]></description><link>https://dilshankelsen.com/different-sql-joins-explained/</link><guid isPermaLink="false">https://dilshankelsen.com/different-sql-joins-explained/</guid><pubDate>Thu, 27 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In a previous blog post, I covered the basics of an &lt;a href=&quot;/joining-two-sql-tables&quot;&gt;SQL &lt;code&gt;JOIN&lt;/code&gt; statement&lt;/a&gt;. I&amp;#x27;d like to further elaborate on the 4 different types of Joins that exist.&lt;/p&gt;&lt;p&gt;The Venn diagrams below give a great overview of how they work, which I recommend you use as a reference to my explanations below.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:738px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:18.999999999999996%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA+0lEQVQI1x2My0rDQABF83GiINIv8C9cqEF84ELdCoIt1IXFghutqS3UiotIJdWFRGmkVGsW9UGmzqtj5pFMx8ELB+65i+tkWfYzhoTQPM+NMXqq2YQRTLX+V61txxArJa0KKRDCGBGLVUdwGX/3R8m7Ejkgn5Vgq3zrfiRDu6c8FVwk8Ktyt33UcWPwcv64X/KXq8FO0V+KQc8x2vj9s/u3ln3qDlrrjYWV2szD8GqqDERjk5to1F2tzW02C9Vgd6+9uObNb1wWXG+2HhYdKSTEAFOkpOIibUfHzacyplBKJW2EZGxy3TtpPJcYp53Xi3p4eBOdeuEB+gV/k5XI49GGFxoAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Venn diagram of different JOIN statements&quot; title=&quot;Venn diagram of different JOIN statements&quot; src=&quot;/static/8f13e9a26895c6deefb8c7f4d6a61cdf/774b6/image-1.png&quot; srcSet=&quot;/static/8f13e9a26895c6deefb8c7f4d6a61cdf/772e8/image-1.png 200w,/static/8f13e9a26895c6deefb8c7f4d6a61cdf/e17e5/image-1.png 400w,/static/8f13e9a26895c6deefb8c7f4d6a61cdf/774b6/image-1.png 738w&quot; sizes=&quot;(max-width: 738px) 100vw, 738px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;&lt;p&gt;  Sourced from &lt;a href=&quot;https://www.w3schools.com/sql/sql_join.asp&quot; target=&quot;_blank&quot;&gt;W3Schools&lt;/a&gt;&lt;/p&gt;&lt;/figcaption&gt;&lt;p&gt;I will also reuse from my previous post the -&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Pets&lt;/strong&gt; and&lt;/p&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Age&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;12&lt;/td&gt;&lt;td&gt;Sandy&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;Odie&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;&lt;strong&gt;Owners&lt;/strong&gt; tables to illustrate each &lt;code&gt;JOIN&lt;/code&gt; statement.&lt;/p&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Pet_Id&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;Dilshan&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;John&lt;/td&gt;&lt;td&gt;NULL&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;div&gt;&lt;p&gt;Be aware that not every SQL database supports a &lt;code&gt;RIGHT JOIN&lt;/code&gt; and &lt;code&gt;FULL OUTER JOIN&lt;/code&gt;.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;inner-join&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#inner-join&quot; aria-label=&quot;inner join permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;INNER JOIN&lt;/h2&gt;&lt;p&gt;Executing a &lt;code&gt;JOIN&lt;/code&gt; statement without specifying the type will always default to an &lt;code&gt;INNER JOIN&lt;/code&gt;. This type will only return rows that have matching values (i.e. &lt;em&gt;pet IDs&lt;/em&gt;) in both tables.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;sql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-sql&quot;&gt;SQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-sql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Pets &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Owners &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;ON&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Pets&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Id&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Owners&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Pet_Id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Query output:&lt;/p&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Age&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Pet_Id&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;Odie&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;Dilshan&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h2 id=&quot;left-join&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#left-join&quot; aria-label=&quot;left join permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;LEFT JOIN&lt;/h2&gt;&lt;p&gt;In a &lt;code&gt;LEFT JOIN&lt;/code&gt;, the entire left-hand table is included even if a row doesn&amp;#x27;t have a match. Consequently, &lt;code&gt;NULL&lt;/code&gt; values are used on the right-hand side to represent this missing match.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;sql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-sql&quot;&gt;SQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-sql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Pets &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;LEFT&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Owners &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;ON&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Pets&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Id&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Owners&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Pet_Id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Query output:&lt;/p&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Age&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Pet_Id&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;12&lt;/td&gt;&lt;td&gt;Sandy&lt;/td&gt;&lt;td&gt;NULL&lt;/td&gt;&lt;td&gt;NULL&lt;/td&gt;&lt;td&gt;NULL&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;Odie&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;Dilshan&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h2 id=&quot;right-join&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#right-join&quot; aria-label=&quot;right join permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;RIGHT JOIN&lt;/h2&gt;&lt;p&gt;A &lt;code&gt;RIGHT JOIN&lt;/code&gt; is identical to a &lt;code&gt;LEFT JOIN&lt;/code&gt;, but only reversed. That means the entire right-hand table is included along with matching (empty) rows on the left-hand side.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;sql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-sql&quot;&gt;SQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-sql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Pets &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;RIGHT&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Owners &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;ON&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Pets&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Id&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Owners&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Pet_Id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Query output:&lt;/p&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Age&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Pet_Id&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;Odie&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;Dilshan&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;NULL&lt;/td&gt;&lt;td&gt;NULL&lt;/td&gt;&lt;td&gt;NULL&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;John&lt;/td&gt;&lt;td&gt;NULL&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h2 id=&quot;full-outer-join&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#full-outer-join&quot; aria-label=&quot;full outer join permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;FULL OUTER JOIN&lt;/h2&gt;&lt;p&gt;A &lt;code&gt;FULL OUTER JOIN&lt;/code&gt;, or simply &lt;code&gt;OUTER JOIN&lt;/code&gt;, is basically a combination of the previous two. Here, all the records on both tables are included along with empty rows for any missing matches.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;sql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-sql&quot;&gt;SQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-sql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Pets &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;OUTER&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Owners &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;ON&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Pets&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Id&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Owners&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Pet_Id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Query output:&lt;/p&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Age&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Pet_Id&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;12&lt;/td&gt;&lt;td&gt;Sandy&lt;/td&gt;&lt;td&gt;NULL&lt;/td&gt;&lt;td&gt;NULL&lt;/td&gt;&lt;td&gt;NULL&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;Odie&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;Dilshan&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;NULL&lt;/td&gt;&lt;td&gt;NULL&lt;/td&gt;&lt;td&gt;NULL&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;John&lt;/td&gt;&lt;td&gt;NULL&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;</content:encoded></item><item><title><![CDATA[How To Squash Existing Commits Into One]]></title><description><![CDATA[Git allows you to easily change any commit history and this includes merging multiple commits. Assume you want to merge your last 3. To…]]></description><link>https://dilshankelsen.com/squash-existing-commits-into-one/</link><guid isPermaLink="false">https://dilshankelsen.com/squash-existing-commits-into-one/</guid><pubDate>Mon, 24 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Git allows you to easily change any commit history and this includes merging multiple commits. Assume you want to merge your last 3.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; rebase -i HEAD~3&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To achieve this goal, use the &lt;code&gt;rebase&lt;/code&gt; command, which simply allows us to reapply our commits. &lt;code&gt;-i&lt;/code&gt; makes the whole process interactive 🕹 and &lt;code&gt;HEAD~3&lt;/code&gt; represents the 3 latest commits.&lt;/p&gt;&lt;p&gt;Run the command and you&amp;#x27;ll be greeted by the following:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;pick e061ddd Some commit message&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;pick e215bb2 Another commit message&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;pick e0401f2 Bananas&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# Rebase 1b15b90..e0401f2 onto 1b15b90 (3 commands)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# Commands:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# p, pick &amp;lt;commit&amp;gt; = use commit&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# r, reword &amp;lt;commit&amp;gt; = use commit, but edit the commit message&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# e, edit &amp;lt;commit&amp;gt; = use commit, but stop for amending&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# s, squash &amp;lt;commit&amp;gt; = use commit, but meld into previous commit&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# [...]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;p&gt;Your IDE may default to a text editor like &lt;a href=&quot;https://en.wikipedia.org/wiki/Vim_(text_editor)&quot; target=&quot;_blank&quot;&gt;Vim&lt;/a&gt; or &lt;a href=&quot;https://en.wikipedia.org/wiki/Emacs&quot; target=&quot;_blank&quot;&gt;Emacs&lt;/a&gt;. If you aren&amp;#x27;t familiar with the editor, you will need to look up the pertinent keystrokes to interact with the text.&lt;/p&gt;&lt;/div&gt;&lt;p&gt;To merge several commits, squash everything into the most recent one. I also prefer to reword the latter to a more descriptive title. You can achieve this by changing the word in front of each commit message to the correct command (&lt;em&gt;as shown in the legend above&lt;/em&gt;).&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;reword e061ddd Some commit message&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;squash e215bb2 Another commit message&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;squash e0401f2 Bananas&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;After you apply those changes, you will see another wall of text. 📃&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Some commit message&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# Please enter the commit message for your changes. Lines starting&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# with &amp;#x27;#&amp;#x27; will be ignored, and an empty message aborts the commit.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# [...]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here you may rename your commit to something like &lt;code&gt;Many commits and bananas&lt;/code&gt;. Apply the change and you&amp;#x27;ll be greeted by yet some more text.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# This is a combination of 3 commits.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# This is the 1st commit message:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Many commits and bananas&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# This is the commit message #2:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Another commit message&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# This is the commit message #3:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Bananas&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# Please enter the commit message for your changes. Lines starting&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# with &amp;#x27;#&amp;#x27; will be ignored, and an empty message aborts the commit.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# [...]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I usually comment out all the remaining commit messages with a &lt;code&gt;#&lt;/code&gt; as I already reworded the first message to something more descriptive. Of course, you may keep them if you prefer a more detailed commit history.&lt;/p&gt;&lt;p&gt;Apply the changes and Bob&amp;#x27;s your uncle! 👨‍🦳&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Change Drawing Colors In HTML5 Canvas]]></title><description><![CDATA[Changing colors of a stroke isn't 🚀 rocket science. All you have to do is  set the   strokeStyle   property  to a color of your choosing…]]></description><link>https://dilshankelsen.com/change-drawing-colors-in-html5-canvas/</link><guid isPermaLink="false">https://dilshankelsen.com/change-drawing-colors-in-html5-canvas/</guid><pubDate>Sat, 22 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Changing colors of a stroke isn&amp;#x27;t 🚀 rocket science. All you have to do is &lt;strong&gt;set the&lt;/strong&gt; &lt;code&gt;strokeStyle&lt;/code&gt; &lt;strong&gt;property&lt;/strong&gt; to a color of your choosing.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; context &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; canvas&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getContext&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;2d&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;strokeStyle&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;red&amp;#x27;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;However, let&amp;#x27;s keep things &lt;em&gt;interesting&lt;/em&gt; by adding some buttons that change the color for you.&lt;/p&gt;&lt;h2 id=&quot;adding-color-buttons&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#adding-color-buttons&quot; aria-label=&quot;adding color buttons permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Adding color buttons&lt;/h2&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Black&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Blue&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Red&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:300px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:13.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAADCAYAAACTWi8uAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAg0lEQVQI122OPQoEIQxGvf+JLAQRCwsv4Bl0hkFt/M/ypVh2YQJBk5enEWstqrXSGIMQc05C7zfBcb5xsHMO95ECQ8YYklKS1ppCCCz23lnYe5NSimecc38ywlrL3HvPtYCYc6bruijGSKUUfgwCtm6tMU8p0X3f34/AcceGcJ/n4foDwnnkPcVyQnIAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Buttons for black, red and blue&quot; title=&quot;Buttons for black, red and blue&quot; src=&quot;/static/5784dc0d19e6c6e4e49ddc5c75d3bf31/5a46d/image-1.png&quot; srcSet=&quot;/static/5784dc0d19e6c6e4e49ddc5c75d3bf31/772e8/image-1.png 200w,/static/5784dc0d19e6c6e4e49ddc5c75d3bf31/5a46d/image-1.png 300w&quot; sizes=&quot;(max-width: 300px) 100vw, 300px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;404: Styling not found! 😆&lt;/figcaption&gt;&lt;p&gt;Since we want to trigger a color change upon clicking a button, we have to &lt;strong&gt;listen to the&lt;/strong&gt; &lt;code&gt;click&lt;/code&gt; &lt;strong&gt;event&lt;/strong&gt; with a function called &lt;code&gt;changeColor&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-name&quot;&gt;onclick&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript function&quot;&gt;changeColor&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript string&quot;&gt;&amp;quot;black&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Black&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-name&quot;&gt;onclick&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript function&quot;&gt;changeColor&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript string&quot;&gt;&amp;quot;blue&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Blue&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-name&quot;&gt;onclick&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript function&quot;&gt;changeColor&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript string&quot;&gt;&amp;quot;red&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value value javascript language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag special-attr attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Red&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then, in our function, we simply use the &lt;em&gt;passed in&lt;/em&gt; color value and assign it to the context.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;changeColor&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;strokeStyle&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; color&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can check out a working version of it &lt;a href=&quot;https://codepen.io/dkelsen/pen/eYgXbvM&quot; target=&quot;_blank&quot;&gt;right here&lt;/a&gt;!&lt;/p&gt;</content:encoded></item><item><title><![CDATA[What Is Currying In JavaScript]]></title><description><![CDATA[I like to describe currying as nested functions that can be sequentially called  whenever  you like to. You may in fact have already…]]></description><link>https://dilshankelsen.com/what-is-currying-in-javascript/</link><guid isPermaLink="false">https://dilshankelsen.com/what-is-currying-in-javascript/</guid><pubDate>Thu, 20 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;I like to describe currying as nested functions that can be sequentially called &lt;em&gt;whenever&lt;/em&gt; you like to. You may in fact have already encountered some so far. This is what they look like:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;someFunction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;some&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;string&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;parameters&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;p&gt;Currying is a concept that can be applied not only to JavaScript, but any programming language in general.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;nested-functions&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#nested-functions&quot; aria-label=&quot;nested functions permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Nested functions&lt;/h2&gt;&lt;p&gt;Any function that takes in 2 or more parameters can be recreated as a curried 🍛 function. Take this simple addition function.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; b&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// 5&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To transform it into a curried function, apply the following changes:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; b&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// 5&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You&amp;#x27;ll notice that a nested function is called by appending parentheses (with or without a parameter) onto the outer function call, e.g. &lt;code&gt;someFunction()()&lt;/code&gt;. Naturally, you&amp;#x27;ll call and pass down any parameters to the nested functions in the order that they are defined.&lt;/p&gt;&lt;h2 id=&quot;call-it-whenever-you-like&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#call-it-whenever-you-like&quot; aria-label=&quot;call it whenever you like permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Call it whenever you like&lt;/h2&gt;&lt;p&gt;Remember how I said that a curried function &amp;quot;&lt;em&gt;can be sequentially called whenever you like&lt;/em&gt;&amp;quot;? This is where they really shine! ✨&lt;/p&gt;&lt;p&gt;You should always keep in mind that a curried function will return another function until you call the most inner nested one. Let&amp;#x27;s continue with our previous example.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; nestedFunction &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;nestedFunction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// function add(b)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If we call only the outer layer of our &lt;code&gt;add&lt;/code&gt; function, it returns the nested function we defined earlier. Of course, you have to store the function somewhere, like &lt;code&gt;nestedFunction&lt;/code&gt; in our example, or else you won&amp;#x27;t be able to call it afterwards.&lt;/p&gt;&lt;p&gt;Now... you could theoretically take a break, drink some coffee ☕️ and complete the curried function 15 minutes later and still receive the same output as before.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;nestedFunction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// 5&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And this is why one would use a curried function. Not so you can go on a break, 😆 but because you don&amp;#x27;t need all the parameters upfront to call a function.&lt;/p&gt;&lt;p&gt;You might have the 1&lt;sup&gt;st&lt;/sup&gt; parameter ready, but it might take some time (and processing) until your 2&lt;sup&gt;nd&lt;/sup&gt;, or 3&lt;sup&gt;rd&lt;/sup&gt; parameter is available.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Joining Two SQL Tables]]></title><description><![CDATA[The  JOIN  statement allows you to combine the data from 2 or more tables. Imagine you've got two tables: A  Pets  table, and Id Age Name…]]></description><link>https://dilshankelsen.com/joining-two-sql-tables/</link><guid isPermaLink="false">https://dilshankelsen.com/joining-two-sql-tables/</guid><pubDate>Mon, 17 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The &lt;code&gt;JOIN&lt;/code&gt; statement allows you to combine the data from 2 or more tables. Imagine you&amp;#x27;ve got two tables:&lt;/p&gt;&lt;p&gt;A &lt;strong&gt;Pets&lt;/strong&gt; table, and&lt;/p&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Age&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;12&lt;/td&gt;&lt;td&gt;Sandy&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;Odie&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;An &lt;strong&gt;Owners&lt;/strong&gt; table.&lt;/p&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Pet_Id&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;Dilshan&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;John&lt;/td&gt;&lt;td&gt;NULL&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;Suddenly, you got the brilliant 🤓 idea to combine both tables to view pet and owner data in a single place. So you execute the following command in your query window:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;sql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-sql&quot;&gt;SQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-sql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Pets&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Owners&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;ON&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; Pets&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Id&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Owners&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Pet_Id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This outputs:&lt;/p&gt;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Age&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Pet_Id&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;Odie&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;Dilshan&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h2 id=&quot;what-happened&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#what-happened&quot; aria-label=&quot;what happened permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;What happened?&lt;/h2&gt;&lt;p&gt;This is the way I like to go about &lt;code&gt;JOIN&lt;/code&gt; statements:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Decide on a &lt;strong&gt;&amp;quot;base&amp;quot; table&lt;/strong&gt; - &lt;code&gt;SELECT * FROM Pets&lt;/code&gt;. For instance, you may think to yourself: &amp;quot;&lt;em&gt;Who are the owners of these pets?&lt;/em&gt;&amp;quot;.&lt;/li&gt;&lt;li&gt;Add the data from your &lt;strong&gt;&amp;quot;target&amp;quot; table&lt;/strong&gt; - &lt;code&gt;JOIN Owners&lt;/code&gt;.&lt;/li&gt;&lt;li&gt;Find the &lt;strong&gt;column that links&lt;/strong&gt; both tables - &lt;code&gt;ON Pets.Id=Owners.Pet_Id&lt;/code&gt;. This is often an id (by design) and in this case as well (&lt;em&gt;Pet id&lt;/em&gt;).&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;By default, a &lt;code&gt;JOIN&lt;/code&gt; statement defaults to an &lt;code&gt;INNER JOIN&lt;/code&gt; statement. This means only rows that have matching &lt;em&gt;Pet Id&lt;/em&gt; values in both tables will be returned. Notice how pets that do not have an owner (😭) and owners that do not have a pet weren&amp;#x27;t included.&lt;/p&gt;&lt;p&gt;SQL Joins can grow in complexity depending on what data you&amp;#x27;re looking for and the type of &lt;code&gt;JOIN&lt;/code&gt; statement you&amp;#x27;re using. Concerning the latter, I plan on writing a blog post soon.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Dragging A Mouse Outside Of An HTML5 Canvas]]></title><description><![CDATA[In an earlier post, I showed you how to create some  basic drawing functionality  using a canvas. However, there's  one edge case  that I…]]></description><link>https://dilshankelsen.com/dragging-mouse-outside-html5-canvas/</link><guid isPermaLink="false">https://dilshankelsen.com/dragging-mouse-outside-html5-canvas/</guid><pubDate>Sat, 15 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In an earlier post, I showed you how to create some &lt;a href=&quot;/drawing-on-html5-canvas-using-mouse&quot;&gt;basic drawing functionality&lt;/a&gt; using a canvas. However, there&amp;#x27;s &lt;strong&gt;one edge case&lt;/strong&gt; that I didn&amp;#x27;t cover: dragging your mouse outside and back into the canvas.&lt;/p&gt;&lt;h2 id=&quot;current-issues&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#current-issues&quot; aria-label=&quot;current issues permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Current issues&lt;/h2&gt;&lt;p&gt;In the &lt;a href=&quot;https://codepen.io/dkelsen/pen/bGgzqXK&quot; target=&quot;_blank&quot;&gt;current implementation&lt;/a&gt;, the &lt;strong&gt;mouse release outside&lt;/strong&gt; the canvas &lt;em&gt;isn&amp;#x27;t being registered&lt;/em&gt;, i.e. &lt;code&gt;isDrawing&lt;/code&gt; remains &lt;code&gt;true&lt;/code&gt;. And on top of that, once the mouse returns to the canvas, &lt;strong&gt;our drawing reconnects&lt;/strong&gt; with our previous drawing.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:182px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:91.20879120879121%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAYAAABb0P4QAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB4klEQVQ4y52TSW7qQBCGfYscJgfIOXKwl11WyS4LpCxYIZCYB8N7oJjBOIAdkM08+3/+22nUBidEKalQNa7++q+qbg2BjUYjlMtl6LqOWq12cq7/NZswjTdYExfNdgd15ih5zKlUKuh2u0RB2+/3SKVSAjoYDCI+fLdgrzZwnx7g3d/B/qtj6HxgeJbHvclkEofDAZplWTAMA99avQDc3gB67uuUeh3j8RgaYVIuT/B9P+rHI/zgm28a8F8ew1jJOwbfaSx7tVpBa7fbJyA/XgBV6PMf+B+jMP7MlcBMJhOWPJlMUK1W8SN7fQacwcXfBOfz+RAo699ut8Idx4HneSLe7XaYzWZwXReLxQLeex+L5VLE8/kcyyDmUCmKt0RMmT/r9VokMSGRSCCdTgsQ4Twsm82iVCqhFsSNRkOsCaAqDoJz6PV6IZBy40qgH2KaT0V0uabSQqFw2qepAOlxcDkw1VhFsVjEZrOJB14D0Njbfr8vQFTGtdx36qEKOzcxjGBIrVZL9I39awbPcTqdRqqIKDyH8SnxnVJBLpcTl9Y0zQhEvbdqZRGFHAKnSSW2bYvpxw0sDiRNkyfRWEqn07lQob6gOIhqESDLZLnnz/Cr/l4FsvG8zNdUXAXGlfIbGO0/YA5nZADhMNMAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Bug dragging mouse back into canvas&quot; title=&quot;Bug dragging mouse back into canvas&quot; src=&quot;/static/3b36fce887e01d7259f1b0c7d3f8560a/a51ee/image-1.png&quot; srcSet=&quot;/static/3b36fce887e01d7259f1b0c7d3f8560a/a51ee/image-1.png 182w&quot; sizes=&quot;(max-width: 182px) 100vw, 182px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Dragging mouse back into canvas&lt;/figcaption&gt;&lt;h2 id=&quot;mouse-release&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#mouse-release&quot; aria-label=&quot;mouse release permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Mouse release&lt;/h2&gt;&lt;p&gt;We are currently &lt;strong&gt;constraining the&lt;/strong&gt; &lt;code&gt;mouseup&lt;/code&gt; &lt;strong&gt;and&lt;/strong&gt; &lt;code&gt;mousedown&lt;/code&gt; &lt;strong&gt;events&lt;/strong&gt; to the canvas itself. We should change this to the &lt;em&gt;entire web page&lt;/em&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token dom variable&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;mousedown&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; startDrawing&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;mouseup&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; stopDrawing&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;canvas&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;mousemove&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; draw&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We can leave the last listener as is since we don&amp;#x27;t really care too much about mouse movements outside of the canvas.&lt;/p&gt;&lt;h2 id=&quot;connecting-points&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#connecting-points&quot; aria-label=&quot;connecting points permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Connecting points&lt;/h2&gt;&lt;p&gt;To prevent the &lt;em&gt;connecting&lt;/em&gt; bug I outlined earlier, we need to &lt;strong&gt;tell the canvas that we&amp;#x27;re starting a new drawing&lt;/strong&gt; whenever the mouse enters the canvas.&lt;/p&gt;&lt;p&gt;There&amp;#x27;s a &lt;code&gt;mouseover&lt;/code&gt; event that allows us to do that.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;enterCanvas&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;beginPath&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;canvas&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;mouseover&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; enterCanvas&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The improved functionality can be viewed in &lt;a href=&quot;https://codepen.io/dkelsen/pen/MWJLXzw&quot; target=&quot;_blank&quot;&gt;this Codepen&lt;/a&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Convert A File To A Byte Array]]></title><description><![CDATA[Let's say you've got a file (e.g. image) that you want to send to a server via REST API. And for that you wish to first convert the file…]]></description><link>https://dilshankelsen.com/convert-file-to-byte-array/</link><guid isPermaLink="false">https://dilshankelsen.com/convert-file-to-byte-array/</guid><pubDate>Thu, 13 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Let&amp;#x27;s say you&amp;#x27;ve got a file (e.g. image) that you want to send to a server via REST API.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; inputNode &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;upload_file&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; selectedFile &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; inputNode&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;files&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And for that you wish to first convert the file into a byte array. To start off, define an asynchronous function called &lt;code&gt;getAsByteArray&lt;/code&gt; that returns it as such. Specifically, it returns a &lt;code&gt;Uint8Array&lt;/code&gt; object.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getAsByteArray&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Uint8Array&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;readFile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To instantiate the object, the constructor accepts a number of &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/Uint8Array#syntax&quot; target=&quot;_blank&quot;&gt;different parameters&lt;/a&gt;. The one we care about is a &lt;code&gt;buffer&lt;/code&gt;, i.e. an &lt;code&gt;ArrayBuffer&lt;/code&gt; object. We can pass our file as one to the constructor with the help of the &lt;code&gt;FileReader&lt;/code&gt; API.&lt;/p&gt;&lt;p&gt;Let&amp;#x27;s do that in a new function called &lt;code&gt;readFile&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;readFile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; reject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Create file reader&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; reader &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;FileReader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Register event listeners&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    reader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;loadend&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;resolve&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    reader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;error&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; reject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Read file&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    reader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;readAsArrayBuffer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This function returns a Promise that will resolve once the file as has been fully read from the &lt;code&gt;readAsArrayBuffer()&lt;/code&gt; function. As this is an asynchronous operation, once done, it triggers a &lt;code&gt;loadend&lt;/code&gt; event, hence why we&amp;#x27;ve added an event listener to the &lt;code&gt;reader&lt;/code&gt; object.&lt;/p&gt;&lt;p&gt;Now that everything&amp;#x27;s set up, converting your file to a byte array is as easy as:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; byteFile &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getAsByteArray&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Properly Use JSON Web Tokens]]></title><description><![CDATA[In a previous blog post, I explained what  JSON Web Tokens  are. However, when developers are first introduced to JWTs, they incorrectly…]]></description><link>https://dilshankelsen.com/how-to-properly-use-json-web-tokens/</link><guid isPermaLink="false">https://dilshankelsen.com/how-to-properly-use-json-web-tokens/</guid><pubDate>Mon, 10 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In a previous blog post, I explained what &lt;a href=&quot;/introduction-json-web-tokens&quot;&gt;JSON Web Tokens&lt;/a&gt; are. However, when developers are first introduced to JWTs, they incorrectly assume that it can replace the battle-tested session ID in every aspect. The reality: it&amp;#x27;s only suitable for very specific use cases.&lt;/p&gt;&lt;h2 id=&quot;when-to-use&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#when-to-use&quot; aria-label=&quot;when to use permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;When to use&lt;/h2&gt;&lt;p&gt;Let&amp;#x27;s get straight to the point. 😜 You should only use JWTs for:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;One-time and/or short-lived (e.g. 15min) authorization, and&lt;/li&gt;&lt;li&gt;Server-to-server communication&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Regarding the 1&lt;sup&gt;st&lt;/sup&gt; point, imagine that Server A issues a JWT that allows you to fetch some data from Server B for a limited time only. And as an example for the 2&lt;sup&gt;nd&lt;/sup&gt; point, using JWTs for API authorization is quite common. Even Google does it!&lt;/p&gt;&lt;h2 id=&quot;security-issues&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#security-issues&quot; aria-label=&quot;security issues permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Security issues&lt;/h2&gt;&lt;p&gt;Security is practically THE reason JWTs will never replace a session ID.&lt;/p&gt;&lt;h3 id=&quot;storage-methods&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#storage-methods&quot; aria-label=&quot;storage methods permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Storage methods&lt;/h3&gt;&lt;p&gt;Local storage is often the go-to method for JWTs, even though it shouldn&amp;#x27;t be. It exposes your token to a multitude of attacks as it can be accessed by anyone using JavaScript. If there&amp;#x27;s one thing you don&amp;#x27;t want to bet against, it&amp;#x27;s the creativity of a hacker. &lt;/p&gt;&lt;p&gt;For this reason alone you &lt;em&gt;should&lt;/em&gt; use an &lt;code&gt;httponly&lt;/code&gt; cookie as it prevents anyone from accessing it, even you! But of course, this introduces a host of new issues as you are no longer able to access it on the client side... 💩&lt;/p&gt;&lt;h3 id=&quot;revoking-authorization&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#revoking-authorization&quot; aria-label=&quot;revoking authorization permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Revoking authorization&lt;/h3&gt;&lt;p&gt;If for some reason or another an account gets compromised by a hacker, how are you going to revoke that users&amp;#x27; session? The short answer: &lt;em&gt;you can&amp;#x27;t&lt;/em&gt;. You&amp;#x27;ll simply need to wait until the token expires. The longer answer: &lt;em&gt;you can by logging out everyone&lt;/em&gt; in your application as a result of changing your secret key. However, that&amp;#x27;s not exactly a desired solution for obvious reasons.&lt;/p&gt;&lt;h3 id=&quot;logged-in-users&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#logged-in-users&quot; aria-label=&quot;logged in users permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Logged in users&lt;/h3&gt;&lt;p&gt;Speaking of logging out, how do you know who&amp;#x27;s logged in in the first place? I think you already know the answer 😉 (P.S. &lt;em&gt;You can&amp;#x27;t know&lt;/em&gt;).&lt;/p&gt;&lt;h3 id=&quot;stale-data&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#stale-data&quot; aria-label=&quot;stale data permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Stale data&lt;/h3&gt;&lt;p&gt;Similar to revoking a token, it&amp;#x27;s impossible to change the payload of your token until the current one expires. Naturally, this will lead to an outdated payload which will then give rise to a host of other problems. For instance, image you downgraded a user&amp;#x27;s role from &amp;quot;admin&amp;quot; to &amp;quot;regular&amp;quot;. Sadly, this change won&amp;#x27;t be reflected in the token.&lt;/p&gt;&lt;h3 id=&quot;exposed-user-data&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#exposed-user-data&quot; aria-label=&quot;exposed user data permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Exposed user data&lt;/h3&gt;&lt;p&gt;Another flaw of a JWT is the lack of encryption of its payload. Literally anyone who has access to the token will be able to read its contents by &lt;em&gt;base64url&lt;/em&gt; decoding it. Granted, you may apply another layer of security by encrypting the payload itself.&lt;/p&gt;&lt;h2 id=&quot;bandwidth-consumption&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#bandwidth-consumption&quot; aria-label=&quot;bandwidth consumption permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Bandwidth consumption&lt;/h2&gt;&lt;p&gt;Another minor, though not insignificant downside of a JWT is its size. It will certainly exceed that of a cookie session. And over the long-run, this can make a difference in bandwidth consumption as it is sent with every request. So yeah, size does matter. 😆&lt;/p&gt;&lt;h2 id=&quot;further-readings&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#further-readings&quot; aria-label=&quot;further readings permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Further readings&lt;/h2&gt;&lt;p&gt;There is certainly more extensive content out there about the use cases of JWT. If you&amp;#x27;re interested in reading up more on this, I recommend &lt;a href=&quot;http://cryto.net/~joepie91/blog/2016/06/13/stop-using-jwt-for-sessions/&quot; target=&quot;_blank&quot;&gt;part 1&lt;/a&gt; and &lt;a href=&quot;http://cryto.net/~joepie91/blog/2016/06/19/stop-using-jwt-for-sessions-part-2-why-your-solution-doesnt-work/&quot; target=&quot;_blank&quot;&gt;part 2&lt;/a&gt; of joepie91&amp;#x27;s Ramblings about JWTs.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Drawing On An HTML Canvas Using A Mouse]]></title><description><![CDATA[Once you understand the  basics of an HTML canvas , adding draw functionality isn't that difficult. But don't worry, I'll still show you how…]]></description><link>https://dilshankelsen.com/drawing-on-html5-canvas-using-mouse/</link><guid isPermaLink="false">https://dilshankelsen.com/drawing-on-html5-canvas-using-mouse/</guid><pubDate>Sat, 08 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Once you understand the &lt;a href=&quot;/introduction-to-html5-canvas&quot;&gt;basics of an HTML canvas&lt;/a&gt;, adding draw functionality isn&amp;#x27;t that difficult. But don&amp;#x27;t worry, I&amp;#x27;ll still show you how to do it &lt;strong&gt;step by step&lt;/strong&gt; even if it seems out of reach to you. 🙌&lt;/p&gt;&lt;h2 id=&quot;mouse-actions&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#mouse-actions&quot; aria-label=&quot;mouse actions permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Mouse actions&lt;/h2&gt;&lt;p&gt;One part of the equation is knowing how to respond to user input. When drawing, there are &lt;strong&gt;only 3 possible actions&lt;/strong&gt;:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Pressing a mouse button&lt;/li&gt;&lt;li&gt;Moving the mouse&lt;/li&gt;&lt;li&gt;Releasing the mouse button&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Consequently, we will &lt;strong&gt;implement 3 listeners&lt;/strong&gt; 👂 that will each listen to one of these actions.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;canvas&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;mousedown&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; startDrawing&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;canvas&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;mouseup&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; stopDrawing&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;canvas&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;mousemove&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; draw&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I&amp;#x27;ve already added the associated functions which I&amp;#x27;m about to go through.&lt;/p&gt;&lt;h2 id=&quot;track-state&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#track-state&quot; aria-label=&quot;track state permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Track state&lt;/h2&gt;&lt;p&gt;Let&amp;#x27;s first get the issue of state out of the way, specifically &lt;strong&gt;when the drawing should be done&lt;/strong&gt;. Think about it... 🤔 &lt;/p&gt;&lt;p&gt;The user might move his mouse and &lt;em&gt;not intend to draw&lt;/em&gt;. For that, we need to know if any mouse movements by the user should be committed to the canvas. We can do that with the help of a simple &lt;strong&gt;boolean variable&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; isDrawing &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;start-drawing&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#start-drawing&quot; aria-label=&quot;start drawing permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Start drawing&lt;/h2&gt;&lt;p&gt;The user clicks on a mouse button 🖱 which triggers &lt;code&gt;startDrawing&lt;/code&gt;. We &lt;em&gt;now&lt;/em&gt; know that &lt;strong&gt;the user intends to draw&lt;/strong&gt; something, so we set &lt;code&gt;isDrawing&lt;/code&gt; to &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;startDrawing&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  isDrawing &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;beginPath&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;moveTo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;clientX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; event&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;clientY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We then &lt;em&gt;tell the context&lt;/em&gt; that we are starting to draw with &lt;code&gt;beginPath&lt;/code&gt; and we add our first point with &lt;code&gt;moveTo&lt;/code&gt;. The &lt;em&gt;x and y coordinates&lt;/em&gt; of the point can be retrieved through the event object.&lt;/p&gt;&lt;h2 id=&quot;moving-the-mouse&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#moving-the-mouse&quot; aria-label=&quot;moving the mouse permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Moving the mouse&lt;/h2&gt;&lt;p&gt;As I mentioned in the &lt;a href=&quot;#track-state&quot;&gt;state section&lt;/a&gt;, we need to know whether the user intends to draw when he&amp;#x27;s moving the mouse. So for that, we simply &lt;strong&gt;quit 🚪 the function early if&lt;/strong&gt; &lt;code&gt;isDrawing&lt;/code&gt; is set to &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;draw&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;isDrawing&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;lineTo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;clientX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; event&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;clientY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;stroke&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;If drawing was the intention&lt;/strong&gt;, we add a new point to the canvas context with &lt;code&gt;lineTo&lt;/code&gt;. Then we make the distance between this point and the previous one visible with &lt;code&gt;stroke&lt;/code&gt;. &lt;/p&gt;&lt;p&gt;Note that &lt;em&gt;the previous point&lt;/em&gt; will either come from &lt;code&gt;startDrawing&lt;/code&gt; at the very start or &lt;code&gt;draw&lt;/code&gt; afterwards.&lt;/p&gt;&lt;h2 id=&quot;stop-drawing&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#stop-drawing&quot; aria-label=&quot;stop drawing permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Stop drawing&lt;/h2&gt;&lt;p&gt;Last but not least, 😌 when the user releases the mouse button, we simply set &lt;code&gt;isDrawing&lt;/code&gt; back to false.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;stopDrawing&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  isDrawing &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can inspect the functioning code in &lt;a href=&quot;https://codepen.io/dkelsen/pen/bGgzqXK&quot; target=&quot;_blank&quot;&gt;my Codepen&lt;/a&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Get URL Parameters Using JavaScript]]></title><description><![CDATA[First of all, it's important to distinguish between: Query parameters : Key value pairs that follow a  ?  or  &  (e.g.  ?key=value ), and…]]></description><link>https://dilshankelsen.com/get-url-parameters-using-javascript/</link><guid isPermaLink="false">https://dilshankelsen.com/get-url-parameters-using-javascript/</guid><pubDate>Thu, 06 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;First of all, it&amp;#x27;s important to distinguish between:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Query parameters&lt;/strong&gt;: Key value pairs that follow a &lt;code&gt;?&lt;/code&gt; or &lt;code&gt;&amp;amp;&lt;/code&gt; (e.g. &lt;code&gt;?key=value&lt;/code&gt;), and&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Path parameters&lt;/strong&gt;: Commonly known as slugs or permalinks (e.g. &lt;code&gt;/param1/param2&lt;/code&gt;)&lt;/li&gt;&lt;/ul&gt;&lt;h2 id=&quot;query-parameters&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#query-parameters&quot; aria-label=&quot;query parameters permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Query parameters&lt;/h2&gt;&lt;p&gt;For query parameters, JavaScript offers a built-in interface called &lt;code&gt;URLSearchParams&lt;/code&gt; that allows you to easily retrieve them. One initializes the interface by passing a string of the parameters to it.&lt;/p&gt;&lt;p&gt;As an example, let&amp;#x27;s assume we have the following link which filters through blog posts: &lt;code&gt;https://dilshankelsen.com/blog/posts?year=2021&amp;amp;category=javascript&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; queryString &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;location&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;search&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;queryString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// &amp;#x27;?year=2021&amp;amp;category=javascript&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; urlParams &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;URLSearchParams&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;queryString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As you see, we use the built-in property from the window object to extract all query parameters as a string, which we then use to instantiate a new &lt;code&gt;URLSearchParams&lt;/code&gt; object. To then retrieve the values for each parameter is as easy as:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; category &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; urlParams&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;category&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;category&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// &amp;#x27;javascript&amp;#x27;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;path-parameters&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#path-parameters&quot; aria-label=&quot;path parameters permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Path parameters&lt;/h2&gt;&lt;p&gt;While there isn&amp;#x27;t any fancy interface that allows the same degree of simplicity for path parameters, retrieving them is still relatively easy. Similarly to before, we first extract the path parameters as whole.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; pathString &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;location&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;pathname&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;pathString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// &amp;#x27;/blog/posts&amp;#x27;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Next, we split up the whole path using &lt;code&gt;/&lt;/code&gt; as a reference. Remember to &lt;code&gt;filter&lt;/code&gt; any empty strings that result from the &lt;code&gt;split&lt;/code&gt; output.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; pathParams &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; pathString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;/&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;param&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; param&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;pathParams&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Array [ &amp;quot;blog&amp;quot;, &amp;quot;posts&amp;quot; ]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then, it&amp;#x27;s as simple as selecting the desired parameter from the array using an index.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Adding Data To An SQL Table]]></title><description><![CDATA[Assume you have the following table: To add data to it, use the  INSERT INTO  command. If you wish to add more than on record at once…]]></description><link>https://dilshankelsen.com/add-data-to-sql-table/</link><guid isPermaLink="false">https://dilshankelsen.com/add-data-to-sql-table/</guid><pubDate>Mon, 03 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Assume you have the following table:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;sql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-sql&quot;&gt;SQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-sql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;CREATE&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;TABLE&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; dog &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  name &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;CHAR&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  age &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;INT&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To add data to it, use the &lt;code&gt;INSERT INTO&lt;/code&gt; command.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;sql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-sql&quot;&gt;SQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-sql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;INSERT&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;INTO&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; dog &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;VALUES&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Sandy&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you wish to add more than on record at once, separate the values with a comma.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;sql&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-sql&quot;&gt;SQL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-sql&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;INSERT&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;INTO&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; dog &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;VALUES&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Sandy&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Odie&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[An Introduction To HTML5 Canvas]]></title><description><![CDATA[The HTML5 Canvas is a fantastic tool that allows us to show graphics of any kind. Yet, one should be aware that it  merely serves as the…]]></description><link>https://dilshankelsen.com/introduction-to-html5-canvas/</link><guid isPermaLink="false">https://dilshankelsen.com/introduction-to-html5-canvas/</guid><pubDate>Sat, 01 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The HTML5 Canvas is a fantastic tool that allows us to show graphics of any kind. Yet, one should be aware that it &lt;em&gt;merely serves as the container&lt;/em&gt; that houses these graphics. 👩‍🎨 The actual &lt;strong&gt;creation is done using JavaScript&lt;/strong&gt;.&lt;/p&gt;&lt;h2 id=&quot;setting-the-size&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#setting-the-size&quot; aria-label=&quot;setting the size permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Setting the size&lt;/h2&gt;&lt;p&gt;You can set the dimensions of the canvas using the &lt;code&gt;height&lt;/code&gt; &lt;strong&gt;and&lt;/strong&gt; &lt;code&gt;width&lt;/code&gt; &lt;strong&gt;attributes&lt;/strong&gt;. &lt;em&gt;By default&lt;/em&gt;, a canvas has a width and height of 300px and 150px, respectively.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;canvas&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;canvas&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;However, in most cases, you&amp;#x27;ll &lt;em&gt;prefer to set the size dynamically&lt;/em&gt;. Let&amp;#x27;s make it as big as the page it&amp;#x27;s in.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token dom variable&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;load&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; canvas &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;canvas&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  canvas&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;innerHeight&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  canvas&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;innerWidth&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;retrieving-the-context&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#retrieving-the-context&quot; aria-label=&quot;retrieving the context permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Retrieving the context&lt;/h2&gt;&lt;p&gt;Let&amp;#x27;s keep things simple and create some shapes. 🔲 For that, we need to retrieve the &lt;em&gt;2D context&lt;/em&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; context &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; canvas&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getContext&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;2d&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Imagine the context as an &lt;strong&gt;interface with properties and methods&lt;/strong&gt; that allows us to &lt;em&gt;perform the drawing&lt;/em&gt;. If we wanted to create a 3D graphic, we would instead have to pass in &lt;code&gt;&amp;quot;webgl&amp;quot;&lt;/code&gt; as a value.&lt;/p&gt;&lt;h2 id=&quot;drawing-a-square&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#drawing-a-square&quot; aria-label=&quot;drawing a square permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Drawing a square&lt;/h2&gt;&lt;p&gt;To create rectangles and squares, the context offers the &lt;code&gt;fillRect&lt;/code&gt; method. The first 2 parameters are the x and y coordinates (&lt;em&gt;in that order&lt;/em&gt;) starting &lt;strong&gt;from the top-left corner&lt;/strong&gt;. The 3&lt;sup&gt;rd&lt;/sup&gt; and 4&lt;sup&gt;th&lt;/sup&gt; are the width and height, respectively.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;fillRect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Square fill&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This function will fill in the body of the square. However, &lt;strong&gt;if you only want the outline&lt;/strong&gt;, use &lt;code&gt;strokeRect&lt;/code&gt; instead.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;strokeRect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;150&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Square outline&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:302px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:42.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAxklEQVQoz72QSwqEMBBEBUWPGLfiLVzqTlxpbuMJPImKHxDc+amZiiTgjLoapuCRVCdddGJFUYQkSRDHsYGe9aqqQG3bptZpmjDP8y08t97CHWmaqqBlWbDvO7quw5P6vj8CPc+D4zgGetallOriuq4qkA0U959QwzAcgbZtnybTPs/z04Q/C7yaUOs/ga7rqhANPetFUXwFXj1XYwLvyLLs9IdseNI4jrB830cYhgiCwEAvhEBZlmZCqq5rRdu2aJrGQK/PXrfgEi9PCKVIAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Results of fillRect and strokeRect&quot; title=&quot;Results of fillRect and strokeRect&quot; src=&quot;/static/7ebf880287382bd9627b0f875813862a/5f1d2/image-1.png&quot; srcSet=&quot;/static/7ebf880287382bd9627b0f875813862a/772e8/image-1.png 200w,/static/7ebf880287382bd9627b0f875813862a/5f1d2/image-1.png 302w&quot; sizes=&quot;(max-width: 302px) 100vw, 302px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;&lt;p&gt;  Results of &lt;em&gt;fillRect&lt;/em&gt; and &lt;em&gt;strokeRect&lt;/em&gt;&lt;/p&gt;&lt;/figcaption&gt;&lt;p&gt;For anything beyond a simple square, you will have to learn how &lt;em&gt;paths&lt;/em&gt; work. 🙃&lt;/p&gt;&lt;h2 id=&quot;paths-explained&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#paths-explained&quot; aria-label=&quot;paths explained permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Paths explained&lt;/h2&gt;&lt;p&gt;&lt;em&gt;What&amp;#x27;s a path&lt;/em&gt;? I find &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Drawing_shapes#drawing_paths&quot; target=&quot;_blank&quot;&gt;MDN&lt;/a&gt; gives a pretty &lt;strong&gt;good explanation&lt;/strong&gt;: &lt;/p&gt;&lt;p&gt;&amp;quot;A path is a list of points, connected by segments of lines that can be of different shapes, curved or not, of different width and of different color&amp;quot;.&lt;/p&gt;&lt;h3 id=&quot;creating-a-path&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#creating-a-path&quot; aria-label=&quot;creating a path permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Creating a path&lt;/h3&gt;&lt;p&gt;For this example, let&amp;#x27;s &lt;strong&gt;draw a triangle&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;beginPath&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;moveTo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// x, y&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;lineTo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// x, y&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;lineTo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;300&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// x, y&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;stroke&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:120px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:100.83333333333333%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAyklEQVQ4y+2UWwqGIBCF2/9KWkCLaAVFUUYPRg/RhYygiydm4Bfpf0nysYEB0eHz6OgJtNbwFcQKaDCOI/I8R1EUzlnXNbIsQ9d1DGVg27YYhgHLskAp9TjneUZVVWiahgUZYBzHvOga27YhiiL0fc9gA6Td9n039/AkKdZ1RZqmmKaJj89AuykuDbJrSZAB2qqeQu+1f8A3z+QDfkBHkFeF9mcgU/Gi8DgOJEnC9leWpR9gGIaQUkII8R54nierIxv72V/wtsP3uQsOqiK6rUjKIwAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Result of code snippet&quot; title=&quot;Result of code snippet&quot; src=&quot;/static/0f085fe20cc5e2c421a237de4f1ea54c/070ae/image-2.png&quot; srcSet=&quot;/static/0f085fe20cc5e2c421a237de4f1ea54c/070ae/image-2.png 120w&quot; sizes=&quot;(max-width: 120px) 100vw, 120px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Execution order of points&lt;/figcaption&gt;&lt;p&gt;Here&amp;#x27;s what happened: 👇&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;beginPath&lt;/code&gt; - tell the context that we want to create a path&lt;/li&gt;&lt;li&gt;&lt;code&gt;moveTo&lt;/code&gt; - place our &lt;em&gt;first point&lt;/em&gt; at the specified coordinates&lt;/li&gt;&lt;li&gt;&lt;code&gt;lineTo&lt;/code&gt; - place the &lt;em&gt;next point&lt;/em&gt; at the specified coordinates&lt;/li&gt;&lt;li&gt;&lt;code&gt;stroke&lt;/code&gt; - draw the lines connecting the points&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;All that&amp;#x27;s left is to &lt;strong&gt;connect points 3 and 1&lt;/strong&gt;, i.e. close the remaining path.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// [...]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;closePath&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Add before stroke()&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// [...]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:118px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:98.30508474576271%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAx0lEQVQ4y63USw6EIAwGYO5/M7dGBBccwscGgnbyM2EmOgFbnCb1AfhZSaw6joP+GQoHrTX1fU/TNNE4jmSMSWdOYi2ex/UHnOc5TaBaZIyR9n1nJdYihmGgEAKp/MnruqYKc3C3Iq+z1r5B3OBNNTRXXkoEvvBUIQetVXgCn6A/4HVCihbBVrQKtqC3oBRlgS2V3oJcVARy0DwuAu9QEchFRWANRVNAAPfe88HSni7LQs65b/tq6cpXdNs26roujavWVl/6DV8waR1upoO4BgAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Closing the triangle&quot; title=&quot;Closing the triangle&quot; src=&quot;/static/8e8f270e569187a4abc69f1d812ce7ff/9e4e1/image-3.png&quot; srcSet=&quot;/static/8e8f270e569187a4abc69f1d812ce7ff/9e4e1/image-3.png 118w&quot; sizes=&quot;(max-width: 118px) 100vw, 118px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Closing the triangle&lt;/figcaption&gt;&lt;h2 id=&quot;adding-style&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#adding-style&quot; aria-label=&quot;adding style permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Adding style&lt;/h2&gt;&lt;p&gt;If everything were black, our lives would be quite dull, wouldn&amp;#x27;t it? 🥱 So let&amp;#x27;s give our triangle some style! We can set the &lt;strong&gt;thickness and color&lt;/strong&gt; of our line using the properties &lt;code&gt;lineWidth&lt;/code&gt; and &lt;code&gt;strokeStyle&lt;/code&gt;. &lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;lineWidth&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;strokeStyle&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;#2F7ED8&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Add before beginPath()&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// [...]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It&amp;#x27;s &lt;strong&gt;important to remember&lt;/strong&gt; that the styling has to be placed &lt;em&gt;before&lt;/em&gt; the item(s) it should affect. In other words, if we were to &lt;em&gt;place it after&lt;/em&gt; the final &lt;code&gt;lineTo&lt;/code&gt; method, the above would not be applied.&lt;/p&gt;&lt;p&gt;To round it off, let&amp;#x27;s &lt;strong&gt;fill our triangle&lt;/strong&gt; with another color.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// [...]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;fillStyle&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;#C0D8F3&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Add before fill()&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Add before stroke()&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// [...]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The function &lt;code&gt;fill&lt;/code&gt; &lt;em&gt;fills in&lt;/em&gt; the body of the shape with color. If the color&amp;#x27;s not specified, it defaults to black. &lt;strong&gt;Once again, order matters.&lt;/strong&gt; ⚠️ If &lt;code&gt;fillStyle&lt;/code&gt; were to come after &lt;code&gt;fill&lt;/code&gt;, the styling would not be applied. Furthermore, these two lines of code are added before &lt;code&gt;stroke&lt;/code&gt; so that the filling &lt;em&gt;is not placed on top&lt;/em&gt; of the triangle outline, but &lt;em&gt;underneath&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:127px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:97.63779527559055%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAABcklEQVQ4y63Uu0vDQBwH8Pxj7m6Cj0XcHEQQQRBR3N2cXHyAgsG+pFU3B0Hrq6ltLTiIWvuItTZoaVPSBErbfL1rjEJokkvwB7/cZcjnfvfjLhxIdHs6/is4+ijW2lg4rGL5SMLqiYSlmDFnyZVjCYvRKtZOP6G1e+B6ulEdn2xgaD0PXqgjei8jcNdAKOWekbSM/UQdU3tl1JQOOJ2A+g8ayzYxuVtGsqDioawhVVSRKWmOmRU1JPIq5kIVA6QQBc0+hsiKo1si4s9KH7zKtXD7OjhvSFLs4knBbKDyV6EVDRN0bFvE5Uurj17njA9NiM7Nd4GMdPFf0MT8oLagEzrugDqCLJWmB6COoJ/tu4JeUGbQS0+FAiNoRTs2ldIqmUE3NE5QelvO6cEOMoJuPTV7OXNAwBYjaIfSazqx84azRwXz4Q98KR5Au+1HMjJGNkVM8++oq11voB0aTMkY3iihqfkAraj5P02Q4+OrQieUxjcgT82X6Us2zQAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Styled triangle&quot; title=&quot;Styled triangle&quot; src=&quot;/static/9e994bf9dfed821d9ed008c2cb426450/a9af1/image-4.png&quot; srcSet=&quot;/static/9e994bf9dfed821d9ed008c2cb426450/a9af1/image-4.png 127w&quot; sizes=&quot;(max-width: 127px) 100vw, 127px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Styled triangle&lt;/figcaption&gt;&lt;p&gt;If you wish to play around with the code in this article, &lt;a href=&quot;https://codepen.io/dkelsen/pen/abpRvWr&quot; target=&quot;_blank&quot;&gt;go ahead&lt;/a&gt;!&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Export And Import Modules In Node.js]]></title><description><![CDATA[Importing and exporting modules in Node.js works differently than in the front-end as this functionality was created before the introduction…]]></description><link>https://dilshankelsen.com/export-import-modules-in-nodejs/</link><guid isPermaLink="false">https://dilshankelsen.com/export-import-modules-in-nodejs/</guid><pubDate>Thu, 29 Apr 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Importing and exporting modules in Node.js works differently than in the front-end as this functionality was created before the introduction of ECMAScript Modules. &lt;/p&gt;&lt;p&gt;Unless you use a compiler like Babel or &lt;a href=&quot;https://nodejs.org/docs/latest-v13.x/api/esm.html#esm_enabling&quot; target=&quot;_blank&quot;&gt;ECMAScript Modules&lt;/a&gt; finally become a stable feature in Node.js, we are stuck with it for now. 🤷‍♀️&lt;/p&gt;&lt;h2 id=&quot;exporting-a-module&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#exporting-a-module&quot; aria-label=&quot;exporting a module permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Exporting a module&lt;/h2&gt;&lt;p&gt;The important thing to understand with Node.js modules is that you export them using JavaScript objects.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;exports&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; y&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; x &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; y&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In the object, you may assign your keys anything from primitives (e.g. numbers) to other objects (e.g. functions). To actually export everything, you must then assign it to &lt;code&gt;module.exports&lt;/code&gt;.&lt;/p&gt;&lt;h3 id=&quot;variations&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#variations&quot; aria-label=&quot;variations permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Variations&lt;/h3&gt;&lt;p&gt;You can write the code snippet above in different ways. Here&amp;#x27;s one:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;exports&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method-variable function-variable method function property-access&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; y&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; x &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; y&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;exports&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Again, nothing unusual if you know how JavaScript objects work. You can rewrite this again in an even cleaner fashion.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; y&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; x &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; y&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; number &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;exports&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; number &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;importing-a-module&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#importing-a-module&quot; aria-label=&quot;importing a module permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Importing a module&lt;/h2&gt;&lt;p&gt;Once you know that you&amp;#x27;re actually importing JavaScript objects, things become quite straightforward.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; helpers &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;./helpers&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In this example, we import a module (with the &lt;code&gt;require&lt;/code&gt; function) from the &lt;code&gt;helpers&lt;/code&gt; file, which is where defined the code from the previous section. This imported module is then assigned to a constant called &lt;code&gt;helpers&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;The most important thing to pay attention to is how to import local, core (Node.js) and external npm/Yarn modules.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Local modules&lt;/strong&gt;: Use a path in the file name (e.g. &lt;code&gt;&amp;#x27;./helpers&amp;#x27;&lt;/code&gt;)&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Core modules and external modules&lt;/strong&gt;: Enter the name without a path (e.g. &lt;code&gt;&amp;#x27;example&amp;#x27;&lt;/code&gt;). Node.js will know which one you&amp;#x27;re referring to. 😉&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;In all cases, you don&amp;#x27;t need to specify the extension.&lt;/p&gt;&lt;p&gt;After importing, you can then use the content of the module.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; sum &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; helpers&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;helpers&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// 8&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;destructuring-the-import&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#destructuring-the-import&quot; aria-label=&quot;destructuring the import permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Destructuring the import&lt;/h3&gt;&lt;p&gt;You can apply some good ol&amp;#x27; destructuring to limit what you import and access the function and variable immediately.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; number &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;./helpers&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; sum &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// 8&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now that looks much cleaner, doesn&amp;#x27;t it? 😄&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Solve "EADDRINUSE - Address Already In Use"]]></title><description><![CDATA[If you ever run into this problem, you have 2 options: Run your application on a different port, or Terminate the existing process Naturally…]]></description><link>https://dilshankelsen.com/solve-eaddrinuse-address-already-in-use/</link><guid isPermaLink="false">https://dilshankelsen.com/solve-eaddrinuse-address-already-in-use/</guid><pubDate>Mon, 26 Apr 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;If you ever run into this problem, you have 2 options:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Run your application on a different port, or&lt;/li&gt;&lt;li&gt;Terminate the existing process&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Naturally, I&amp;#x27;ll be covering the latter. 😋&lt;/p&gt;&lt;h2 id=&quot;find-the-process-id&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#find-the-process-id&quot; aria-label=&quot;find the process id permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Find the process ID&lt;/h2&gt;&lt;p&gt;First things first, we need to identify the process ID of the application that&amp;#x27;s occupying the port. You can do so with &lt;code&gt;lsof -i :&amp;lt;port number&amp;gt;&lt;/code&gt;.&lt;/p&gt;&lt;h2 id=&quot;terminate-the-process&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#terminate-the-process&quot; aria-label=&quot;terminate the process permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Terminate the process&lt;/h2&gt;&lt;p&gt;Terminate the process with &lt;code&gt;kill -9 &amp;lt;PID&amp;gt;&lt;/code&gt;.&lt;/p&gt;&lt;h2 id=&quot;windows&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#windows&quot; aria-label=&quot;windows permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Windows&lt;/h2&gt;&lt;p&gt;If you&amp;#x27;re on Windows and aren&amp;#x27;t using &lt;a href=&quot;https://docs.microsoft.com/en-us/windows/wsl/about&quot; target=&quot;_blank&quot;&gt;WSL&lt;/a&gt;, the next best thing is to simply open up your task manager, find the process and end it.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:433px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:88.99999999999999%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAIAAADUsmlHAAAACXBIWXMAAAsTAAALEwEAmpwYAAADB0lEQVQ4yzVT2ZLcNBT1/38DD7wHiuIhpIAHQgoIMGHCTPf0YrfbtnZLlm1Z3t0Lx5NCdbvrSrqL7jnHATc144qJvBum6/0Ou9zu19vqLLfVVud6my63+dXG5dJPM4L7cQ6YUIxSRpng3NVVYUxd19YWVVV633StL62dpum+rtur3W/ruuIXUC5x3TSNq2uttVK5EALJKJGrXOc407awdVV737raVWU1z/P1eh3HMXg8IyPHBUq23jvnCl0Mw4CtFNJog1DvfVVVqGOMEVwURYFabdsGD5t4v90IIRGNU7RN4kRJhW18ihlh4zj1/aCkTJIU0+1fduHheDrFzjXB8+EsGRn6tVVVWp0ryTj6YJsmSZZmyOz6vixLkhEp1fkUZ0mGoh6dI75NiTB+7C93VXqm64QqDLY+m3PJBdCap6n1jRLCaM0YlYIrIbuuD35+/PH9Tn33QL/94/T9s3138O+2xeeQsWb59a/Nh0+7ULmjqJ9iAf/jU/T+782Hf3a/fQ6JccGOPFtrOaNRGGJy0AO2hNQgNiUsI9y1vfO9sVVK8ZIch4xLwkTd+OCX338AP4SS7eYFlPjWgx/5il8cRZgTz+77vm09ISQ9J3EY0SwDfsAl+Pqbr8Aq5zwjhHFeoIBFb4Pk5JxgB6oAGNhiWJSd4xXFZVlwHrz96Q30AGCjKILUKojMFnmeIxnB4BNx/TCiOfjLlUJFSii0NM9L8PD4BBixwmN42B9LWxaFwR2SozACJcM4tl1njTkejjSjX3jOZQ7+gz9Pti4tmkASaZJiElCavyajBIIw85rvvTGYyKI/RIY3z8sSPO1CrSTJsu3zVhsNYWLmLCWvncNTGA/DiJkb10BhwG//sj9FJ2vLtfOnozRGU0rTNDsnqV55EqYoVoWlGckYBm67vmk8bpXMCVkjdW4GJENSmjNByAJQ2sa7ypXF2OM7uXWu7hp3uyxXQDsNvW8Q0DbOKKE465o6OKruKNye1Wk5UXdh7oJ/Ui+kmmm9wL745H8fAXtWfdwm/57z/wDCddzDRQPO4AAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Task manager in Windows&quot; title=&quot;Task manager in Windows&quot; src=&quot;/static/9917b92af81125c0e62287249f750d6f/55fc0/image-1.png&quot; srcSet=&quot;/static/9917b92af81125c0e62287249f750d6f/772e8/image-1.png 200w,/static/9917b92af81125c0e62287249f750d6f/e17e5/image-1.png 400w,/static/9917b92af81125c0e62287249f750d6f/55fc0/image-1.png 433w&quot; sizes=&quot;(max-width: 433px) 100vw, 433px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;&lt;p&gt;  Source from &lt;a href=&quot;https://stackoverflow.com/a/54413670&quot; target=&quot;_blank&quot;&gt;Stack Overflow&lt;/a&gt;&lt;/p&gt;&lt;/figcaption&gt;</content:encoded></item><item><title><![CDATA[Renaming A Git Branch]]></title><description><![CDATA[You can rename a branch with If you are already on the branch you wish to rename, you can  skip the first argument . In both cases,  -m  is…]]></description><link>https://dilshankelsen.com/renaming-git-branch/</link><guid isPermaLink="false">https://dilshankelsen.com/renaming-git-branch/</guid><pubDate>Sat, 24 Apr 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;You can rename a branch with&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; branch -m &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;old branch name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;new branch name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you are already on the branch you wish to rename, you can &lt;strong&gt;skip the first argument&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; branch -m &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;new branch name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In both cases, &lt;code&gt;-m&lt;/code&gt; is shorthand for &lt;code&gt;--move&lt;/code&gt;.&lt;/p&gt;&lt;h2 id=&quot;renaming-a-remote-branch&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#renaming-a-remote-branch&quot; aria-label=&quot;renaming a remote branch permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Renaming a remote branch&lt;/h2&gt;&lt;p&gt;Unfortunately, 🙁 there is &lt;strong&gt;no direct way&lt;/strong&gt; to rename a remote branch. As a work around, you can delete the remote branch and then &lt;em&gt;push the new renamed local branch&lt;/em&gt; to your repository. &lt;/p&gt;&lt;p&gt;Assuming you&amp;#x27;ve already renamed your local branch, you can do so in one fell swoop.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; push origin :&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;old branch name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;new branch name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;After that, you will &lt;strong&gt;need to reset the upstream branch&lt;/strong&gt;,&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; push origin –u &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;new branch name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;where &lt;code&gt;-u&lt;/code&gt; is shorthand for &lt;code&gt;--set-upstream&lt;/code&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Debug A Node.js App In Visual Studio Code]]></title><description><![CDATA[Debugging a Node.js app inside Visual Studio Code is quite straightforward.  Add a new script In your  package.json  file, add a new npm…]]></description><link>https://dilshankelsen.com/debug-nodejs-in-visual-studio-code/</link><guid isPermaLink="false">https://dilshankelsen.com/debug-nodejs-in-visual-studio-code/</guid><pubDate>Thu, 22 Apr 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Debugging a Node.js app inside Visual Studio Code is quite straightforward. &lt;/p&gt;&lt;h2 id=&quot;add-a-new-script&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#add-a-new-script&quot; aria-label=&quot;add a new script permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Add a new script&lt;/h2&gt;&lt;p&gt;In your &lt;code&gt;package.json&lt;/code&gt; file, add a new npm script called &amp;quot;debug&amp;quot;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;json&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-json&quot;&gt;JSON&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-json&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;scripts&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;test&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;echo \&amp;quot;Error: no test specified\&amp;quot; &amp;amp;&amp;amp; exit 1&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;start&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;node app.js&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;debug&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;node --inspect app.js&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;debug&lt;/code&gt; is essentially a copy of &lt;code&gt;start&lt;/code&gt; with &lt;code&gt;--inspect&lt;/code&gt; added before the main application file. What this does is attach your debugger to the Node.js application.&lt;/p&gt;&lt;h3 id=&quot;start-the-debugger&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#start-the-debugger&quot; aria-label=&quot;start the debugger permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Start the debugger&lt;/h3&gt;&lt;p&gt;To launch your debugger, open the integrated terminal and create a new &amp;quot;JavaScript Debug Terminal&amp;quot;.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:466px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:42.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAAAsTAAALEwEAmpwYAAABt0lEQVQoz1WRWWvbQBSF9SfS5a0Q+mDHdiRb26zapZEsa0aj1ZLtJC20NG0f+hpa6Hv/dCcQKIXD4fLBOVw4GvajKM1YUcYZC1MWJSxlRZKXrCzjlCEa2C5wdxvqGMgyyjQAu029zxQBlq5RJnI58fGuPX+cP33rL58P3Wl4+CLPH+R07zG+xSHCmFLiuKAo9qZtt4KD3Ro5Wy3MiqISXLaVbGU35gcu+7Fue+WiHbrjPEynrh+aru+GkYtaQV0V3C4sfaWlSbzP07Jgkh/qqixzFgd+5HuhT9MoCD0SBZ5PcRqFWRwqkiSxC6CtL21jpd0EYp00q6hehmKTtEufXwO2jmtF3pOD8oVfbSL5DuSvjejtNnplZksbu/rCNtaang2keTCLCYo7u5wvP34//vpjsMGtTrt83LLhNu2ccr4m1ZWRvNmlV2b+L8w5n8ZBCt7Kumub718ffz49lfscI0gxQsBVgq5DEPQw8jD0CHVc9+XtfSX68TifL6KWQja1bA6VkG13nOZ+UHb2/BARiglFzyKQBpYLXsJqRQChGgMipCrVrQ4AFX0WJtiy7f/lWKZhGzcq/Bduao7MoMxKWwAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Selecting a new terminal type&quot; title=&quot;Selecting a new terminal type&quot; src=&quot;/static/4bed887420fe3b9db64bb84e8a54f5b9/fc1a1/image-1.png&quot; srcSet=&quot;/static/4bed887420fe3b9db64bb84e8a54f5b9/772e8/image-1.png 200w,/static/4bed887420fe3b9db64bb84e8a54f5b9/e17e5/image-1.png 400w,/static/4bed887420fe3b9db64bb84e8a54f5b9/fc1a1/image-1.png 466w&quot; sizes=&quot;(max-width: 466px) 100vw, 466px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;&lt;p&gt;  Source from &lt;a href=&quot;https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_javascript-debug-terminal&quot; target=&quot;_blank&quot;&gt;Visual Studio Code&lt;/a&gt;&lt;/p&gt;&lt;/figcaption&gt;&lt;p&gt;Then run your debug command (e.g. &lt;code&gt;npm run debug&lt;/code&gt;) and you&amp;#x27;re in!&lt;/p&gt;&lt;h2 id=&quot;debug-with-configuration&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#debug-with-configuration&quot; aria-label=&quot;debug with configuration permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Debug with configuration&lt;/h2&gt;&lt;div&gt;&lt;p&gt;You may skip the rest of this post if your debugging requires no configuration.&lt;/p&gt;&lt;/div&gt;&lt;p&gt;There are situations where you might want to configure your debugger (e.g. &lt;a href=&quot;#debugging-nodemon&quot;&gt;Nodemon&lt;/a&gt;). Create a new file called &lt;code&gt;.vscode/launch.json&lt;/code&gt; at the root of your project. It should have the following content:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;json&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-json&quot;&gt;JSON&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-json&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;version&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;0.2.0&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;configurations&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;type&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;node&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;request&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;attach&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;name&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Node.js&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;processId&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;${command:PickProcess}&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;protocol&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;inspector&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Let&amp;#x27;s briefly go over the &lt;code&gt;configurations&lt;/code&gt; properties one by one:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;type&lt;/code&gt;: Debug in Node.js&lt;/li&gt;&lt;li&gt;&lt;code&gt;request&lt;/code&gt;: Set up an &amp;#x27;Attach&amp;#x27; configuration&lt;/li&gt;&lt;li&gt;&lt;code&gt;name&lt;/code&gt;: Name of the configuration&lt;/li&gt;&lt;li&gt;&lt;code&gt;processId&lt;/code&gt;: Use the selected process ID for debugging&lt;/li&gt;&lt;li&gt;&lt;code&gt;protocol&lt;/code&gt;: Use the inspector debug protocol&lt;/li&gt;&lt;/ul&gt;&lt;h3 id=&quot;start-the-configured-debugger&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#start-the-configured-debugger&quot; aria-label=&quot;start the configured debugger permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Start the configured debugger&lt;/h3&gt;&lt;p&gt;Instead of running your &lt;code&gt;debug&lt;/code&gt; script in a special debug terminal, run it in a regular one this time. Next, head over to the debugger tab, select your configuration from the dropdown and click on the arrow button.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:404px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:57.00000000000001%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAABW0lEQVQoz62S3VLCMBCF+yCKVkpK6U9om9CW/ghFCwOMjAM3juP4Kr75cZMW5MJxBvXim2432ZOT3RhrUeKtkRjvJUwhwRyJgUe4AtZIgOlYUhxrVMy8SYfs1sUpZ7hRgTBdwGQhnEAiSipwUUBkM0zyGqbD4cQSSbFAVj3A9gSu+yP0LA+mzUkshpjOcMt8jcFlRcU1LDqFyxL5bEUsadMcDk/JVQQWCPhRDj/OyYXA3TDU9J1Iu3aCpPsPYQyDlAozTTFfY3d4RbPZI05rfT21bvvJ6UoqVrlzznOGG+aIyaESzO5XWG4PSMsGbjjVOdtPvxHoOM8dBVXRMZlWDcp6jXr5hMVq1xW3rn6m3TMadw4DmjSjHqpGq8aqbxsHF0JD8ehq2/cPuKLGlTnEzcDX9Kwv4UvQU7Z5BUa2e333JPhbjM3zi+6Xek/qhD8LNjTVSfGo39N/CH4CPSxQ9LZYJxgAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Starting debugger from debugger tab&quot; title=&quot;Starting debugger from debugger tab&quot; src=&quot;/static/060d988cf7408c0ac37576d3db5cd712/494f9/image-2.png&quot; srcSet=&quot;/static/060d988cf7408c0ac37576d3db5cd712/772e8/image-2.png 200w,/static/060d988cf7408c0ac37576d3db5cd712/e17e5/image-2.png 400w,/static/060d988cf7408c0ac37576d3db5cd712/494f9/image-2.png 404w&quot; sizes=&quot;(max-width: 404px) 100vw, 404px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;You will the be prompted to select the process you wish to debug. If you don&amp;#x27;t know the process ID of your Node.js program, check the terminal where you ran the debugger script.&lt;/p&gt;&lt;h3 id=&quot;debugging-nodemon&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#debugging-nodemon&quot; aria-label=&quot;debugging nodemon permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Debugging Nodemon&lt;/h3&gt;&lt;p&gt;If you&amp;#x27;re running your Node.js application with Nodemon, you should add the &lt;code&gt;restart&lt;/code&gt; property to your configuration file:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;json&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-json&quot;&gt;JSON&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-json&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;configurations&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;restart&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This tells the debugger to reattach whenever your Node.js process restarts.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Stop Custom Font Flickering On Page Load]]></title><description><![CDATA[When you use a custom font via  @font-face , you tend to see unstyled text for a brief moment before your font files are fully loaded and…]]></description><link>https://dilshankelsen.com/stop-custom-font-flickering-page-load/</link><guid isPermaLink="false">https://dilshankelsen.com/stop-custom-font-flickering-page-load/</guid><pubDate>Mon, 19 Apr 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;When you use a custom font via &lt;code&gt;@font-face&lt;/code&gt;, you tend to see unstyled text for a brief moment before your font files are fully loaded and applied. This issue has been coined the name &lt;a href=&quot;https://en.wikipedia.org/wiki/Flash_of_unstyled_content&quot; target=&quot;_blank&quot;&gt;&amp;quot;Flash Of Unstyled Text&amp;quot;&lt;/a&gt;, or FOUT for short.&lt;/p&gt;&lt;p&gt;Fortunately, this can be fixed without much trouble.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;CSS&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-CSS&quot;&gt;CSS&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-CSS&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;@font-face {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  font-family: &amp;quot;My Font&amp;quot;;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  src: url(&amp;quot;/fonts/My-Font.otf&amp;quot;);&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  font-display: block; /* Fix flickering */&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;What this does is replace the unstyled text with an invisible placeholder until your custom font files have fully loaded.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[What Are React Server Components]]></title><description><![CDATA[I recently came across the concept of  React Server Components . Naturally, like any person, I wanted to know what they're all about. And in…]]></description><link>https://dilshankelsen.com/what-are-react-server-components/</link><guid isPermaLink="false">https://dilshankelsen.com/what-are-react-server-components/</guid><pubDate>Sat, 17 Apr 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;I recently came across the concept of &lt;a href=&quot;https://reactjs.org/blog/2020/12/21/data-fetching-with-react-server-components.html&quot; target=&quot;_blank&quot;&gt;React Server Components&lt;/a&gt;. Naturally, like any person, I wanted to know what they&amp;#x27;re all about. And in this article &lt;strong&gt;I will try&lt;/strong&gt; to give you a quick explanation of it.&lt;/p&gt;&lt;h2 id=&quot;should-you-care&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#should-you-care&quot; aria-label=&quot;should you care permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Should you care?&lt;/h2&gt;&lt;p&gt;My guess is &lt;em&gt;not yet&lt;/em&gt;. This feature is still in an experimental stage and it &lt;strong&gt;might take a while&lt;/strong&gt; until it&amp;#x27;s added to the core of React. End of 2021, maybe in 2022? Only time will tell. 🙃&lt;/p&gt;&lt;p&gt;However, my first impressions are &lt;em&gt;positive&lt;/em&gt; and I have a feeling that this will have a &lt;em&gt;substantial&lt;/em&gt; impact on React in the future. Furthermore, you can bet your ass that &lt;strong&gt;frameworks like Gatsby and Next.js&lt;/strong&gt; will adopt it as soon as it comes out.&lt;/p&gt;&lt;h2 id=&quot;differences-at-first-glance&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#differences-at-first-glance&quot; aria-label=&quot;differences at first glance permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Differences at first glance&lt;/h2&gt;&lt;p&gt;The React team has provided a &lt;a href=&quot;https://github.com/reactjs/server-components-demo/tree/main/src&quot; target=&quot;_blank&quot;&gt;demo project&lt;/a&gt; that makes use of server components. Here are some differences you&amp;#x27;ll notice while inspecting it.&lt;/p&gt;&lt;h3 id=&quot;file-extension&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#file-extension&quot; aria-label=&quot;file extension permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;File extension&lt;/h3&gt;&lt;p&gt;Server components use the &lt;strong&gt;file extension&lt;/strong&gt; &lt;code&gt;.server.js&lt;/code&gt; while client components (&lt;em&gt;which is what we currently use&lt;/em&gt;) now have &lt;code&gt;.client.js&lt;/code&gt; in their file name.&lt;/p&gt;&lt;p&gt;Files that have neither are shared between them.&lt;/p&gt;&lt;h3 id=&quot;no-hooks&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#no-hooks&quot; aria-label=&quot;no hooks permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;No Hooks&lt;/h3&gt;&lt;p&gt;Yes, you read that right! 🤪 &lt;strong&gt;Hooks or lifecycle methods don&amp;#x27;t work on the server&lt;/strong&gt;, hence neither do they in server components. Consequently, this will keep your components rather simple.&lt;/p&gt;&lt;h3 id=&quot;nodejs-api&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#nodejs-api&quot; aria-label=&quot;nodejs api permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Node.js API&lt;/h3&gt;&lt;p&gt;But you know what? Because these components live on the server, they have &lt;strong&gt;access to the Node.js API and any server-side libraries&lt;/strong&gt;, including ORMs. And that means your React component will be able to &lt;em&gt;directly query a database&lt;/em&gt;, among other things.&lt;/p&gt;&lt;h2 id=&quot;difference-to-ssg&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#difference-to-ssg&quot; aria-label=&quot;difference to ssg permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Difference to SSG&lt;/h2&gt;&lt;p&gt;Some of you might wonder how it&amp;#x27;s any different to &lt;em&gt;server-side generation&lt;/em&gt; (SSG). &lt;/p&gt;&lt;p&gt;As a recap, SSG is where your HTML content is created on the server. Once sent to the client, your JavaScript bundle is then injected 💉 into it. However, &lt;strong&gt;server components are complementary&lt;/strong&gt; and won&amp;#x27;t replace SSG.&lt;/p&gt;&lt;h3 id=&quot;zero-bundle-size&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#zero-bundle-size&quot; aria-label=&quot;zero bundle size permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Zero-Bundle-Size&lt;/h3&gt;&lt;p&gt;The important thing to understand about server components is that they are &lt;em&gt;fully rendered on the server&lt;/em&gt;. This means any associated JavaScript stays there and is no longer included in the bundle sent to the client. Hence, why they&amp;#x27;re referred to as &lt;strong&gt;Zero-Bundle-Size Components&lt;/strong&gt;.&lt;/p&gt;&lt;h2 id=&quot;how-they-work&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#how-they-work&quot; aria-label=&quot;how they work permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;How they work&lt;/h2&gt;&lt;p&gt;This is where they really shine! ✨ Given that server components are fully rendered before they reach the client, this allows you to &lt;strong&gt;individually request them from the server&lt;/strong&gt; and then integrate them into the frontend.&lt;/p&gt;&lt;p&gt;As an example, this &lt;a href=&quot;https://next-server-components.vercel.app/&quot; target=&quot;_blank&quot;&gt;live demo&lt;/a&gt; shows a notes application. Whenever you click on a note on the left-hand side, the application will &lt;em&gt;request the notes details component&lt;/em&gt;, which you then see on the right-hand side.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:469px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:62.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAAAsTAAALEwEAmpwYAAABE0lEQVQoz5WSyU7DMBBA8/+fAkhASlOo2oYGDhzgxA1QiRq3FjGJ99hOszDpAQmx1U9jWbL87BmPg6Pjk7Pz8BRGOIqiyXR6FcfxcpkkyQ3EYhHP5vPJ7HocXY4Hos8IRxeB0lW/p+06hFC2BlD6msKUZVnTNP3vBO8F40JTJpmQ6XqTYlKUBcY4z3NCiHOubdvmG7BYGRtkCBdUSG0gBWutVBqE/gBcvQuenlcl41JbSADO677yj0wKprTlsqKM/737B7nkEkyhDBfSXy6ptU4rbYzpfRjk+9X2cVO88JpqeCfPm28Ru9uKB9rmapA7LxlKNdYpVVXGdL5p4zcCTeaqgjZ711yy4YeIvVzvdocH5PsB/ejqmOWMqssAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Screenshot of demo&quot; title=&quot;Screenshot of demo&quot; src=&quot;/static/b4628146286122165a21cd45b4c686a6/5f759/image-1.png&quot; srcSet=&quot;/static/b4628146286122165a21cd45b4c686a6/772e8/image-1.png 200w,/static/b4628146286122165a21cd45b4c686a6/e17e5/image-1.png 400w,/static/b4628146286122165a21cd45b4c686a6/5f759/image-1.png 469w&quot; sizes=&quot;(max-width: 469px) 100vw, 469px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Screenshot of demo&lt;/figcaption&gt;&lt;p&gt;The server sends the server component in a special format that allows it to be integrated into the front-end without disrupting any state.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-text&quot;&gt;TEXT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-text&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;M1:{&amp;quot;id&amp;quot;:&amp;quot;dBD0&amp;quot;,&amp;quot;chunks&amp;quot;:[0,6],&amp;quot;name&amp;quot;:&amp;quot;&amp;quot;}&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;M2:{&amp;quot;id&amp;quot;:&amp;quot;iQ/d&amp;quot;,&amp;quot;chunks&amp;quot;:[0,4],&amp;quot;name&amp;quot;:&amp;quot;&amp;quot;}&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;S3:&amp;quot;react.suspense&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;J0:[&amp;quot;$&amp;quot;,&amp;quot;div&amp;quot;,null,{&amp;quot;className&amp;quot;:&amp;quot;container&amp;quot;,&amp;quot;children&amp;quot;:[[&amp;quot;$&amp;quot;,&amp;quot;div&amp;quot;, [...]&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;J4:[&amp;quot;$&amp;quot;,&amp;quot;ul&amp;quot;,null,{&amp;quot;className&amp;quot;:&amp;quot;notes-list&amp;quot;,&amp;quot;children&amp;quot;:[null,null,null, [...]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I don&amp;#x27;t even know what kind of format this is. 😆&lt;/p&gt;&lt;h2 id=&quot;read-more&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#read-more&quot; aria-label=&quot;read more permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Read more&lt;/h2&gt;&lt;p&gt;This article is really only meant to &lt;strong&gt;give you an idea and quick overview&lt;/strong&gt; of React Server Components. If you wish you to have a deeper understanding, I recommend you check out &lt;a href=&quot;https://www.youtube.com/watch?v=TQQPAU21ZUw&amp;amp;feature=emb_title&quot; target=&quot;_blank&quot;&gt;Facebook&amp;#x27;s video presentation&lt;/a&gt; and &lt;a href=&quot;https://github.com/reactjs/rfcs/blob/bf51f8755ddb38d92e23ad415fc4e3c02b95b331/text/0000-server-components.md&quot; target=&quot;_blank&quot;&gt;RFC&lt;/a&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[What Is Cross-Site Request Forgery (CSRF)]]></title><description><![CDATA[Imagine the following example: A user is logged into his online bank account which allows him to send money to another person by specifying…]]></description><link>https://dilshankelsen.com/what-is-cross-site-request-forgery/</link><guid isPermaLink="false">https://dilshankelsen.com/what-is-cross-site-request-forgery/</guid><pubDate>Thu, 15 Apr 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Imagine the following example: A user is logged into his online bank account which allows him to send money to another person by specifying an account number and amount in a form. &lt;/p&gt;&lt;p&gt;Suddenly, (s)he receives a dubious email from his/her bank and makes the foolish decision to click on a link inside the email. Before (s)he knows it, some money has been sent to another individual, all unwittingly! 😭 &lt;/p&gt;&lt;p&gt;What happened? Cross-Site Request Forgery, that&amp;#x27;s what happened.&lt;/p&gt;&lt;h2 id=&quot;how-it-works&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#how-it-works&quot; aria-label=&quot;how it works permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;How it works&lt;/h2&gt;&lt;p&gt;The idea behind this exploit is simple: the server cannot (easily) distinguish where a request is coming from. Even if it&amp;#x27;s from another tab, i.e. website, in your browser.&lt;/p&gt;&lt;p&gt;In the earlier scenario, the malicious hacker recreated the bank transfer form on another website. How? Well, maybe he&amp;#x27;s got access to the same bank and can therefore inspect the code in his browser.&lt;/p&gt;&lt;p&gt;All he needs to know is what kind of information the server is expecting (e.g. &lt;code&gt;bank_account_number&lt;/code&gt; and &lt;code&gt;amount&lt;/code&gt;) and which endpoint to send it to (e.g. &lt;code&gt;/transfer?confirm=true&lt;/code&gt;).&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:386px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:48.50000000000001%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA7UlEQVQoz5WQSYqEQBBFc+0dBL2NBxAX6qG8VqkHEEFcOCDtbDnQgla/0u6GHql6i+QT+eNHZIpxHJdlmaYJMc/zcoCmgqByXn2DYtu2oqqqMAxRiCRJiqLg7Ps+TdMoiuI4xnr7yr7vnHVdC+5oux40TdN1HUEED8PQHDD89Qfrut4nc80c1GlFEFmWZfUB+uU3MAvcWZZhOlPyPCf19i/n2vdmdmAUS57fQySV0/EX27a9v9l1Xd/3Pc+7XC5BEBBxfQw+S0iSpKqqLMsI0zQ/t3oEATQrioLQNO25ZsMwLMuybVvXdcdxnmp+AxTpHWycPGxxAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Bank transfer form from hacking scenario&quot; title=&quot;Bank transfer form from hacking scenario&quot; src=&quot;/static/b08845da9a7faf9870170fa97f08afe5/7bc0b/image-1.png&quot; srcSet=&quot;/static/b08845da9a7faf9870170fa97f08afe5/772e8/image-1.png 200w,/static/b08845da9a7faf9870170fa97f08afe5/7bc0b/image-1.png 386w&quot; sizes=&quot;(max-width: 386px) 100vw, 386px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Hypothetical bank transfer form&lt;/figcaption&gt;&lt;h3 id=&quot;hidden-and-ready-to-send&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#hidden-and-ready-to-send&quot; aria-label=&quot;hidden and ready to send permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Hidden and ready to send&lt;/h3&gt;&lt;p&gt;Okay, so the link that the user clicked on opened the hacker&amp;#x27;s malicious website which contains the duplicated form. Yet, all the user sees is a webpage full of cat pictures (&lt;em&gt;why not?&lt;/em&gt; 😎).&lt;/p&gt;&lt;p&gt;What the user doesn&amp;#x27;t know is that there&amp;#x27;s a hidden form on the page, prefilled with values and ready to be sent (using JavaScript) as soon as the page is loaded.&lt;/p&gt;&lt;h3 id=&quot;credentials-abuse&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#credentials-abuse&quot; aria-label=&quot;credentials abuse permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Credentials abuse&lt;/h3&gt;&lt;p&gt;Some of you might be asking, doesn&amp;#x27;t the request to the server need to be authorized? Well, yes. But remember that the user is already logged in. And in case you weren&amp;#x27;t already aware of it, the cookie containing the session ID is automatically sent to the server, even if the request is done from a completely different website.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:502px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:48.50000000000001%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAKABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAMBAv/EABcBAAMBAAAAAAAAAAAAAAAAAAACAwT/2gAMAwEAAhADEAAAAbbyy1qkY//EABoQAAICAwAAAAAAAAAAAAAAAAECAEIREiH/2gAIAQEAAQUCx0OEdxubXaf/xAAWEQADAAAAAAAAAAAAAAAAAAAAARH/2gAIAQMBAT8BSpD/xAAYEQACAwAAAAAAAAAAAAAAAAAAAhEhUf/aAAgBAgEBPwG1JbT/xAAXEAADAQAAAAAAAAAAAAAAAAAAAREg/9oACAEBAAY/AoMtx//EABsQAAICAwEAAAAAAAAAAAAAAAARMUEBECHB/9oACAEBAAE/IZXhWKBq7EDscjX0SP/aAAwDAQACAAMAAAAQeC//xAAXEQADAQAAAAAAAAAAAAAAAAAAASER/9oACAEDAQE/EEQYUP/EABYRAQEBAAAAAAAAAAAAAAAAAAERAP/aAAgBAgEBPxBYNuELn//EAB0QAQACAgMBAQAAAAAAAAAAAAEAESExQXGBUWH/2gAIAQEAAT8QsPpZCZByb8uVPYAJf6K84I7lsUsIbyfczYXLW4ZfSJSqrXPbP//Z&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;CSRF Meme&quot; title=&quot;CSRF Meme&quot; src=&quot;/static/5a320c36c8f86270048db4282d051f35/6f76c/meme-csrf.jpg&quot; srcSet=&quot;/static/5a320c36c8f86270048db4282d051f35/e07e9/meme-csrf.jpg 200w,/static/5a320c36c8f86270048db4282d051f35/066f9/meme-csrf.jpg 400w,/static/5a320c36c8f86270048db4282d051f35/6f76c/meme-csrf.jpg 502w&quot; sizes=&quot;(max-width: 502px) 100vw, 502px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;That sucks... 😕&lt;/figcaption&gt;&lt;h2 id=&quot;how-to-prevent-it&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#how-to-prevent-it&quot; aria-label=&quot;how to prevent it permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;How to prevent it&lt;/h2&gt;&lt;p&gt;As mentioned earlier, this exploit relies on the server not knowing where the request originated from. While in theory, it could figure that out with the &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer&quot; target=&quot;_blank&quot;&gt;&lt;code&gt;Referer&lt;/code&gt;&lt;/a&gt; header in a request, in practice, it isn&amp;#x27;t always sent for various reasons such as security and privacy.&lt;/p&gt;&lt;h3 id=&quot;using-a-token&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#using-a-token&quot; aria-label=&quot;using a token permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Using a token&lt;/h3&gt;&lt;p&gt;The most common solution to this problem are CSRF tokens, which are used in all modern web frameworks. The idea is as follows:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Generate a random string of letters and numbers on the server-side (e.g. &lt;code&gt;CIwNZNlR4Xbi1JF39I8yWnWX9wX4WFoz&lt;/code&gt;), then&lt;/li&gt;&lt;li&gt;Embed this token in the form when it&amp;#x27;s sent to the client, and&lt;/li&gt;&lt;li&gt;Only accept form submissions that contain this very token&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;More often than not, the token is single-use only, which makes it impossible to do any CSRF attacks.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Delete All Git Branches Except Master]]></title><description><![CDATA[If you wish to delete all Git branches except for one, run the command below. Replace  "master"  if your main branch is named differently…]]></description><link>https://dilshankelsen.com/delete-all-git-branches-except-master/</link><guid isPermaLink="false">https://dilshankelsen.com/delete-all-git-branches-except-master/</guid><pubDate>Mon, 12 Apr 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;If you wish to delete all Git branches except for one, run the command below. Replace &lt;code&gt;&amp;quot;master&amp;quot;&lt;/code&gt; if your main branch is named differently.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; branch &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;grep&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; -v &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;master&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;xargs&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; branch -D&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Of course, make sure not to be on a branch that will be deleted, or else you will run into an error.&lt;/p&gt;&lt;h2 id=&quot;how-it-works&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#how-it-works&quot; aria-label=&quot;how it works permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;How it works&lt;/h2&gt;&lt;p&gt;Let&amp;#x27;s quickly cover some Unix basics to properly understand the logic of this command. &lt;/p&gt;&lt;p&gt;&lt;code&gt;|&lt;/code&gt;, also known as the pipe symbol, serves to take the output of the previous command and feed it as an input to the next one. However, not every command like &lt;code&gt;grep&lt;/code&gt; will understand the format of this input. That&amp;#x27;s where &lt;code&gt;xargs&lt;/code&gt; comes into play.&lt;/p&gt;&lt;p&gt;Moving on to the list of subcommands, here&amp;#x27;s what they mean:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;git branch&lt;/code&gt;: list all local branches&lt;/li&gt;&lt;li&gt;&lt;code&gt;grep -v &amp;quot;master&amp;quot;&lt;/code&gt;: filter out the word &lt;code&gt;&amp;quot;master&amp;quot;&lt;/code&gt; from the list&lt;/li&gt;&lt;li&gt;&lt;code&gt;xargs git branch -D&lt;/code&gt;: delete all branches in the list&lt;/li&gt;&lt;/ul&gt;&lt;h2 id=&quot;keeping-more-than-1-branch&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#keeping-more-than-1-branch&quot; aria-label=&quot;keeping more than 1 branch permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Keeping more than 1 branch&lt;/h2&gt;&lt;p&gt;Maybe you&amp;#x27;ve got another branch called &lt;code&gt;staging&lt;/code&gt; that you wish to preserve as well. To exclude it from the deletion process, simply add it as follows:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; branch &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;grep&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; -v &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;master&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;grep&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; -v &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;staging&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;xargs&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; branch -D&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Ignore Library Code While Debugging In The Browser]]></title><description><![CDATA[All major browsers offer a way to ignore code from JavaScript libraries (e.g. jQuery and React) during debugging. Let me show you how…]]></description><link>https://dilshankelsen.com/ignore-library-code-while-debugging-browser/</link><guid isPermaLink="false">https://dilshankelsen.com/ignore-library-code-while-debugging-browser/</guid><pubDate>Thu, 08 Apr 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;All major browsers offer a way to ignore code from JavaScript libraries (e.g. jQuery and React) during debugging. Let me show you how! 😄&lt;/p&gt;&lt;h2 id=&quot;chrome&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#chrome&quot; aria-label=&quot;chrome permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Chrome&lt;/h2&gt;&lt;p&gt;While debugging in the Developer Tools, right-click on a stack in the &amp;quot;Call Stack&amp;quot; window and select &lt;code&gt;Add script to ignore list&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:424px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:80.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAIAAACZeshMAAAACXBIWXMAAAsTAAALEwEAmpwYAAACcUlEQVQoz1VTiWrcMBT0R5TsZn3bkmX5vm353GVpCBRSEgLNtW0J/f9v6MgOKYVByNodzbx5T0qalWI8zaebNG9c4lMv+A8s9PxIYt3gk3ohWc/xq0KivmyGfjqVTX+tmqpua4bzCVWzd3v9aq/tD8Zurx00yyGMBzHjseV4Cg+icTqezjfH89dpOeVF5RJCqUc9RqjHg3Ac52la2lZgjeI0DKM0ywHf54rl0LIWcF63YzcsdTdusGyqalaSZq9vl8vl14+nF6x3d9+LsoqTNM8L26GKbpKi7luxxFm9enYBVXeuVWt/MCHz9Pz6/PJ2+fn7/v7x/f3Pw8Pj7e03IQZcLcllMzZiBmzCv1ypoAEHzUYEjut1YuzEIPqxacUwzKixH+a8qHXTVQyTVC18Trii7Zes7HBy+CDbWB2X2S6zbI9QjnagHNtFIty0PUU33ArK3dz2R/jH0W5v7K+Ng9Q34SuMsiBMgyAN4yyKc7kPUx4kIErlrBLwDPFWzFCumoGHGQqGLP6B9rK1zz6PAbZ9+tGHbRCyskW3qTwiEDcsKp2rFsJDSzEYgKTx2FvHA3vDIjKwtGhRdpI3QBAXYVKCv8UGYBiYH29zxni0zlngc5DXViV5209n2bB+idKK0FAOFsrWZGA28SG4wQ+SlS9LMG0qbcNwN5xqMffzOc0az4+jpKQsQmwoDPFs1X5GBdtYkb9iWhTkdli2KyDeDce8Eg7h8IzA4FmW6v8LDOQPZdydVx3S3vrcDkeQbYdd7bQtMBhhDGlHG3l7W1ihKm0jp6IZqm5C2TzMeZTrFt3SwsOSr4/hGYafr1K2DX02yV9Ii6RtBuFoMAAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Call stack list in Chrome&quot; title=&quot;Call stack list in Chrome&quot; src=&quot;/static/1a785c30a7c76ff9a9ace87178e25f01/1cfa9/image-1.png&quot; srcSet=&quot;/static/1a785c30a7c76ff9a9ace87178e25f01/772e8/image-1.png 200w,/static/1a785c30a7c76ff9a9ace87178e25f01/e17e5/image-1.png 400w,/static/1a785c30a7c76ff9a9ace87178e25f01/1cfa9/image-1.png 424w&quot; sizes=&quot;(max-width: 424px) 100vw, 424px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;Alternatively, you may open a file in the editor pane, right-click inside the file and select &lt;code&gt;Add script to ignore list&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:606px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:46.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAAAsTAAALEwEAmpwYAAABt0lEQVQozxWRZ3KjQBCFucXaQoQJzIhhYMhBBBGNwkrCErLX97/Ijqte/+nuV1/XawVhYjNuO2LHPMZ9suOYMNmznNAJ92EUE0JN09yomhxZxIYII4vueOB4oSIr2vdpUVPb001sQgIQhZgCwimPuCviOBcicr2QEAYg2WqmbiBMfUS4gikPimmcL/u6L5uh7qa8bJKs8oQPAOBu+Hr9PNfvx/qdFdWOuW+q+a6CP+/6RjWVLE3XW3f+u7RdX9WHummLooyTzPMEQpYQyePxdf9cPx+veT6GSWqxwHZz5ifY5grCbJ9N3XQqylYen+a15MdZBTGRZpuJ5/qzvv5dLtcojpPA5wQTalPbQQgpEDlJcO3nY9N+DB/nbjzN53tetTIVLKNjYlnW+/K83ZZ+GH0hdM182xhS6hZIspMG56Yb91Vft6PEHoZjUbVYRmqzMMyX5Xk6XYdhHqbZcQPpMUxLlzLQrznxL00/5uWhbPqqHet2itMSIonl3AtlzoxHFvOJlxDmAUi3GlQ1qOnw1xwHp6rtJU2qOoxNN0VpKb/BmCOfL/cAdKDlAhYA29cxM6gwMFO35n8U6Wl0lJp/rgAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Editor Pane in Chrome&quot; title=&quot;Editor Pane in Chrome&quot; src=&quot;/static/d78050a2321e52078858cc8ecc8a0df5/4d4a2/image-2.png&quot; srcSet=&quot;/static/d78050a2321e52078858cc8ecc8a0df5/772e8/image-2.png 200w,/static/d78050a2321e52078858cc8ecc8a0df5/e17e5/image-2.png 400w,/static/d78050a2321e52078858cc8ecc8a0df5/4d4a2/image-2.png 606w&quot; sizes=&quot;(max-width: 606px) 100vw, 606px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;If you know upfront which files to ignore, head over to &lt;strong&gt;Developer Tools &amp;gt; Settings (Cog Icon) &amp;gt; Ignore List&lt;/strong&gt;. Then add a file name or regex pattern you wish to exclude.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:510px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:51.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAAAsTAAALEwEAmpwYAAABcUlEQVQoz32QTUvkQBCG5+6Ch3Vnkk7S39/ppDvp9EzcRVbwKojgxV8h7P8/bEVFhjnYPBTVUG/VW7UzLsbpRLjBVLkwWZ9cPxkfbZ86ovZVd6jxB185JBUidUN31se53DJhKTdlvUvzCXqlvI5TgRZS98r00kAMxo/KBsi1G5qOg34HMh9giCRMD3Hxw9xvZD/kEBcuHROOvUehe/iCRyosatk2mXJFqPx1QPuqGcYcxgwtxrhABC+HM9sbVQeNoAtM3sTG9i1mVz+uf94c5rzGqUz5NC/rEHOFKFScUyEMFwmptETWYBucuD7WDefSlvW3MlZbByhtuJAXUMbTnNNcoLrDHHY22oa6ZYSIJaVpjKRBuGkJg9JLuFDH47GU4p3DZBNbuCdCBIkgn9+6vy/65R97eJVCKGWU0ufAM/C2oDfx5z4gpsbfP+nTg7l7JPEPJpRQfgEGyDuUNx3bfR0DNRQ8BB9Ii6t9Ddf6HpD8B1YNVfnknF5dAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Ignore list settings in Chrome&quot; title=&quot;Ignore list settings in Chrome&quot; src=&quot;/static/34abfce90e9ab2b0a9b996c6bee4f58b/0abdd/image-3.png&quot; srcSet=&quot;/static/34abfce90e9ab2b0a9b996c6bee4f58b/772e8/image-3.png 200w,/static/34abfce90e9ab2b0a9b996c6bee4f58b/e17e5/image-3.png 400w,/static/34abfce90e9ab2b0a9b996c6bee4f58b/0abdd/image-3.png 510w&quot; sizes=&quot;(max-width: 510px) 100vw, 510px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;div&gt;&lt;p&gt;This should be the same for any other Chromium-based browser like Vivaldi or Edge. At most, the names may differ.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;firefox&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#firefox&quot; aria-label=&quot;firefox permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Firefox&lt;/h2&gt;&lt;p&gt;With the exception of the last point, files can be ignored the same way as in Chrome. The only difference is the naming - &lt;code&gt;Ignore source&lt;/code&gt;. &lt;/p&gt;&lt;p&gt;On top of that, you can right-click on a file in the File Tree View. Even cooler, you can click on that eye 👀 at the bottom of the file editor.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:675px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:36%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAAsTAAALEwEAmpwYAAABhklEQVQozyWR64rVMBSF+/7zEv4URBD/CqL4AuLgKBzPvde0p02aNk3Sy+eeTmBDNklWvrV2ci0e/L405KpDVYprVXFWI3/vHX/SjkI9aJSiVrVUg9H9vr/fUyq5qztNCAFjDHlekEw+kpU1rRzM8wLbhht6ButwzcKtnclaR9ePhDXgZ88UZ9zk5bMWPXhcWJiXFe89ybqBNj2qbt4EZWVFyWgtsV54rgMnIbpUIyeTkbsM5ToRcZybwLGJdGNE9IRlI9kVmNB1gbNm7x5NLYSj9AGrLZ02VCbQ9EIn1WuPHR23NCcTm3OM+7t1XUk+v0Q+/HR8eVb8OikO1cThmovtkbqNHPKJtPGUxmNcxPaSVx9pjaXIc9I0kwyFcFmw4ir5flr4evC8+5by9PGFp0//eP8jF8KBwUyUeuLRjpKxCE4rNm6sYm0TmlEotdA7N+2EwzC8WpbcvEyuzGRy2T6c6+FI85qjBK+0IwzhLfh5Zl2WPetBHNxuN87nC8fjib63tG3Hf4LbEt+LrlniAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Debugger window in Firefox&quot; title=&quot;Debugger window in Firefox&quot; src=&quot;/static/af4359d14dad468cf90a61ea77af417c/23296/image-4.png&quot; srcSet=&quot;/static/af4359d14dad468cf90a61ea77af417c/772e8/image-4.png 200w,/static/af4359d14dad468cf90a61ea77af417c/e17e5/image-4.png 400w,/static/af4359d14dad468cf90a61ea77af417c/23296/image-4.png 675w&quot; sizes=&quot;(max-width: 675px) 100vw, 675px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Sourced from &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Tools/Debugger/How_to/Ignore_a_source&quot; target=&quot;_blank&quot;&gt;Firefox&lt;/a&gt;.&lt;/figcaption&gt;&lt;h2 id=&quot;safari&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#safari&quot; aria-label=&quot;safari permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Safari&lt;/h2&gt;&lt;p&gt;In the File Tree View, you can ignore a script by either right-clicking on it and selecting &lt;code&gt;Blackbox Script&lt;/code&gt;, or by clicking on the eye that becomes visible while hovering over it.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:306px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:127%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAZCAIAAAC+dZmEAAAACXBIWXMAAAsTAAALEwEAmpwYAAADL0lEQVQ4y4VUW28bRRSe/8xF6gsvlRBqK8Qj6ltfiiKaUkQDoo1TK6iJU2fjXXsvszt7v87eZmYdaFokCF/sQoObJkdHa1veb853vvOdIUmaHZ/Mjo4mnsfSNE2SpO978fHAf4MUSSUWUUOivDk8sU606bGmeZ6X5/kN4F4slaBpf+gKEmT1q7lLbcuYX0QQBOLauKisRFJ2TtIRP8pfaZZhzBzH8X2/bVul1PV4KUXV9GBOal6bC9MwjCiKqqrKVoEjbgRnXJCyqEzTtiwLDUMwFAe46zq5CrDAc4O2kiLjPct7witumaBtUOpSSm3bdl3XNM04SZqmKYpigwUEO1XCScVLV5KqBNjG24yxOI7DMMS04lWgC3DBEZeL/6u2OPQkyfNiMbdQkHM+DINaxX+Er6QNtcOiX0SCoL21YCgIzdAznlAeZ23A3vesZFZULMoI57VlXQi21hlHgGoUxXXTdkL2V5nk90EsErnnDKQoSt1YaJqGJkH7nchSni07JBjiZ38pVoNQaRx6jknADIJDG/CEsE3b9l2blu29vdPbz890L48CxuumuxQoD6WCMCJpluMDrqzret0knk0nJv7ypaeirMrzrEELl2I9PLxGsoLP5s5Um5mWQ13Psh3Lph7zA9dSHRdSNZ3krdjMpm/anvz9RjLP9G34+9g2565tIpnrJFE4KHl2Ks7fIuUHqf58PZAvtqs7j8xPHrC72/TxQf7ooPxhwrcPSnx5Minv7/JPv+Ofb/0vP9uqb23lD/cC8s0v9bdPjK++d55OiwMz/fWIjvVorIcjzT+0kqda/eWP1b2f67s77/POTv31TvnTJCHnb4eFPtWnvwWBb/uxYVE3iE3KyqZr5bJp+V+vu/M3ciPR7NkfA+nl4DH2bHdEXeYyH6K5roclCcMI8yxKXtUtxP8g+64XBO7FMuzv7zPmA2cYc4dSuBUODYIQ818N9uogcBWGvDsaYZ+jEK6m8DYAaz+tnXMdGMvw4sWY4fpkDNcYKoPLsBzUoG4AgzZcPRqNwBMYLAljHjYUq4KbALa7Aazr+ng89pmPm2Q2m4E/6mvHuIkZ59U14H8AQYkvl39ayicAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Debugger window in Safari&quot; title=&quot;Debugger window in Safari&quot; src=&quot;/static/d65e9bd29b41210fef6995c383fe8202/98b92/image-5.png&quot; srcSet=&quot;/static/d65e9bd29b41210fef6995c383fe8202/772e8/image-5.png 200w,/static/d65e9bd29b41210fef6995c383fe8202/98b92/image-5.png 306w&quot; sizes=&quot;(max-width: 306px) 100vw, 306px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;Similar to Chrome, you can specify a regex pattern that you wish to exclude under &lt;strong&gt;Settings (Cog Icon) &amp;gt; Blackbox&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:492px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:41.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAIAAAB2/0i6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAA4ElEQVQY042Q2Q6CMBBF+/+fxyKILBUoSwArtIAJm9fWvhhNPA+TO5eZ6QxkXdddcRxH3/d3gxACcRgGWHzab9MBp2kaOCjetm1ZFpJlmWVZtm17nue6LvRJAW0bXMfRwvd9FDiKsixJ27Z5nqdpWiigETERgjGmU+0zQ1Gwuq6llGSeZ6ggCOA+FHB0/Aql1DtfoijC/gQDOOfYAffgSN35i3maUHPJ+47fpRBkHEfMwONd18n/GI0gQqET8Y1B8WG+m6uqiuOYKpIkwTFIwzB8pTTRn+BfDfi1OBD7ovkJo9K57GU+/aMAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Blackbox Settings in Safari&quot; title=&quot;Blackbox Settings in Safari&quot; src=&quot;/static/29802dda10e6f8eed1a34f070ed6c60b/5c6e9/image-6.png&quot; srcSet=&quot;/static/29802dda10e6f8eed1a34f070ed6c60b/772e8/image-6.png 200w,/static/29802dda10e6f8eed1a34f070ed6c60b/e17e5/image-6.png 400w,/static/29802dda10e6f8eed1a34f070ed6c60b/5c6e9/image-6.png 492w&quot; sizes=&quot;(max-width: 492px) 100vw, 492px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Customize The Input File Button]]></title><description><![CDATA[Customizing the input file button  used to be  notoriously difficult. As such, developers had to come up with a creative workaround…]]></description><link>https://dilshankelsen.com/customize-input-file-button/</link><guid isPermaLink="false">https://dilshankelsen.com/customize-input-file-button/</guid><pubDate>Mon, 05 Apr 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Customizing the input file button &lt;em&gt;used to be&lt;/em&gt; notoriously difficult. As such, developers had to come up with a creative workaround. Fortunately, things have changed for the better as CSS specifications evolved. Nonetheless, I&amp;#x27;d like to cover both the workaround and the new way of customizing the button.&lt;/p&gt;&lt;p&gt;So let&amp;#x27;s start with the former! 😜&lt;/p&gt;&lt;h2 id=&quot;the-old-way&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-old-way&quot; aria-label=&quot;the old way permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The old way&lt;/h2&gt;&lt;p&gt;The idea behind the workaround is relatively simple. As you may or may not know, clicking on the label of an input of type file can trigger the file dialog as well. This allows us to simply place our custom button inside the label and then hide the original input button.&lt;/p&gt;&lt;h3 id=&quot;layout-does-not-matter&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#layout-does-not-matter&quot; aria-label=&quot;layout does not matter permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Layout does not matter&lt;/h3&gt;&lt;p&gt;Regardless of whether you decide to wrap the label around the input or keep both elements separate, the solution works. And to hide the input element, simply add on the &lt;code&gt;hidden&lt;/code&gt; attribute.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;upload-file&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;upload-file&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Upload&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;&amp;lt;!-- OR --&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  Upload&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;styling-the-button&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#styling-the-button&quot; aria-label=&quot;styling the button permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Styling the button&lt;/h3&gt;&lt;p&gt;Depending on your circumstances, you may prefer to either:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Style the label so it looks like a button, or&lt;/li&gt;&lt;li&gt;Add an element (e.g. &lt;code&gt;div&lt;/code&gt;) inside the label that acts and looks like a button &lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;p&gt;Adding a &lt;code&gt;button&lt;/code&gt; element inside the input label will unfortunately not trigger the file dialog. Wacky, I know... 😅&lt;/p&gt;&lt;/div&gt;&lt;p&gt;You can play around with &lt;a href=&quot;https://codepen.io/dkelsen/pen/dyOBWJK&quot; target=&quot;_blank&quot;&gt;some code&lt;/a&gt; I&amp;#x27;ve prepared on Codepen. It covers what I&amp;#x27;ve just mentioned above.&lt;/p&gt;&lt;h2 id=&quot;the-new-way&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-new-way&quot; aria-label=&quot;the new way permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The new way&lt;/h2&gt;&lt;p&gt;Unknown to many, there&amp;#x27;s actually a CSS pseudo-element called &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/CSS/::file-selector-button&quot; target=&quot;_blank&quot;&gt;::file-selector-button&lt;/a&gt;, or &lt;code&gt;::-webkit-file-upload-button&lt;/code&gt; for WebKit/Blink compatible browsers, which allows you to effortlessly style the input button. It has &lt;em&gt;finally&lt;/em&gt; garnered enough support by most browsers that it can actually be used.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;CSS&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-CSS&quot;&gt;CSS&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-CSS&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;input[type=file]::file-selector-button {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  /* Add properties here */&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[An Introduction To JSON Web Tokens]]></title><description><![CDATA[A JSON Web Token, or JWT for short, is an authorization web standard for client-to-server or even server-to-server communication…]]></description><link>https://dilshankelsen.com/introduction-json-web-tokens/</link><guid isPermaLink="false">https://dilshankelsen.com/introduction-json-web-tokens/</guid><pubDate>Thu, 01 Apr 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A JSON Web Token, or JWT for short, is an authorization web standard for client-to-server or even server-to-server communication. Authorization should not be confused with authentication, where:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Authorization: Checks whether a user has the permission to access a certain resource,&lt;/li&gt;&lt;li&gt;Authentication: Checks whether a user really is who they claim to be.&lt;/li&gt;&lt;/ul&gt;&lt;h2 id=&quot;how-it-works&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#how-it-works&quot; aria-label=&quot;how it works permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;How it works&lt;/h2&gt;&lt;p&gt;To understand how exactly it&amp;#x27;s used, let&amp;#x27;s imagine the following scenario on a website.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:56.49999999999999%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAABh0lEQVQoz21RzUrDQBDOa3jwHcR3ETwJgl71DTz7c+wDeNQH6M0iSMUerGILtSj1ICpomqRu/rbJ7uZzZ5MNoXHgYyYz3858k3GKooBUCqKCVAUoR9byuqZyCZWJErkwuSbHeZsznA9ecDmc4UKD4p8wNUVVlM0Lqcx3MJjicfsU4/0OxnsdPGwdI5y8V8NKjjP69HDUHeKsN8LJ1ZOJPxZxSUJllQq3e4/r9V30Nw/R3zhAb20Hwd205FZDnWiZg1TOXGb86/cCLE4h8hxZltXIlUT0Ncf8Zgz/dqLxjEB7wZJ6ZYKDVZMCPE2RplwjBee8BOUyDrFCF4Uyw+t/SIGyh5HSrKl0bFRVKq03B6DDCImMLxH+Mvi+jziOa5VO85JCCEjdlBpYddYn2lOe6gRVHcGu2mpIBCKSkiiKzFRqFieJiRPtKW+b2oZNaykkkvWkdhWUt6DGYRjC8zy4rosgCNoN7WR6TGroAflIKySVtm7XtjzGmNmsPkpzbavyPzTr/xnl/wA+DExZFhtamgAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;HTTP request sequence including the use of a JWT&quot; title=&quot;HTTP request sequence including the use of a JWT&quot; src=&quot;/static/7cf7d375e85933d34a5b30e0fc50f1e3/5a190/image-1.png&quot; srcSet=&quot;/static/7cf7d375e85933d34a5b30e0fc50f1e3/772e8/image-1.png 200w,/static/7cf7d375e85933d34a5b30e0fc50f1e3/e17e5/image-1.png 400w,/static/7cf7d375e85933d34a5b30e0fc50f1e3/5a190/image-1.png 800w,/static/7cf7d375e85933d34a5b30e0fc50f1e3/30c92/image-1.png 874w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;&lt;p&gt;  Image from &lt;a href=&quot;https://stackoverflow.com/questions/41471563/how-to-add-jwt-to-authorization-header&quot; target=&quot;_blank&quot;&gt;Stack Overflow&lt;/a&gt;&lt;/p&gt;&lt;/figcaption&gt;&lt;h3 id=&quot;step-1&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-1&quot; aria-label=&quot;step 1 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 1&lt;/h3&gt;&lt;p&gt;The user starts off logging in to the website by sending a POST request to the backend.&lt;/p&gt;&lt;h3 id=&quot;step-2&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-2&quot; aria-label=&quot;step 2 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 2&lt;/h3&gt;&lt;p&gt;Upon receiving the user credentials from the client, the server &lt;em&gt;authenticates&lt;/em&gt; the user. If successful, it then generates a JWT using a secret key. The token includes some kind of user information such as a &lt;em&gt;user id&lt;/em&gt;, so that the server can easily identify the client in future requests.&lt;/p&gt;&lt;h3 id=&quot;step-3&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-3&quot; aria-label=&quot;step 3 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 3&lt;/h3&gt;&lt;p&gt;The server sends a response to the client that includes the newly created JWT, with the token then stored in the browser.&lt;/p&gt;&lt;h3 id=&quot;step-4&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-4&quot; aria-label=&quot;step 4 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 4&lt;/h3&gt;&lt;p&gt;The client sends a new request, let&amp;#x27;s say, to access a page. The JWT is included in the request header in the &lt;code&gt;Authorization&lt;/code&gt; field. Notice that the JWT is prepended by the keyword &lt;a href=&quot;https://tools.ietf.org/html/rfc6750#section-1.2&quot; target=&quot;_blank&quot;&gt;Bearer&lt;/a&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-text&quot;&gt;TEXT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-text&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI...2QT4fwpMeJf36POk6yJV_adQsw5c&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;step-5&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-5&quot; aria-label=&quot;step 5 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 5&lt;/h3&gt;&lt;p&gt;The server verifies the authenticity of the JWT with its secret key, making sure it has not been tampered with. If successful, any user information included in the token is used to identify the user&amp;#x27;s permissions.&lt;/p&gt;&lt;h3 id=&quot;step-6&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-6&quot; aria-label=&quot;step 6 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 6&lt;/h3&gt;&lt;p&gt;A response is sent back to the client, either containing the requested page or an error depending on the user&amp;#x27;s permissions.&lt;/p&gt;&lt;h2 id=&quot;the-structure&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-structure&quot; aria-label=&quot;the structure permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The structure&lt;/h2&gt;&lt;p&gt;Let&amp;#x27;s dive a level deeper. A JWT will typically have the following layout: &lt;code&gt;xxxxx.yyyyy.zzzzz&lt;/code&gt;. The dots serve the purpose of separating 3 things:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;The header (x),&lt;/li&gt;&lt;li&gt;Payload (y), and&lt;/li&gt;&lt;li&gt;Signature (z)&lt;/li&gt;&lt;/ul&gt;&lt;h3 id=&quot;header&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#header&quot; aria-label=&quot;header permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Header&lt;/h3&gt;&lt;p&gt;The header consists of a JSON Object which typically holds two types of data:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;The signing algorithm (in this case HS256), and&lt;/li&gt;&lt;li&gt;The token type (in this case JWT... of course! 😆)&lt;/li&gt;&lt;/ul&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;json&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-json&quot;&gt;JSON&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-json&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;alg&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;HS256&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;typ&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;JWT&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This JSON Object is then &lt;em&gt;Base64url&lt;/em&gt; encoded and placed in the 1&lt;sup&gt;st&lt;/sup&gt; spot of the token layout.&lt;/p&gt;&lt;h3 id=&quot;payload&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#payload&quot; aria-label=&quot;payload permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Payload&lt;/h3&gt;&lt;p&gt;The payload is yet another JSON object with more information. The important thing to notice here is that there are predefined property names: either as reserved names called &lt;em&gt;registered claims&lt;/em&gt;, or through agreement, called &lt;em&gt;public claims&lt;/em&gt;. A comprehensive list can be viewed on the &lt;a href=&quot;https://www.iana.org/assignments/jwt/jwt.xhtml#claims&quot; target=&quot;_blank&quot;&gt;IANA website&lt;/a&gt;, with the former names registered by &lt;em&gt;IESG&lt;/em&gt;, and the latter by &lt;em&gt;OpenID Foundation&lt;/em&gt;. Examples are &lt;code&gt;iss&lt;/code&gt; (issuer), &lt;code&gt;exp&lt;/code&gt; (expiration) and &lt;code&gt;name&lt;/code&gt; (full name). &lt;/p&gt;&lt;p&gt;You may of course name custom properties if you wish to do so.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;json&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-json&quot;&gt;JSON&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-json&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;sub&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;1234567890&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;name&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;John Doe&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token property&quot;&gt;&amp;quot;iat&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1516239022&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Once again, the JSON Object is &lt;em&gt;Base64url&lt;/em&gt; encoded and placed in the 2&lt;sup&gt;nd&lt;/sup&gt; spot of the token layout.&lt;/p&gt;&lt;div&gt;&lt;p&gt;Be aware that anyone who has access to your JWT can decode and read this information. If you decide to use sensitive data, make sure to encrypt it first.&lt;/p&gt;&lt;/div&gt;&lt;h3 id=&quot;signature&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#signature&quot; aria-label=&quot;signature permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Signature&lt;/h3&gt;&lt;p&gt;The purpose of the signature is to be certain that none of the above-mentioned information has been changed. To obtain the signature, you simply add the encoded header and payload, and then encode (&lt;em&gt;aka sign&lt;/em&gt;) it with your secret key using the specified algorithm.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token constant&quot;&gt;HMACSHA256&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;base64UrlEncode&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;.&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;base64UrlEncode&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  secret&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;jwt-debugger&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#jwt-debugger&quot; aria-label=&quot;jwt debugger permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;JWT debugger&lt;/h2&gt;&lt;p&gt;Jwt.io offers a great debugger with which you can play around and see how the encoded values and their validity change depending on different inputs. Give it &lt;a href=&quot;https://jwt.io/&quot; target=&quot;_blank&quot;&gt;a try&lt;/a&gt;! 🚀&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:53.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA+0lEQVQoz5WQC0vEMBCE+///nejpCQURa5/e2Sbpbl6dNS2IbSl3+LEsC8nMTpLVTV1UpQs+YpJfsFSibds8z7XWIQTvPQBZkUExqgGdQaOk0dJq1GkYwbMXEZlxZO+YKemnLRmXUT0H/Rr1KapzUKdAL6F8C4OZE0xxktGJYpk2O+czIBs8CpbaSs1SMVL/svLhwcvllDVqBjscke2esYNTXJt8DpRzbCzqdclfByKgJHQSLnIQ+8bmJI4++vTTAyb+pzhhiLrr1TpKPjHsuSPuyT5+8/tAhaKLoXGkNfc2M597fujt02BbQzsy3MQ51xr+NLY0NqVwdsMPg5yFPhk90QoAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;An encoded and decoded JWT token&quot; title=&quot;An encoded and decoded JWT token&quot; src=&quot;/static/db3b20516f3a1d9a8b0819b5871a5af5/5a190/image-2.png&quot; srcSet=&quot;/static/db3b20516f3a1d9a8b0819b5871a5af5/772e8/image-2.png 200w,/static/db3b20516f3a1d9a8b0819b5871a5af5/e17e5/image-2.png 400w,/static/db3b20516f3a1d9a8b0819b5871a5af5/5a190/image-2.png 800w,/static/db3b20516f3a1d9a8b0819b5871a5af5/95e27/image-2.png 1199w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Remove Specific Element From JavaScript Array]]></title><description><![CDATA[JavaScript does not currently offer any built-in array method(s) to remove a specific element. However, there are two main functions that…]]></description><link>https://dilshankelsen.com/remove-specific-element-from-array/</link><guid isPermaLink="false">https://dilshankelsen.com/remove-specific-element-from-array/</guid><pubDate>Mon, 29 Mar 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;JavaScript does not currently offer any built-in array method(s) to remove a specific element. However, there are two main functions that allow us to achieve the same outcome: &lt;code&gt;splice&lt;/code&gt; and &lt;code&gt;filter&lt;/code&gt;.&lt;/p&gt;&lt;h2 id=&quot;splice&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#splice&quot; aria-label=&quot;splice permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Splice&lt;/h2&gt;&lt;p&gt;Using the &lt;code&gt;splice&lt;/code&gt; method, we first need to identify the index of an element we wish to remove, typically with &lt;code&gt;indexOf&lt;/code&gt;. The &lt;code&gt;splice&lt;/code&gt; function is then called on the array with the 1&lt;sup&gt;st&lt;/sup&gt; argument being the index we&amp;#x27;d like to start at and the 2&lt;sup&gt;nd&lt;/sup&gt; argument being how many to remove.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; array &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;bananas&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;apples&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;pears&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; index &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; array&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;indexOf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;apples&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;splice&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Array [ &amp;quot;apples&amp;quot; ]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Array [ &amp;quot;bananas&amp;quot;, &amp;quot;pears&amp;quot; ]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;p&gt;You may have noticed 2 things in the code snippet above. When called, the function returns an array with the removed element. But even more importantly, it modifies the original array itself.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;filter&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#filter&quot; aria-label=&quot;filter permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Filter&lt;/h2&gt;&lt;p&gt;Unlike &lt;code&gt;splice&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt; does not alter the original array, which should make it the preferred option in my opinion. 🤓 Similarly, the function is called on the array and iterates through each element of said array. However, while iterating, each element is compared to a statement we define and is included in the new array if the statement equals to true.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; array &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;bananas&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;apples&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;pears&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; filteredArray &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; array&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; item &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!==&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;apples&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;filteredArray&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Array [ &amp;quot;bananas&amp;quot;, &amp;quot;pears&amp;quot; ]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In each iteration, we simply make sure that the item of the array is not equals to &lt;code&gt;apples&lt;/code&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Remove Gatsby Folder From Page Slug]]></title><description><![CDATA[By default, Gatsby's  gatsby-source-filesystem  plugin will create a page's slug based on its file path. However, what if you wish to use…]]></description><link>https://dilshankelsen.com/remove-folder-from-gatsby-slug/</link><guid isPermaLink="false">https://dilshankelsen.com/remove-folder-from-gatsby-slug/</guid><pubDate>Thu, 25 Mar 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;By default, Gatsby&amp;#x27;s &lt;code&gt;gatsby-source-filesystem&lt;/code&gt; plugin will create a page&amp;#x27;s slug based on its file path. However, what if you wish to use folders merely for organizing your files, therefore excluding them from the slug?&lt;/p&gt;&lt;p&gt;In my case, I wanted to organize my blog posts by year, yet have the year excluded from the slugs (for better SEO juice 😏). In other words, I wanted a slug like &lt;code&gt;/2021/remove-folder-from-gatsby-slug/&lt;/code&gt; to become &lt;code&gt;/remove-folder-from-gatsby-slug/&lt;/code&gt;.&lt;/p&gt;&lt;h2 id=&quot;regex-removal&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#regex-removal&quot; aria-label=&quot;regex removal permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Regex removal&lt;/h2&gt;&lt;p&gt;While I couldn&amp;#x27;t find a &amp;quot;plugin supported&amp;quot; solution, I came up with a workaround by removing the date from the slug right after it is generated and right before it is added to a page. So in my &lt;code&gt;gatsby-node.js&lt;/code&gt; file in the &lt;code&gt;onCreateNode&lt;/code&gt; function, I made the following change.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;diff&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-diff&quot;&gt;DIFF&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-diff&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;const slug = createFilePath({ node, getNode, basePath: &amp;quot;&amp;quot; })&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;createNodeField({&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token unchanged prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token unchanged line&quot;&gt;  node,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token unchanged line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token unchanged prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token unchanged line&quot;&gt;  name: &amp;quot;slug&amp;quot;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token unchanged line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token deleted-sign deleted prefix deleted&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token deleted-sign deleted line&quot;&gt;  value: slug,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token deleted-sign deleted line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token unchanged prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token unchanged line&quot;&gt;  // Remove any subfolder in the slug&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token unchanged line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted line&quot;&gt;  value: slug.replace(/^\/[0-9a-z]+\//, &amp;#x27;/&amp;#x27;),&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token inserted-sign inserted line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;})&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;What this regular expression does is basically look at the start of the slug for a &lt;code&gt;/&lt;/code&gt;, followed by at least one or more letters and/or numbers, and then another &lt;code&gt;/&lt;/code&gt;. If it finds a match, it replaces the find with a &lt;code&gt;/&lt;/code&gt;, hence the 2nd argument in the &lt;code&gt;replace()&lt;/code&gt; function. If it doesn&amp;#x27;t, nothing happens. 😄&lt;/p&gt;&lt;div&gt;&lt;p&gt;This regex only works on folders that do not include any special characters. For example, a folder with a hyphen like &lt;code&gt;bla-bla&lt;/code&gt; will not work.&lt;/p&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Local User Authentication With Passport And Express 4]]></title><description><![CDATA[Setting up user authentication can be a tricky business. But fret not, I've got you covered! In this tutorial, I'll show you how to set up…]]></description><link>https://dilshankelsen.com/user-authentication-with-passport-express/</link><guid isPermaLink="false">https://dilshankelsen.com/user-authentication-with-passport-express/</guid><pubDate>Tue, 23 Mar 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Setting up user authentication can be a tricky business. But fret not, I&amp;#x27;ve got you covered! In this tutorial, I&amp;#x27;ll show you how to set up your own user authentication from scratch with Passport.js and Express 4, specifically implementing the local strategy with Mongoose and MongoDB.&lt;/p&gt;&lt;h2 id=&quot;how-it-works&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#how-it-works&quot; aria-label=&quot;how it works permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;How it works&lt;/h2&gt;&lt;p&gt;Before we dive right into the code, let&amp;#x27;s take a minute to explain what we&amp;#x27;re exactly doing. I mean, the goal of this article is to &lt;em&gt;make you smarter&lt;/em&gt;, not more confused.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:480px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:54%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAABAAD/8QAFQEBAQAAAAAAAAAAAAAAAAAAAgH/2gAMAwEAAhADEAAAAS54ILJNqf/EABsQAAEEAwAAAAAAAAAAAAAAAAEAAgMREiIy/9oACAEBAAEFAo+pWtxV7k2gLX//xAAWEQADAAAAAAAAAAAAAAAAAAAQESH/2gAIAQMBAT8BcH//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/AT//xAAYEAACAwAAAAAAAAAAAAAAAAAAAREgIf/aAAgBAQAGPwJ6SnX/xAAbEAEAAgIDAAAAAAAAAAAAAAABABEhMVGBsf/aAAgBAQABPyEGgBuKzyOblXmoLtGQPDDFvs//2gAMAwEAAgADAAAAEGcP/8QAFxEAAwEAAAAAAAAAAAAAAAAAAAERIf/aAAgBAwEBPxBJgh//xAAWEQEBAQAAAAAAAAAAAAAAAAAAESH/2gAIAQIBAT8Qmq//xAAcEAEAAwACAwAAAAAAAAAAAAABABEhMUFRYbH/2gAIAQEAAT8QtteNFqX1L+952LgxRU+oAblhznY8tgwV44iYWxrEfJ//2Q==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Confused look&quot; title=&quot;Confused look&quot; src=&quot;/static/5363e08664a14c14b882f43f6fe3d528/7cc5e/confused.jpg&quot; srcSet=&quot;/static/5363e08664a14c14b882f43f6fe3d528/e07e9/confused.jpg 200w,/static/5363e08664a14c14b882f43f6fe3d528/066f9/confused.jpg 400w,/static/5363e08664a14c14b882f43f6fe3d528/7cc5e/confused.jpg 480w&quot; sizes=&quot;(max-width: 480px) 100vw, 480px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;First of all, what do we mean by &amp;quot;local strategy&amp;quot;? Well, Passport.js offers many &lt;a href=&quot;http://www.passportjs.org/packages/&quot; target=&quot;_blank&quot;&gt;login strategies&lt;/a&gt;, such as social media logins. However, the most common and simple strategy of them all, and the one we are considering in this tutorial, is using good ol&amp;#x27; login with a username and passport.&lt;/p&gt;&lt;p&gt;The great thing about Passport.js is that most of the user authentication process is already taken care off. Nevertheless, we will still remain responsible for:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Storing and authenticating any user information with Mongoose in our database. Fortunately, there&amp;#x27;s a &lt;a href=&quot;https://github.com/saintedlama/passport-local-mongoose&quot; target=&quot;_blank&quot;&gt;library&lt;/a&gt; that greatly simplifies this process.&lt;/li&gt;&lt;li&gt;The sessions and cookies, so users don&amp;#x27;t have to login every time they visit our page.&lt;/li&gt;&lt;/ul&gt;&lt;h2 id=&quot;setting-up-our-express-app&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#setting-up-our-express-app&quot; aria-label=&quot;setting up our express app permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Setting up our Express app&lt;/h2&gt;&lt;p&gt;To begin this tutorial, you&amp;#x27;ll first need Express 4 up and running. You can generate an Express app with the following command line in your project folder.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ npx express-generator &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;express-app-name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Subsequently, we need install all our dependencies with &lt;code&gt;$ npm install&lt;/code&gt; and make sure that everything works fine with &lt;code&gt;$ npm run start&lt;/code&gt;. You can find the app running by default on &lt;code&gt;http://localhost:3000/&lt;/code&gt;.&lt;/p&gt;&lt;h2 id=&quot;installing-the-mongodb-driver&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#installing-the-mongodb-driver&quot; aria-label=&quot;installing the mongodb driver permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Installing the MongoDB driver&lt;/h2&gt;&lt;p&gt;If you don&amp;#x27;t have MongoDB installed yet on your computer, you can read on &lt;a href=&quot;https://docs.mongodb.com/manual/administration/install-community/&quot; target=&quot;_blank&quot;&gt;their site&lt;/a&gt; how to do so.&lt;/p&gt;&lt;p&gt;Then, install the Node.js driver with&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; mongodb --save&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and launch your db with&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ mongod&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;If &lt;code&gt;mongod&lt;/code&gt; failed for you, double-check your installation guide as it may differ depending on your installation method and OS.&lt;/li&gt;&lt;li&gt;If you find yourself running into the error &lt;code&gt;Failed to set up listener: SocketException: Address already in use&lt;/code&gt;, run &lt;code&gt;$ killall mongod&lt;/code&gt; once before.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;h2 id=&quot;installing-the-right-dependencies&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#installing-the-right-dependencies&quot; aria-label=&quot;installing the right dependencies permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Installing the right dependencies&lt;/h2&gt;&lt;p&gt;Alright, it&amp;#x27;s about time we get to the interesting bits! 🤩&lt;/p&gt;&lt;p&gt;The &lt;a href=&quot;https://mongoosejs.com/&quot; target=&quot;_blank&quot;&gt;Mongoose ODM&lt;/a&gt; is the first library we&amp;#x27;ll need to install after setting up our basic project template.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; mongoose --save&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;For Passport.js, on the other hand, we will need to install several dependencies.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; passport passport-local passport-local-mongoose --save&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And lastly, to handle sessions and cookies we require &lt;a href=&quot;https://github.com/expressjs/session&quot; target=&quot;_blank&quot;&gt;express-session&lt;/a&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; express-session --save&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;From the terminal, we next move on to our &lt;em&gt;app.js&lt;/em&gt; file and include the following changes&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; mongoose &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;mongoose&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; passport &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;passport&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; session &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;express-session&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;LocalStrategy&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;passport-local&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access maybe-class-name&quot;&gt;Strategy&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Easy so far, ain&amp;#x27;t it? 👍&lt;/p&gt;&lt;p&gt;Oh! And before I forget it, we also need to connect our app to our database.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;mongoose&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;connect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;mongodb://localhost/&amp;lt;database-name&amp;gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;useNewUrlParser&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;including-middleware&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#including-middleware&quot; aria-label=&quot;including middleware permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Including middleware&lt;/h2&gt;&lt;p&gt;Our next step is to include any necessary middleware, which we certainly got a few of. Starting with &lt;a href=&quot;https://github.com/expressjs/session&quot; target=&quot;_blank&quot;&gt;express-session&lt;/a&gt;:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;session-id&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;secret&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;123-456-789&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;saveUninitialized&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;resave&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Immediately afterwards, we initialise our passport module and connect it to our session module.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;passport&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;passport&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Last but not least, we configure our Passport/Passport-Local modules using &lt;a href=&quot;https://github.com/saintedlama/passport-local-mongoose#simplified-passportpassport-local-configuration&quot; target=&quot;_blank&quot;&gt;passport-local-mongoose&lt;/a&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;passport&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;LocalStrategy&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;authenticate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;passport&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;serializeUser&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;serializeUser&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;passport&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;deserializeUser&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;deserializeUser&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;creating-our-user-model&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#creating-our-user-model&quot; aria-label=&quot;creating our user model permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Creating our User model&lt;/h2&gt;&lt;p&gt;If you&amp;#x27;ve paid any particular attention to the Passport/Passport-Local configuration code, you&amp;#x27;ll notice that it relies on a certain &lt;code&gt;User&lt;/code&gt; module. This module is essentially our Mongoose user schema with which we sign up and login all our users.&lt;/p&gt;&lt;p&gt;Let&amp;#x27;s continue and create a file called &lt;em&gt;user.js&lt;/em&gt; in a folder called &lt;em&gt;models&lt;/em&gt;. Our file will contain the following.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; mongoose &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;mongoose&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; passportLocalMongoose &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;passport-local-mongoose&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;UserSchema&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;mongoose&lt;/span&gt;&lt;span class=&quot;token class-name punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Schema&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;unique&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;required&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;UserSchema&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;plugin&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;passportLocalMongoose&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; mongoose&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;User&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;UserSchema&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;exports&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In our user model, we don&amp;#x27;t need to explicitly define a password or even a username as it is automatically taken care of by &lt;a href=&quot;https://github.com/saintedlama/passport-local-mongoose#plugin-passport-local-mongoose&quot; target=&quot;_blank&quot;&gt;passport-local-mongoose&lt;/a&gt;. Pretty neat, right? However, in this example, I took the liberty of adding some requirements to the username. And in case you were worried about security, our library also takes care of this by hashing and salting our passwords. Double neat, right? 😲&lt;/p&gt;&lt;p&gt;After doing this, we have to &lt;em&gt;require&lt;/em&gt; our model into our &lt;em&gt;app.js&lt;/em&gt; file in order to ensure our Passport/Passport-Local configurations work properly.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;./models/user&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;configuring-our-routes&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#configuring-our-routes&quot; aria-label=&quot;configuring our routes permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Configuring our routes&lt;/h2&gt;&lt;p&gt;Still awake? Don&amp;#x27;t worry, we&amp;#x27;re almost done! 🤞&lt;/p&gt;&lt;span class=&quot;text-center&quot;&gt;&lt;p&gt;  &lt;img src=&quot;/static/sleeping-448b6d63ae113e3d447f80de211a6b14.gif&quot; alt=&quot;Falling asleep&quot;/&gt;&lt;/p&gt;&lt;/span&gt;&lt;p&gt;Moving on, let&amp;#x27;s open up the existing &lt;em&gt;users.js&lt;/em&gt; file created by Express in the routes folder, and include the following changes.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;../models/user&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; passport &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;passport&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;router&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;/signup&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;req&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; res&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; next&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;register&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; req&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    req&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; user&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        res&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;statusCode&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        res&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;setHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Content-Type&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;application/json&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        res&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; err&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        passport&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;authenticate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;local&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;req&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; res&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;findOne&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; req&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; person&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;            res&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;statusCode&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;            res&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;setHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Content-Type&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;application/json&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;            res&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;              &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;success&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;              &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Registration Successful!&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In this chunk of code, we&amp;#x27;re signing up a user whenever they access &lt;em&gt;/users/signup&lt;/em&gt; with a POST request. As I&amp;#x27;ve made it a requirement that each username is unique, our database will throw an error whenever we try to register a duplicate, thus never reaching &lt;code&gt;passport.authenticate(&amp;#x27;local&amp;#x27;)&lt;/code&gt;, which then automatically logs in the newly created user.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;router&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;/login&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; passport&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;authenticate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;local&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;req&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; res&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;findOne&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; req&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; person&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    res&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;statusCode&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    res&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;setHeader&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Content-Type&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;application/json&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    res&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;success&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;You are successfully logged in!&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Similarly to before, whenever a user accesses &lt;em&gt;/users/login&lt;/em&gt; with a POST request, Passport.js takes care of all the bothersome authentication details for us when we reach &lt;code&gt;passport.authenticate(&amp;#x27;local&amp;#x27;)&lt;/code&gt;. Furthermore, Passport.js automatically creates a cookie and a session for the user logging in.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;router&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;/logout&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;req&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; res&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; next&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;req&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    req&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;logout&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    req&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;destroy&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        res&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;clearCookie&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;session-id&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        res&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;You are successfully logged out!&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; err &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Error&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;You are not logged in!&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    err&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;403&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Last but not least, a user can log out by accessing &lt;em&gt;/users/logout&lt;/em&gt; with a POST request. We&amp;#x27;re using POST instead of GET as &lt;a href=&quot;https://stackoverflow.com/questions/3521290/logout-get-or-post&quot; target=&quot;_blank&quot;&gt;the browser&lt;/a&gt; might otherwise try to cache the response.&lt;/p&gt;&lt;p&gt;In this scenario, next to logging out the user with &lt;code&gt;req.logout()&lt;/code&gt;, we have to manually destroy a user&amp;#x27;s session and clear any cookies. We also provide some error handling should someone try to logout who&amp;#x27;s not logged in.&lt;/p&gt;&lt;p&gt;Oof, we&amp;#x27;ve finally made it! Why not give your code a try with &lt;a href=&quot;https://www.getpostman.com/&quot; target=&quot;_blank&quot;&gt;Postman&lt;/a&gt;? 👏&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Schedule Netlify Builds For Your Website]]></title><description><![CDATA[At the time of this writing, Netlify does not offer any feature that allows you to schedule builds and deployments. Fortunately, there are…]]></description><link>https://dilshankelsen.com/schedule-netlify-builds/</link><guid isPermaLink="false">https://dilshankelsen.com/schedule-netlify-builds/</guid><pubDate>Mon, 22 Mar 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;At the time of this writing, Netlify does not offer any feature that allows you to schedule builds and deployments. Fortunately, there are free tools that allow us to accomplish the same thing.&lt;/p&gt;&lt;div&gt;&lt;p&gt;I previously wrote an article on how to create and schedule &lt;a href=&quot;/create-future-blog-posts-in-gatsby&quot;&gt;future blog posts&lt;/a&gt; in Gatsby, which complements this one.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;add-a-netlify-build-hook&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#add-a-netlify-build-hook&quot; aria-label=&quot;add a netlify build hook permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Add a Netlify build hook&lt;/h2&gt;&lt;p&gt;The first step to achieving out goal is to create a netlify build hook. You can do so under &lt;strong&gt;Site settings &amp;gt; Build &amp;amp; deploy &amp;gt; Continuous deployment &amp;gt; Build hooks&lt;/strong&gt;. Simply give your hook a name and which git branch it should build, and Bob&amp;#x27;s your uncle! 👴&lt;/p&gt;&lt;p&gt;You should then see a link similar to this:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-text&quot;&gt;TEXT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-text&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;https://api.netlify.com/build_hooks/xxxxxxxxxxxxxx&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;p&gt;Make sure you have under &lt;strong&gt;Continuous deployment &amp;gt; Build settings&lt;/strong&gt; &amp;quot;Activate builds&amp;quot; selected or else the build hook will not work.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;trigger-the-hook&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#trigger-the-hook&quot; aria-label=&quot;trigger the hook permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Trigger the hook&lt;/h2&gt;&lt;p&gt;There are a plethora of tools you can use to trigger your build hook, for example &lt;a href=&quot;https://zapier.com/&quot; target=&quot;_blank&quot;&gt;Zapier&lt;/a&gt;, &lt;a href=&quot;https://ifttt.com/&quot; target=&quot;_blank&quot;&gt;IFTTT&lt;/a&gt; or &lt;a href=&quot;https://www.easycron.com/&quot; target=&quot;_blank&quot;&gt;EasyCron&lt;/a&gt;. What you choose is more a choice of preference as they all get the job done, with the free tier being completely sufficient for even daily builds.&lt;/p&gt;&lt;p&gt;In my case, I went for IFTTT.&lt;/p&gt;&lt;p&gt;Create a new applet and add a &amp;quot;If This&amp;quot;.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:695px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:70.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAIAAACgpqunAAAACXBIWXMAAAsTAAALEwEAmpwYAAACo0lEQVQoz51TS0waURQdQBKJ2KQ20aRNaFx1UxeybEjUvRtiDH5YstWVmyaNiCKgggiCgARGMDSWVhrxEzV8nA7IRyURNcSAiCQti640MYAL6eGTtOuezNy8e9458+597w1RKpfwlMvlh4eHcDicSqXS6fTp6Wkul7u7u7u+vk5nMj9/FUBeXV3d3GRKkD89wYABUalUnp+fEYvFovvrt89f3E7nularPTw8XF1dNVssa6Q9cRIn7TaT2bK15a2L6yAU6kmFVT0zMyOXy2eln+YmPypn5UqVSqfTGQyGeZVSodEp1r6rNEvaBeXCnBIyE7muXlySTU0RxJv3jNZWoo6WDuLtB6KJ08RistnsKsFpJppfMNrfESxOVyvRwSZauNyXXYJXr3kMTHvzxUAoSh0FKeoHlftNlSpUIhWmKZqmA4FANBajKYquzh4lT0JnUToSjUbDIb/P5/V6qz1fXFygQnRo0WstGoXJoDeb0Spps9lMJpPRaNTp9WaLeXFJP7+gAdRqdT6fr/aMVzY9jRJYLBaTycSAx+NJpVI+nz8+Pj42NiYQCPr6+sDzu7uNBsPy8nJPT8/KykrDjD3419zZ2TkxMdHW1gYPzBKJRCaTcbnc3t5eNOLz+UQiEcppmHd2doRCoVgsHhkZGR0dHR4eHhwcRBwaGsJAVAOmBgYGkILs7++PxWIN83+jas5ms1h8f38fVcXjcb8/sL29vbu7CzIYDIJBqWAgODg4QNzb2ysUCg1zMpm0Wq12u31zcxM7n0icRSKR4+PjUCiE87i8vMRtxc09Pz/3eDyQ4RSwXsMMA3Kn0+lyuaDGh3HIfr8fN3RjYwMMVgODKtxut8PhIEnyr/n+/h5JroZsDfgNbmq4vb2tM5lMpp5Cg/j4+AjjH9GzHJJHEdKKAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Interface of create a new applet in IFTTT&quot; title=&quot;Interface of create a new applet in IFTTT&quot; src=&quot;/static/57cc4897a42efca974656d860b2aa911/83b75/image-1.png&quot; srcSet=&quot;/static/57cc4897a42efca974656d860b2aa911/772e8/image-1.png 200w,/static/57cc4897a42efca974656d860b2aa911/e17e5/image-1.png 400w,/static/57cc4897a42efca974656d860b2aa911/83b75/image-1.png 695w&quot; sizes=&quot;(max-width: 695px) 100vw, 695px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;No thanks, I shall stay with the free tier 😛&lt;/figcaption&gt;&lt;p&gt;After that, you need to select a service which should go after the name &amp;quot;Date and Time&amp;quot;.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:43.50000000000001%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAACEUlEQVQoz6WQW0jTcRTHf5QaRhfLCgk1sJeyiSEi3SwGSdEegthLuFoirNKue1l4aWM99ZB0IchZOlwW3ib854WMNYMREZRr1S5JkYseTNJwZG5u+/RvtNhj0BcO58v3nO/hyxH8QSKRSPZYLIbD4WBoaAiXy4Xb7cbv9ye50+lEkiSCweBfT8qXgkiJ6YNAIMD4+Dherxefz8fk5CQejyep/9ZCoRDpQdJLJBYXScip4nL/OT9POBzmXxCJRIhGFkj6UxWPI9KXfkSjfJ+d5X8gZrolvvUNMmcfwTs8ijQyzIDdzqBDov/JC9oev+XuqIfOMT/3B0awtt/DYrFgtdl43dvFTL+Nqd5OpnqsLHz+hPCsLOZdiYqJ9aWcL9zKAfURbl6/waEqJZkVGvJ1HRTXtSHUtxA5W8hZnoVKpUIIgSFT4M0TuAszeJotmB54gHiTv5OJgzo+Fu3hwuYSzC3X5J/G0Z+tY8VuLQr9QyoNNlYfb0WsU7B/7y4aGxqTB025S/CXbuBVVRnPCpYyLXUjvBvL8e04zIeiSi4WFlOu3Mdp3UkqyraTq6xljdZCQc1tOWk7Ik/WVmWj2KZAZC7DkCHwrJUTbspiTOZf7V2IUH0TX4wtTOmv0HPJSIPZxOWmZszGZs5c7eBUq4v6O4+oa3Oh1ZvQao6i0WioPlFDn66a0LljvNfXEtSpmXv5nF9nGxxz7EI0MwAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;IFTTT applet date and time service&quot; title=&quot;IFTTT applet date and time service&quot; src=&quot;/static/00f7b0c993ac880d4949bc63cb6b7fa7/5a190/image-2.png&quot; srcSet=&quot;/static/00f7b0c993ac880d4949bc63cb6b7fa7/772e8/image-2.png 200w,/static/00f7b0c993ac880d4949bc63cb6b7fa7/e17e5/image-2.png 400w,/static/00f7b0c993ac880d4949bc63cb6b7fa7/5a190/image-2.png 800w,/static/00f7b0c993ac880d4949bc63cb6b7fa7/ec3e2/image-2.png 997w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;Then you have the option to choose between the frequency of your posts. As I want to trigger a build every Monday and Thursday, I opt for &lt;em&gt;Every day of the week at&lt;/em&gt;. Afterwards, you will have have to select a timezone for your cron job as well as define the exact time and day of its trigger. No rocket science so far, right? 🚀&lt;/p&gt;&lt;p&gt;You&amp;#x27;re greeted once again by the previous interface, only this time you choose &amp;quot;Then That&amp;quot;.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:614px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:83%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAARCAIAAABSJhvpAAAACXBIWXMAAAsTAAALEwEAmpwYAAADS0lEQVQ4y3VT60tTYRg/iIWXNZeGgqLmcZtO3XStnW2J2SDygpdNpA+a+iFNSEW8TEUWUoh+iPK6rSH1B4j4yQ+ClgmCJuLaXSczFXdRLAI1BS+n32aCFP0453mf53ee533f876/hzg/P6dpem9vb21t7fDw8Pj4eHd3F87p6enBwcEvP46OjuCD3N/fPzk5oS9BnJ2dYZidne3p6enu7lar1SqVamhoaHR0tKOjQ9XW9vbNm8+fPr4A1Oquri6TyYT8iyrCubHudDqxrMVms686jEajzWZbXV2dm5szGAxmkwm0Y8tttZiNZqvRYnc4HOvrzu0NXwnBuhXOZDJZzBs3Q66zgq9FsMIQJpIkn89PSU2NYDFZyVS4vJwRy02KChEn3mSEhiSnCUSPn6ekpBAkSSYmkpxUAaf2NbtRzxHd43HZFCWJi4tLT0+/c0dI8vi8u7K4JH6hhHz2kHM7gbz/QE49Ks7NzSU8Xq/X7fb83PfQtJem3d9/INzZ2XGD9Hh8jmvbvb3lcbkQ4/V6Qbtdm9+2NjeJwcHBYY1Gr9PqBt4OvO7VDg9ptVqNRuOz2j/Q6d4Bejz6d3BHRkbef/iAeQmBQNDX1/f0aTWbwy1WlmQIhdhtRkYGrFAohAOL/0/2g8fjcblcmUwmlcoWFr4QSqVyZmamuro6Ly9vcnKysrKyvLz8iR9wysrKqqqqSktLs7Ky7vuRnZ0NH/W4F0IkEhUUFIBFnJmZSVGUWCymLiH2QyKR3LsCpEmlUl+x2WzGYLFYrFYrrO0SCA2Gr3DsdvtF+BcgO4L+D6DT8fHxq2L8F8TKysry8vLS0hImg2igLTCwi4uL09PTWBOSutgLHGgTydgpEjA7AeTn5+OIfIohiODg4NDQUAaDUVJSUlFRERMTExAQEBUVFR0dHRQUlJaWhgPD+SNtfn7eV1xTU4OL1ev1L1+97O3txc0jqba2VqfT9ff3t7S0jI2NFRYWxsbGosPQCFhTLpdPTEz4iuPj49lsSJJSKBU5OTn4EBkZCQYSwMEqFIqioqKEhITAwMC6urqmpqbm5uawsLCpqSlC7Qe6r729HazKDzCdnZ2+llSpWltbsbja348NDQ2NjY319fUIXS7Xb4e/KjfqWgloAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Interface of adding Then That in IFTTT applet&quot; title=&quot;Interface of adding Then That in IFTTT applet&quot; src=&quot;/static/e3147dd4aeeed726b1d723d5a3f18adf/e9131/image-3.png&quot; srcSet=&quot;/static/e3147dd4aeeed726b1d723d5a3f18adf/772e8/image-3.png 200w,/static/e3147dd4aeeed726b1d723d5a3f18adf/e17e5/image-3.png 400w,/static/e3147dd4aeeed726b1d723d5a3f18adf/e9131/image-3.png 614w&quot; sizes=&quot;(max-width: 614px) 100vw, 614px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;However, this time you select the &amp;quot;Webhooks&amp;quot; service.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:662px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:66.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAAAsTAAALEwEAmpwYAAACTklEQVQoz3WQX2hSURzHD6scxGYk0YpgRE899BDSS4MepKd0PUT00ENv0T/I6FUIEVOrrT9uVFREG7RwikvXdF61GTEdLFOmu9bSWIxy3nvV6726+ed6z+le1xUp+vA9P87v8PvAOQcgiXq9Ho/H0+k0wzD5fL5QKFRalEoliqKy2Wwmk6FpulqtthUgLAihUAmCMBqNOp3ObDYbDAa9Xu92u202m9VqtVgswy1MJlMsFhOGeZ6X5JYvVl6kKSDU9l7oYIvWCeoAdDbw/whaZ7s1DBqNRjaX+7G2toynOJ7/k+Y/4WE7TYgqmyKA47iNMsvQRfLXz60XSPkLviOwyQn/WxevnWtAfIP/VkOpMrfMcniZw/OVFMUmSSZBMF8oFhdC83gJpVpJ0mi1Ir1Zu8yCdzkFRuycJXdjZK+PlD8NdVuc/Y99h55gOyxT8odY70StZxIp7HCPA22bQKqAJF9JsuDt+kC4eCZa6n9PgRkCPPDvvT99DVu67k8ceR4At6e7xmtgDGk/wdMhBJ6h475OeWpdtVC8mmQPijIJhmZPjM9d9MZGo98veaPA6AKvagMYGl2Bj76iA06k9EjyDbws85D7AlS3l1BglNxHbh8JAqND61+yRFbkw66uux4wVlN60YUwPD+P9jvRMa8kX06wfQFKuPbhD4WjHwt9fgoM+cCtNz333LI7LqC3AaMbvKjKXiOFA+6yI/ASKWckeWR1U7VAn/tcOhsVo16kT7niGtu8xh4ZtEc0k2G1fVEdrKvnkJDBEDoZRDejovwbxCBnxLetl18AAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;IFTTT applet webhooks service&quot; title=&quot;IFTTT applet webhooks service&quot; src=&quot;/static/8c0bb5a6ca83792f7c2956669bdb0d23/be86f/image-4.png&quot; srcSet=&quot;/static/8c0bb5a6ca83792f7c2956669bdb0d23/772e8/image-4.png 200w,/static/8c0bb5a6ca83792f7c2956669bdb0d23/e17e5/image-4.png 400w,/static/8c0bb5a6ca83792f7c2956669bdb0d23/be86f/image-4.png 662w&quot; sizes=&quot;(max-width: 662px) 100vw, 662px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;Click through some stuff and you&amp;#x27;ll finally land on this page.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:465px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:147.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAeCAIAAACjcKk8AAAACXBIWXMAAAsTAAALEwEAmpwYAAAEhUlEQVQ4y5VUWWwbVRR97haWspW2LKV7KeKHH4QQX0ggWkAgVISqVK2KEOKHjyYI0VSVEPyBxDd8QVpSO0kdx3FSutAmdVM7cTYvM7bjxG4aErtxMvZk3Dgeu/HMHO6b6YJaBO3o6Pj4zh37zb33XPZICza6scoB1gR24gGwzA72tBO7ug1i1mQst1PIWHaCoK+wY4WDC/q63H4PHIaNHn79jB6YqQzOlEVZG5mtCHmNIMr60LVScLYSUxDJVyO5OwgTy7owu7j7XIXV+rAoZU573H293mRc7O05PzLQJ4wMUqT73OlENOK7eKEwLwMwDMNi/qEWvvAW2d7LKCqyPzB42d8nRmPBULh/YDAkiPGx8VBEEKKxeGKM9ORfU3SlUql0Ol3VDVSKn19aZHu8GrRSuSCrSq5anNcWFRJlJaeXCjcW5qtFxVAXKoqkFmRZuS7lZaVwvVLVAe0z3xJb6zRqe7V9PmO/z6i9zHHAz7G3l0f2mcGPLxlfDxvu1GJLYqEtqdpjhY5k8c0/dbaqGc+0Ya0TVPA1Ti4ebzXbcAJUfJvJrBF7Lhr+yOgfAbHTF/T4QoOx5EG/zijviZN48iQea8Wak1jfxgU9sNJBrboFymnF9k5s68RWD7Z5sN2D1a1gTzmxsR3PuuifDfa7OSr3TosVPG78E3Qo9pwLH3jxYS/e8WKdeX7rFe4CBemuhfUuDv46b3djsrAUSivhaXlyvjxbRlb9H0wvaLnFG/v9YJ8OYGkh7z3l8nY5i4X87WH418u6pci5+YlogwD2xlkMy+jLwT+rB3Lokzh8c+ibg19Cv4QAIcdh6f45rsUC3usBq3FgU7vxgsvY1oFNbmx243kX5w0uLrZ28HJucVMOKGFzB8iFK+3GSu4fMBt3ktlPx01R08wziB9q5kw9s7DK1KbzboKtbcPLncYrXdqWdo14h1uzNersNwsaa7zFFix9zCDmraKjfivixxS+S+CnFH5Iol7AoTDqBNSLnC1xO0L4MohDIWzpANvrA6rqlfFEIiokE6PE01fGs1MTUoZMNJadunp1LD49MS5nMxSfSMQoouSlSv5aXRCs1g8slXrOnhKG+pNCMDYSGBdG0qnRyYQYHQ5MxCNXR8VEaGgqGU9Fw6OhoUR4WJGy1fmZenqYinlUwPcJHI3hcAxH4miI4hsRh6M4EuOaREOMRxqsSAxfhXE4gh0eMKre6hbjYYe2osksUqN+d3nuBRXsmDnbj7bw0SWmNtQ0PwDIbYz89NZ5vHoaZClb0y0D3Qd48mtn+LgN5DB436DkUF5/t8dgBwcgTyWl7Mx/W8LyhbU8aYORmer6y2x3D+aUYqag5sq6pGo5VZNM3BaEvKplS1q6hGtlZFSkVUgl7ZNLOqNX39nF6/6ihzOtmJc6sdPDvUGR7R08uM4J2oFdsVn7SKZNnHMEM2fG8u/TsWn6KY+6TR7a0M6NRWvQcohlA1pg7Dh2XTBahyZ+9ad+7o7+4o07hyc/8poLsMZxxz0klpur02axKWzmb9lawFqwrBU2Wn3NPPI3JXEIjRfMK1kAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;IFTTT applet webhooks service configuration&quot; title=&quot;IFTTT applet webhooks service configuration&quot; src=&quot;/static/fb8d754be859ddcc3af69a99192e64f8/9ff85/image-5.png&quot; srcSet=&quot;/static/fb8d754be859ddcc3af69a99192e64f8/772e8/image-5.png 200w,/static/fb8d754be859ddcc3af69a99192e64f8/e17e5/image-5.png 400w,/static/fb8d754be859ddcc3af69a99192e64f8/9ff85/image-5.png 465w&quot; sizes=&quot;(max-width: 465px) 100vw, 465px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;In the URL field, paste your build hook URL that we &lt;a href=&quot;#add-a-netlify-build-hook&quot;&gt;discussed earlier&lt;/a&gt; and set &amp;quot;Method&amp;quot; to &lt;code&gt;POST&lt;/code&gt;. For &amp;quot;Content Type&amp;quot;, select &lt;code&gt;application/x-www-form-urlencoded&lt;/code&gt; and lastly for &amp;quot;Body&amp;quot; enter &lt;code&gt;{}&lt;/code&gt;. A few more clicks et voilà, you&amp;#x27;re done!&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Create Future Blog Posts In Gatsby]]></title><description><![CDATA[As any blogger on a schedule will tell you, writing content in advance is important. Unfortunately, this isn't something that comes out-of…]]></description><link>https://dilshankelsen.com/create-future-blog-posts-in-gatsby/</link><guid isPermaLink="false">https://dilshankelsen.com/create-future-blog-posts-in-gatsby/</guid><pubDate>Thu, 18 Mar 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;As any blogger on a schedule will tell you, writing content in advance is important. Unfortunately, this isn&amp;#x27;t something that comes out-of-the-box with Gatsby and Markdown, so some tinkering is needed.&lt;/p&gt;&lt;h2 id=&quot;filtering-out-future-dates&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#filtering-out-future-dates&quot; aria-label=&quot;filtering out future dates permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Filtering out future dates&lt;/h2&gt;&lt;p&gt;The idea is relatively simple. With the help of GraphQL, whenever we build our site, we can choose to not include any posts that are newer than the current time and day.&lt;/p&gt;&lt;p&gt;In our markdown file, we define the publishing date of the blog post in the frontmatter, i.e. the part between the dashed lines 😋.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;markdown&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-markdown&quot;&gt;MARKDOWN&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-markdown&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token front-matter-block punctuation&quot;&gt;---&lt;/span&gt;&lt;span class=&quot;token front-matter-block&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token front-matter-block&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token front-matter-block front-matter yaml language-yaml key atrule&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;token front-matter-block front-matter yaml language-yaml punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token front-matter-block front-matter yaml language-yaml&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token front-matter-block front-matter yaml language-yaml string&quot;&gt;&amp;quot;How to create future blog posts in Gatsby&amp;quot;&lt;/span&gt;&lt;span class=&quot;token front-matter-block front-matter yaml language-yaml&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token front-matter-block front-matter yaml language-yaml&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token front-matter-block front-matter yaml language-yaml key atrule&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;token front-matter-block front-matter yaml language-yaml punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token front-matter-block front-matter yaml language-yaml&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token front-matter-block front-matter yaml language-yaml datetime number&quot;&gt;2021-03-18&lt;/span&gt;&lt;span class=&quot;token front-matter-block&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token front-matter-block&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token front-matter-block punctuation&quot;&gt;---&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;However, GraphQL doesn&amp;#x27;t offer any time-based filter. So instead, we need to create a new dynamic field for each blog post that evaluates to &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; depending on the post date. And how do we do that? While Gatsby creates its nodes, of course!&lt;/p&gt;&lt;p&gt;So in your &lt;code&gt;gatsby-node.js&lt;/code&gt; file, you may add the following:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;exports&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method-variable function-variable method function property-access&quot;&gt;onCreateNode&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; node&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; getNode&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; actions &lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; createNodeField &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; actions&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;internal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Mdx&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Create scheduled post&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; publishDate &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; node&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;frontmatter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; isScheduledPost &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;publishDate &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;publishDate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getTime&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;createNodeField&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      node&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;isScheduledPost&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; isScheduledPost&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;While Gatsby is busy creating its nodes, we want to make sure our &lt;code&gt;isScheduledPost&lt;/code&gt; field is only created on nodes belonging to markdown files, hence the node type verification. If we happen to not find any publishing date associated with a markdown file, we will assume it&amp;#x27;s a draft and therefore schedule it forever (until you remember to add a date, naturally 😉).&lt;/p&gt;&lt;p&gt;The next step is to query blog posts during the page creation where &lt;code&gt;isScheduledPost&lt;/code&gt; evaluates to &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;exports&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method-variable function-variable method function property-access&quot;&gt;createPages&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; actions&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; graphql &lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; result &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;graphql&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;    allMdx(&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;      sort: { fields: [frontmatter___date], order: DESC },&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;      filter: {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;        fields: { isScheduledPost: { eq: false } }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;      }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;    ) {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;      edges {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;        node {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;          fields {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;            slug&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;          }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;          frontmatter {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;            title&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;            date&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;          }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;        }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;      }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;    }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;More likely than not, you probably have an overview page that lists all your blog posts. Evidently, you&amp;#x27;ll also want to filter out any scheduled posts there. So similarly, in your page query:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; pageQuery &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; graphql&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql keyword&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql definition-query function&quot;&gt;BlogIndexQuery&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql property-query&quot;&gt;allMdx&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql attr-name&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql attr-name&quot;&gt;fields&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql property&quot;&gt;frontmatter___date&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql attr-name&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql constant&quot;&gt;DESC&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql attr-name&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql attr-name&quot;&gt;fields&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql attr-name&quot;&gt;isScheduledPost&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql attr-name&quot;&gt;eq&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql object&quot;&gt;edges&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql object&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql property&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql property&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql property-query&quot;&gt;excerpt&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql attr-name&quot;&gt;pruneLength&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql number&quot;&gt;180&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql attr-name&quot;&gt;truncate&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql property&quot;&gt;timeToRead&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql object&quot;&gt;fields&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql property&quot;&gt;slug&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql object&quot;&gt;frontmatter&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql property-query&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql attr-name&quot;&gt;formatString&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql string&quot;&gt;&amp;quot;DD/MM/YYYY&amp;quot;&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql property&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string graphql language-graphql&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You may have other instances where you need to filter out scheduled posts, such as an RSS feed. Make sure to apply the filter in those cases, too.&lt;/p&gt;&lt;h2 id=&quot;access-during-development&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#access-during-development&quot; aria-label=&quot;access during development permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Access during development&lt;/h2&gt;&lt;p&gt;But what if you&amp;#x27;re like me and want to see your beautiful blog posts in action before letting it off into the wild? 🥺 Well, I got you covered.&lt;/p&gt;&lt;p&gt;By default, Gatsby supports 2 environments:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Development, when you run &lt;code&gt;gatsby develop&lt;/code&gt;, and&lt;/li&gt;&lt;li&gt;Production, when you run either &lt;code&gt;gatsby build&lt;/code&gt; or &lt;code&gt;gatsby serve&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;And it&amp;#x27;s as simple as switching out your GraphQL query depending on the environment. One that includes a filter (i.e.&lt;code&gt;productionQuery&lt;/code&gt;) and one that doesn&amp;#x27;t (i.e. &lt;code&gt;developmentQuery&lt;/code&gt;).&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// ... (gatsby-node.js)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; developmentQuery &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; productionQuery &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; result&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;NODE_ENV&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;production&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  result &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;graphql&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;productionQuery&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  result &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;graphql&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;developmentQuery&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;//...&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;refactoring-the-code&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#refactoring-the-code&quot; aria-label=&quot;refactoring the code permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Refactoring the code&lt;/h3&gt;&lt;div&gt;&lt;p&gt;Unfortunately, string interpolations do not work in &lt;code&gt;graphql&lt;/code&gt; tags, except for those in a &lt;code&gt;gatsby-__.js&lt;/code&gt; file.&lt;/p&gt;&lt;/div&gt;&lt;p&gt;If you&amp;#x27;re like me, you might opt for a more elegant solution. In a separate file, I declared the following:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;exports&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method-variable function-variable method function property-access&quot;&gt;allBlogPostsQuery&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;queryBody&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; process&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;NODE_ENV&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;production&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getPublishedPosts&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;queryBody&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getAllPosts&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;queryBody&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;getAllPosts&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;queryBody&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;  {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;    allMdx(sort: { fields: [frontmatter___date], order: DESC }) {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token template-string interpolation&quot;&gt;queryBody&lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;    }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;  }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;getPublishedPosts&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;queryBody&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;  {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;    allMdx(&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;      sort: { fields: [frontmatter___date], order: DESC },&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;      filter: {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;        fields: { isScheduledPost: { eq: false } }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;      }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;    ) {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token template-string interpolation&quot;&gt;queryBody&lt;/span&gt;&lt;span class=&quot;token template-string interpolation interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;    }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;  }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You may then go on and use it in your &lt;code&gt;gatsby-__.js&lt;/code&gt; file.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; result &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;graphql&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;allBlogPostsQuery&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string string&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;    edges {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;      node {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;        fields {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;          slug&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;        }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;        frontmatter {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;          title&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;          date&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;        }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;      }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;    }&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string string&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Iterate Through JavaScript Objects]]></title><description><![CDATA[Unlike arrays, JavaScript objects are not iterable. This means functions and statements like  map() ,  forEach()  or  for..of  will not work…]]></description><link>https://dilshankelsen.com/iterate-through-javascript-object/</link><guid isPermaLink="false">https://dilshankelsen.com/iterate-through-javascript-object/</guid><pubDate>Mon, 15 Mar 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Unlike arrays, JavaScript objects are not iterable. This means functions and statements like &lt;code&gt;map()&lt;/code&gt;, &lt;code&gt;forEach()&lt;/code&gt; or &lt;code&gt;for..of&lt;/code&gt; will not work. If you attempt to do so, you&amp;#x27;ll be greeted with lovely errors such as &lt;code&gt;TypeError: &amp;lt;Object&amp;gt;.forEach is not a function&lt;/code&gt; or &lt;code&gt;TypeError: &amp;lt;Object&amp;gt; is not iterable&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;However, there are a few ways to loop through an object, depending on whether you want its keys or values.&lt;/p&gt;&lt;p&gt;Let&amp;#x27;s take this object as an example:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; daysInMonth &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;January&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;February&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;March&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;iterating-over-keys&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#iterating-over-keys&quot; aria-label=&quot;iterating over keys permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Iterating over keys&lt;/h2&gt;&lt;p&gt;You can easily obtain the keys/properties of an object with &lt;code&gt;Object.keys()&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;keys&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;daysInMonth&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Array(3) [ &amp;quot;January&amp;quot;, &amp;quot;February&amp;quot;, &amp;quot;March&amp;quot; ]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As the output is an iterable object (i.e. array), you may then apply one of the above-mentioned methods or statements. However, there is also a way to directly iterate over your object using &lt;code&gt;for...in&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; month &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; daysInMonth&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;month&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// January&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// February&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// March&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;iterating-over-values&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#iterating-over-values&quot; aria-label=&quot;iterating over values permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Iterating over values&lt;/h2&gt;&lt;p&gt;Similarly, to get the values of an object, you may use &lt;code&gt;Object.values()&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token known-class-name class-name&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;daysInMonth&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Array(3) [ 31, 28, 31 ]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Yet, to loop through the values, I prefer the following method:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; month &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; daysInMonth&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;daysInMonth&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;month&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// 31&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// 28&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// 31&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Add Lottie Animation Files In React]]></title><description><![CDATA[Lottie files are awesome. I consider them a big leap forward in the right direction for animations. If you weren't already aware of it…]]></description><link>https://dilshankelsen.com/how-to-add-lottie-animation-files-in-react/</link><guid isPermaLink="false">https://dilshankelsen.com/how-to-add-lottie-animation-files-in-react/</guid><pubDate>Sat, 23 Jan 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Lottie files are awesome. I consider them a big leap forward in the right direction for animations. If you weren&amp;#x27;t already aware of it, Lottie files are platform agnostic. This means they cannot only be run on the web, but on smartphone, eBooks, operating systems and more! But in this blog post, I&amp;#x27;ll focus on how to add Lottie animation files in React. It&amp;#x27;s not as difficult as it may seem. 😃&lt;/p&gt;&lt;p&gt;You can already have access and play around with the final code in &lt;a href=&quot;https://codesandbox.io/s/lottie-animations-q42xt&quot; target=&quot;_blank&quot;&gt;my CodeSandbox example&lt;/a&gt;.&lt;/p&gt;&lt;h2 id=&quot;step-1-install-lottie&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-1-install-lottie&quot; aria-label=&quot;step 1 install lottie permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 1: Install Lottie&lt;/h2&gt;&lt;p&gt;You know the drill, buddy! You can install the module using Yarn or npm. Choose your poison. 💉&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; i lottie-react &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;#OR&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;yarn&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; react-lottie&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Next, you simply import the library at the top of a React file of your choosing.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;Lottie&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;react-lottie&amp;#x27;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;step-2-choose-a-lottie-file&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-2-choose-a-lottie-file&quot; aria-label=&quot;step 2 choose a lottie file permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 2: Choose a Lottie file&lt;/h2&gt;&lt;p&gt;Don&amp;#x27;t have a Lottie file of your own you can try this out on? No problem! There&amp;#x27;s a vibrant 🍹 community of designers offering plenty of free and beautiful animations. Check them out &lt;a href=&quot;https://lottiefiles.com/popular&quot; target=&quot;_blank&quot;&gt;at LottieFiles&lt;/a&gt;. You can even edit the Lottie file before you download it. Ain&amp;#x27;t that cool?&lt;/p&gt;&lt;p&gt;Now, equipped with your Lottie file, you want to import it into the same React file as the Lottie library.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;DancingBurger&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;./dancing-burger.json&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;step-3-add-the-lottie-animation&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-3-add-the-lottie-animation&quot; aria-label=&quot;step 3 add the lottie animation permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 3: Add the Lottie animation&lt;/h2&gt;&lt;p&gt;Let&amp;#x27;s take a look at the bare minimum to run the Lottie animation.&lt;/p&gt;&lt;p&gt;To do so, ☝️ we need to use the &lt;code&gt;&amp;lt;Lottie /&amp;gt;&lt;/code&gt; component we imported earlier and pass in our configurations as a props.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Lottie&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;lottieOptions&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To make the whole thing run, we only need to specify the Lottie file under the property &lt;code&gt;animationData&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; lottieOptions &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;animationData&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;DancingBurger&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;That&amp;#x27;s all folks! 🤷‍♀️&lt;/p&gt;&lt;h2 id=&quot;step-4-configure-the-lottie-animation&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-4-configure-the-lottie-animation&quot; aria-label=&quot;step 4 configure the lottie animation permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 4: Configure the Lottie animation&lt;/h2&gt;&lt;p&gt;We could have left it at that, but we&amp;#x27;re ambitious developers, aren&amp;#x27;t we? 🤓 Let&amp;#x27;s go over them.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; lottieOptions &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;animationData&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;DancingBurger&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// default: true&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;autoplay&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// default: true&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;rendererSettings&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;preserveAspectRatio&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;xMidYMid meet&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Next to our initial &lt;code&gt;animationData&lt;/code&gt; setting, we have the &lt;code&gt;autoplay&lt;/code&gt; property. It contains a boolean that determines whether the animation runs as soon as it&amp;#x27;s ready.&lt;/p&gt;&lt;p&gt;&lt;code&gt;loop&lt;/code&gt; can either be a boolean or number. In the former case, the animation goes on forever. However, the latter determines how many times it should run.&lt;/p&gt;&lt;p&gt;Last but not least, &lt;code&gt;rendererSettings&lt;/code&gt; is a setting for the animation rendering... duh! 😂 There are quite a few options and you can check them out in the &lt;a href=&quot;https://github.com/airbnb/lottie-web/wiki/Renderer-Settings&quot; target=&quot;_blank&quot;&gt;documentation&lt;/a&gt;. However, most individuals can safely ignore this option if they&amp;#x27;re not interested.&lt;/p&gt;&lt;h2 id=&quot;step-5-control-the-lottie-animation&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-5-control-the-lottie-animation&quot; aria-label=&quot;step 5 control the lottie animation permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 5: Control the Lottie animation&lt;/h2&gt;&lt;p&gt;Many of you will want to control the animation through external triggers such as a button click. Me too! Here&amp;#x27;s how we do it.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Lottie&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;lottieOptions&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;isStopped&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;isPaused&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;There are two props with which we can control the flow 🌊 of the animation, &lt;code&gt;isStopped&lt;/code&gt; and &lt;code&gt;isPaused&lt;/code&gt;. Both are of type boolean. As their names imply, the former stops and resets the animation, while the latter merely pauses it.&lt;/p&gt;&lt;p&gt;We can try to control these two props by toggling their state with buttons. Let&amp;#x27;s take a look at the following code snippet.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;isStopped&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; setIsStopped&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;isPaused&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; setIsPaused&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;onClick&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript function&quot;&gt;setIsPaused&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript parameter&quot;&gt;prevState&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;prevState&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;isPaused &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Play&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Pause&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;onClick&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript function&quot;&gt;setIsStopped&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript parameter&quot;&gt;prevState&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;prevState&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;isStopped &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Start&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Stop&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Lottie&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;lottieOptions&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;isStopped&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;isStopped&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;isPaused&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;isPaused&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We keep track of each respective states using the &lt;code&gt;useState&lt;/code&gt; hook. Depending on the state, we also render different button texts. When clicking on a button, we simply toggle the state by inverting it.&lt;/p&gt;&lt;p&gt;In case you weren&amp;#x27;t aware of it, in a &lt;code&gt;useState&lt;/code&gt; updater function, we can easily retrieve the previous state by passing in a callback function.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Create A Tiny Slider Carousel]]></title><description><![CDATA[Carousels are an amazing tool you can use to pep up your site and there's no shortage of JavaScript libraries that enable you to do so. Tiny…]]></description><link>https://dilshankelsen.com/how-to-create-a-tiny-slider-carousel/</link><guid isPermaLink="false">https://dilshankelsen.com/how-to-create-a-tiny-slider-carousel/</guid><pubDate>Sat, 16 Jan 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Carousels are an amazing tool you can use to pep up your site and there&amp;#x27;s no shortage of JavaScript libraries that enable you to do so. Tiny Slider is one of them. It&amp;#x27;s a solid 💪 choice for most people since it is built on Vanilla JavaScript. This means a smaller file size as well as fewer risks of becoming deprecated due to its dependencies. And in this tutorial, I will show you how to create a Tiny Slider 2 carousel on your website.&lt;/p&gt;&lt;p&gt;Strap your seat belts boys and girls, cause you&amp;#x27;re in for a wild ride! (Get it? Cause it&amp;#x27;s about a carousel?... Okay, I&amp;#x27;ll stop... 😅)&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:458px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:67%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAMEAv/EABUBAQEAAAAAAAAAAAAAAAAAAAEC/9oADAMBAAIQAxAAAAGY3RQkaJ//xAAZEAEBAQEBAQAAAAAAAAAAAAABAgADEyH/2gAIAQEAAQUCaOZzu3O8IMRI1P3/xAAVEQEBAAAAAAAAAAAAAAAAAAAAEf/aAAgBAwEBPwFH/8QAFhEBAQEAAAAAAAAAAAAAAAAAABEB/9oACAECAQE/AauP/8QAGxAAAgIDAQAAAAAAAAAAAAAAAAERMRASQWH/2gAIAQEABj8C9HtiXLZXSz//xAAaEAADAAMBAAAAAAAAAAAAAAAAAREhMUHB/9oACAEBAAE/IdoVLCLJKnJlCZLF0mxymnoY66P/2gAMAwEAAgADAAAAEFAv/8QAFhEBAQEAAAAAAAAAAAAAAAAAABEh/9oACAEDAQE/EJin/8QAFREBAQAAAAAAAAAAAAAAAAAAEDH/2gAIAQIBAT8Qov/EABsQAQEAAwADAAAAAAAAAAAAAAERACExQVFx/9oACAEBAAE/EGaFqPXF0pUFLwjhO/fuDDU0s3zyZEDoXgQw9FyAQbScz//Z&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;A meme with Dart Vader about creating the ultimate Tiny Slider carousel.&quot; title=&quot;A meme with Dart Vader about creating the ultimate Tiny Slider carousel.&quot; src=&quot;/static/7850d823e7a0c35668c8d49ebde0602d/f7c3c/meme-darth-vader.jpg&quot; srcSet=&quot;/static/7850d823e7a0c35668c8d49ebde0602d/e07e9/meme-darth-vader.jpg 200w,/static/7850d823e7a0c35668c8d49ebde0602d/066f9/meme-darth-vader.jpg 400w,/static/7850d823e7a0c35668c8d49ebde0602d/f7c3c/meme-darth-vader.jpg 458w&quot; sizes=&quot;(max-width: 458px) 100vw, 458px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;I am not your father!&lt;/figcaption&gt;&lt;h2 id=&quot;installation&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#installation&quot; aria-label=&quot;installation permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Installation&lt;/h2&gt;&lt;p&gt;There are a few ways you can install this library on your site. However, for the purpose of this tutorial, &lt;strong&gt;the easiest and most straightforward way is to include it through CDN&lt;/strong&gt; links in your document&amp;#x27;s &lt;code&gt;head&lt;/code&gt; (Not your actual head, silly 😜).&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;// For CSS&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;link&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;rel&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;stylesheet&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;href&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;https://cdnjs.cloudflare.com/ajax/libs/tiny-slider/2.9.3/tiny-slider.css&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;// For JavaScript&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;https://cdnjs.cloudflare.com/ajax/libs/tiny-slider/2.9.2/min/tiny-slider.js&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token script&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;understanding-the-html&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#understanding-the-html&quot; aria-label=&quot;understanding the html permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Understanding the HTML&lt;/h2&gt;&lt;p&gt;So, where shall we start, mate? How about the HTML, alright? 😋&lt;/p&gt;&lt;p&gt;Virtually all JavaScript carousels will require the same structure you see below.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;my-slider&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Slide 1&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Slide 2&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Slide 3&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;First, &lt;strong&gt;you need a wrapper&lt;/strong&gt; which contains a &lt;code&gt;class&lt;/code&gt; or an &lt;code&gt;id&lt;/code&gt; with which you can identify your carousel to begin with. Then, inside the wrapper, &lt;strong&gt;each child 🧒 element is basically a slide of your carousel&lt;/strong&gt;. However, for your slide content, I recommend you add another &lt;code&gt;div&lt;/code&gt; (or any other tag) layer as such.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;my-slider&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Slide 1&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Slide 2&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Slide 3&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This will allow you to style the slides to your heart&amp;#x27;s content without being interfered by the styling of the library.&lt;/p&gt;&lt;h3 id=&quot;understanding-the-javascript&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#understanding-the-javascript&quot; aria-label=&quot;understanding the javascript permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Understanding the JavaScript&lt;/h3&gt;&lt;p&gt;Most of the magic 🧙‍♀️ takes places in JavaScript. Quite obvious, right? Let&amp;#x27;s see what happens in the code snippet below.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; slider &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;tns&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;.my-slider&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;slideBy&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;page&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;autoplay&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Above, we have a &lt;code&gt;slider&lt;/code&gt; variable that receives the output from a function called &lt;code&gt;tns&lt;/code&gt;. This function accepts an object with many possible key-value pairs. What happens with the variable isn&amp;#x27;t too important. Let Tiny Slider take care of that. 😆 What&amp;#x27;s more &lt;strong&gt;interesting to us is the object passed into the &lt;code&gt;tns&lt;/code&gt; function&lt;/strong&gt;. It&amp;#x27;s basically like the settings of our carousel.&lt;/p&gt;&lt;h2 id=&quot;how-to-configure-tiny-slider&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#how-to-configure-tiny-slider&quot; aria-label=&quot;how to configure tiny slider permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;How to configure Tiny Slider&lt;/h2&gt;&lt;p&gt;Let&amp;#x27;s talk about how exactly these carousel &amp;quot;&lt;em&gt;settings&lt;/em&gt;&amp;quot; work. For that we use the example from before. Probably &lt;strong&gt;the most important 🧐 setting is &lt;code&gt;container&lt;/code&gt;&lt;/strong&gt;. Here you need to give the &lt;code&gt;id&lt;/code&gt; or &lt;code&gt;class&lt;/code&gt; of your carousel wrapper. By default, Tiny Slider will look for the class &lt;code&gt;slider&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:375px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:125.50000000000001%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAZABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAwAEAQX/xAAVAQEBAAAAAAAAAAAAAAAAAAAAAf/aAAwDAQACEAMQAAAB9bOgS6I6yzMZ2WP/xAAbEAADAQADAQAAAAAAAAAAAAAAAQIDERITMf/aAAgBAQABBQJ3w9NGevJWbdVl1PBl2puq7Co0+iP/xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAEDAQE/AR//xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAECAQE/AR//xAAdEAABAwUBAAAAAAAAAAAAAAAAAREhAhASIDFB/9oACAEBAAY/AiLPkTV06Mqnuv8A/8QAGxAAAgMBAQEAAAAAAAAAAAAAAAERIUExcVH/2gAIAQEAAT8hod1FHScsUUpIo4n8Esn0RpBhkQtKpOLhDYuX4dB4cv0//9oADAMBAAIAAwAAABB4JX7/xAAYEQACAwAAAAAAAAAAAAAAAAAAEQEQMf/aAAgBAwEBPxCNpjP/xAAVEQEBAAAAAAAAAAAAAAAAAAABIP/aAAgBAgEBPxBj/8QAHxABAAMAAQQDAAAAAAAAAAAAAQARITFBUWGhcYGx/9oACAEBAAE/EHoFE06LEJdYpzY1xH3E1wKaNZNJGxdM4D5Sscn0NlNMNILRSFWxeTd49U/Jw+DPcT//2Q==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;A meme about debugging days on end an obvious bug while creating a Tiny Slider carousel.&quot; title=&quot;A meme about debugging days on end an obvious bug while creating a Tiny Slider carousel.&quot; src=&quot;/static/ce225f2207214b4c6dcccb4e47950127/bf173/meme-harold.jpg&quot; srcSet=&quot;/static/ce225f2207214b4c6dcccb4e47950127/e07e9/meme-harold.jpg 200w,/static/ce225f2207214b4c6dcccb4e47950127/bf173/meme-harold.jpg 375w&quot; sizes=&quot;(max-width: 375px) 100vw, 375px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Hasn&amp;#x27;t it happened to us all? 😭&lt;/figcaption&gt;&lt;p&gt;Next up is the key &lt;code&gt;items&lt;/code&gt;. This dictates &lt;strong&gt;how many slides are visible at once&lt;/strong&gt;. Naturally, you should have at least as many slides in your HTML as you indicate in this option.&lt;/p&gt;&lt;p&gt;Closely related is &lt;code&gt;slideBy&lt;/code&gt;, which refers to &lt;strong&gt;how many slides the carousel should 🏃‍♀️ move by.&lt;/strong&gt; You can also pass in the value &lt;code&gt;page&lt;/code&gt;, which simply refers to how many slides are visible on each &amp;quot;page&amp;quot; of the carousel. This value is identical to &lt;code&gt;items&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Lastly, &lt;code&gt;autoplay&lt;/code&gt; simply indicates whether the slides should &lt;strong&gt;move automatically&lt;/strong&gt; without interaction by the user.&lt;/p&gt;&lt;p&gt;However, it would be pointless to go through all possible settings and values in this article since you can already read up on them in &lt;a href=&quot;https://github.com/ganlanyuan/tiny-slider#options&quot; target=&quot;_blank&quot;&gt;the documentation&lt;/a&gt;. Yet again, it won&amp;#x27;t hurt for me to go over some of the more important ones. 😉&lt;/p&gt;&lt;h2 id=&quot;what-does-node-mean&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#what-does-node-mean&quot; aria-label=&quot;what does node mean permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;What does node mean?&lt;/h2&gt;&lt;p&gt;If you&amp;#x27;ve already skimmed the documentation, you may have encountered the word &lt;code&gt;Node&lt;/code&gt; under the &lt;em&gt;Type&lt;/em&gt; column.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:734px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:20%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAhUlEQVQI102Oyw6FIAxE/f+/hAUPgzEgGECBe26IxlmQ9nSmZbHWbtvWe2+t9UdjjPM84RSlFOecUgpC+zp5F2bXdU36DcNTShRMvffHcRCOMU4z+odxcGHf9/u+x0dwrfUMSympjTFCCH7BrrliqbXmnLlDuD3iOCSEAMS3rusMIPyv+QfuE+hyWVnZ/AAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Screenshot of part of the documentation&quot; title=&quot;Screenshot of part of the documentation&quot; src=&quot;/static/aed20fc75d8e582822259512d5f7bd1d/c6d67/image-1.png&quot; srcSet=&quot;/static/aed20fc75d8e582822259512d5f7bd1d/772e8/image-1.png 200w,/static/aed20fc75d8e582822259512d5f7bd1d/e17e5/image-1.png 400w,/static/aed20fc75d8e582822259512d5f7bd1d/c6d67/image-1.png 734w&quot; sizes=&quot;(max-width: 734px) 100vw, 734px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Node, shnode... 👀&lt;/figcaption&gt;&lt;p&gt;&lt;strong&gt;Instead of typing the CSS selector,&lt;/strong&gt; i.e. &lt;code&gt;id&lt;/code&gt; or &lt;code&gt;class&lt;/code&gt;, &lt;strong&gt;you can instead give the actual DOM Node itself&lt;/strong&gt; to the &lt;code&gt;container&lt;/code&gt; option. I hope you paid attention in JavaScript school 😝 for how this works. For example, we can get the node of our carousel wrapper with &lt;code&gt;document.getElementsByClassName(&amp;#x27;.my-slider&amp;#x27;)&lt;/code&gt;. While I find the selector method way simpler, I haven&amp;#x27;t found a use case for the former yet.&lt;/p&gt;&lt;h3 id=&quot;playing-around&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#playing-around&quot; aria-label=&quot;playing around permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Playing around&lt;/h3&gt;&lt;p&gt;Before we move on, I&amp;#x27;d like to congratulate you for making it this far. This would be a good time to start playing around with the carousel, trying out different settings. As a bonus, 🥳 you can check out and &lt;strong&gt;mess around with a live demo of Tiny Slider&lt;/strong&gt; in my &lt;a href=&quot;https://codepen.io/dkelsen/pen/zYBVbLO&quot; target=&quot;_blank&quot;&gt;CodePen example&lt;/a&gt;.&lt;/p&gt;&lt;h2 id=&quot;adding-responsiveness&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#adding-responsiveness&quot; aria-label=&quot;adding responsiveness permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Adding responsiveness&lt;/h2&gt;&lt;p&gt;Ah yes, responsiveness. The bane of every front-end developer. Fortunately, Tiny Slider makes this a breeze to implement. Let&amp;#x27;s add to our initial carousel code some responsiveness.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; slider &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;tns&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;.my-slider&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;slideBy&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;page&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;autoplay&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;responsive&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;640&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1400&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To add responsiveness to your carousel, &lt;strong&gt;you simply add the option &lt;code&gt;responsive&lt;/code&gt; with an object as its value.&lt;/strong&gt; In this object literal, you specify the breakpoints ✂️ in pixels, which in turn accept an object with settings that we saw before.&lt;/p&gt;&lt;p&gt;However, &lt;strong&gt;not all options will work inside the responsive setting.&lt;/strong&gt; To see what is possible, check out the &lt;a href=&quot;https://github.com/ganlanyuan/tiny-slider#responsive-options&quot; target=&quot;_blank&quot;&gt;list of responsive options&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Keep in mind the responsiveness in Tiny Slider works from a mobile first perspective (As it should 😏). For example, this means a breakpoint of &lt;code&gt;640&lt;/code&gt; should be translated as 640px and above.&lt;/p&gt;&lt;h2 id=&quot;adding-custom-buttons&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#adding-custom-buttons&quot; aria-label=&quot;adding custom buttons permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Adding custom buttons&lt;/h2&gt;&lt;p&gt;Another important feature for many developers are buttons. &lt;strong&gt;Custom buttons&lt;/strong&gt; are the way to go, you know what I mean? 😎 So how do we go about this?&lt;/p&gt;&lt;p&gt;In this article, I&amp;#x27;ll go over the &lt;code&gt;next&lt;/code&gt;, &lt;code&gt;previous&lt;/code&gt; and &lt;code&gt;autoplay&lt;/code&gt; buttons.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;controls&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;previous&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Prev&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Next&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As you can see above, we first need to create a container that holds all our buttons. You should know that the &lt;code&gt;Prev&lt;/code&gt; and &lt;code&gt;Next&lt;/code&gt; buttons &lt;em&gt;need&lt;/em&gt; to be wrapped by a container. However, the autoplay button can be placed wherever you like. In addition to that, each element must have its own CSS selector.&lt;/p&gt;&lt;p&gt;Next, we &lt;strong&gt;pass the selectors to each respective property&lt;/strong&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; slider &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;tns&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token spread operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;controlsContainer&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;#controls&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;prevButton&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;.previous&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;nextButton&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;.next&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;autoplayButton&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;.auto&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now, the &lt;strong&gt;autoplay button functions in a sightly different manner.&lt;/strong&gt; And you may already have noticed 👀 earlier that I didn&amp;#x27;t add any text for this button. This is because Tiny Slider dynamically adds it based on the state of the autoplay, i.e. stop 🛑 or start.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; slider &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;tns&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token spread operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;autoplay&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;autoplayText&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Start&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Stop&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can of course change this text with the property &lt;code&gt;autoplayText&lt;/code&gt; by passing an array with two strings. Be aware that the order matters 🚨. Last but not least, make sure you actually activate &lt;code&gt;autoplay&lt;/code&gt; with &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Don&amp;#x27;t forget, you can check out and &lt;strong&gt;mess around with a live demo of Tiny&lt;/strong&gt; &lt;strong&gt;Slider&lt;/strong&gt; in my &lt;a href=&quot;https://codepen.io/dkelsen/pen/zYBVbLO&quot; target=&quot;_blank&quot;&gt;CodePen example&lt;/a&gt;! 🤩&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Use V-Model With Vuex]]></title><description><![CDATA[In my previous blog post " Learn Vuex Through A Tutorial ", I demonstrated how to implement state management using Vuex. As you surely know…]]></description><link>https://dilshankelsen.com/v-model-with-vuex/</link><guid isPermaLink="false">https://dilshankelsen.com/v-model-with-vuex/</guid><pubDate>Sat, 09 Jan 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In my previous blog post &amp;quot;&lt;a href=&quot;/learn-vuex-through-tutorial/&quot;&gt;Learn Vuex Through A Tutorial&lt;/a&gt;&amp;quot;, I demonstrated how to implement state management using Vuex. As you surely know, a central theme of Vuex is its unidirectional data flow. This means &lt;strong&gt;two-way binding is a no-go&lt;/strong&gt;! Unfortunately, this means the Vue directive &lt;code&gt;v-model&lt;/code&gt; will not work with it out of the box. 😣 &lt;em&gt;&amp;quot;Oh no! &lt;a href=&quot;https://www.youtube.com/watch?v=tpD00Q4N6Jk&quot; target=&quot;_blank&quot;&gt;What am I gonna do!?&lt;/a&gt;&amp;quot;&lt;/em&gt;. Don&amp;#x27;t worry, I&amp;#x27;ve got you covered. In this blog post, I&amp;#x27;ll show you how to use v-model with Vuex.&lt;/p&gt;&lt;p&gt;In case you were wondering, no matter if you&amp;#x27;re using &lt;strong&gt;Vue 3 or Vue 2&lt;/strong&gt;, you&amp;#x27;ll encounter the &lt;strong&gt;same issue&lt;/strong&gt;.&lt;/p&gt;&lt;h3 id=&quot;make-your-choice&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#make-your-choice&quot; aria-label=&quot;make your choice permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Make your choice&lt;/h3&gt;&lt;p&gt;There are two ways of tackling this problem: creating a two-way computed property or &amp;quot;modifying&amp;quot;, for lack of a better term, the &lt;code&gt;v-model&lt;/code&gt; logic to accommodate Vuex. Both of these solutions work in strict 👮‍♂️ mode.&lt;/p&gt;&lt;p&gt;Let me first explain the latter in more detail.&lt;/p&gt;&lt;h2 id=&quot;solution-1-modifying-v-model-logic&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#solution-1-modifying-v-model-logic&quot; aria-label=&quot;solution 1 modifying v model logic permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Solution 1: Modifying v-model logic&lt;/h2&gt;&lt;p&gt;As you may or may not know, the &lt;code&gt;v-model&lt;/code&gt; directive is essentially syntactic sugar. 🍭 It can be &lt;strong&gt;rewritten in a more verbose manner using two other directives&lt;/strong&gt;, as seen below. In this example, the two-way binding happens by calling &lt;code&gt;updateMessage&lt;/code&gt; on any input change and updating &lt;code&gt;message&lt;/code&gt; with the input value.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;v-model&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;&amp;lt;!-- Is the equivalent of --&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;:value&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;@input&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;updateMessage&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript function&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript string&quot;&gt;&amp;#x27;Chunk Bytes is awesome&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;methods&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript comment&quot;&gt;/* Behind the scenes */&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript function&quot;&gt;updateMessage&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token script language-javascript parameter&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; event&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now that we know how v-model actually works under the hood, 🚗 we can make some simple changes to the code in order to make it work with Vuex. Let me show you!&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;:value&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;@input&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;updateMessage&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;computed&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript comment&quot;&gt;/* Use a computed property instead */&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript function&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;$store&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;methods&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript comment&quot;&gt;/* Call a mutation instead */&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript function&quot;&gt;updateMessage&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token script language-javascript parameter&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;$store&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript method function property-access&quot;&gt;commit&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token script language-javascript string&quot;&gt;&amp;#x27;updateMessage&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; event&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The first change we need to make is &lt;strong&gt;using a computed property&lt;/strong&gt; (or &lt;em&gt;getter&lt;/em&gt;) to retrieve the state data. Alright, step one done. ✅ Next, we need to alter this state by &lt;strong&gt;calling a mutation through a method function&lt;/strong&gt;. You may of course dispatch an action and then commit the mutation, if you prefer to do so.&lt;/p&gt;&lt;p&gt;In case you were wondering, here&amp;#x27;s what the mutation handler would look like in our store.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; mutations &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;updateMessage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; payload&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    state&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; payload&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And really, that&amp;#x27;s all there is to it! 😊&lt;/p&gt;&lt;h2 id=&quot;solution-2-two-way-computed-property&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#solution-2-two-way-computed-property&quot; aria-label=&quot;solution 2 two way computed property permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Solution 2: Two-way computed property&lt;/h2&gt;&lt;p&gt;&lt;em&gt;&amp;quot;What&amp;#x27;s the other solution you talked about?&amp;quot;&lt;/em&gt;. Glad you didn&amp;#x27;t forget dear reader! As I previously mentioned, we can tackle this issue by constructing a two-way computed property using a getter and a setter (⚠️ getter in this context is not referring to the actual getter function in Vuex).&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;v-model&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;computed&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token script language-javascript comment&quot;&gt;/* By default get() is used */&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token script language-javascript function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;$store&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token script language-javascript comment&quot;&gt;/* We add a setter */&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token script language-javascript function&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;$store&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript method function property-access&quot;&gt;commit&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token script language-javascript string&quot;&gt;&amp;#x27;updateMessage&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In this solution, we don’t actually mess around with the &lt;code&gt;v-model&lt;/code&gt; directive, but with the property it’s bound to. &lt;strong&gt;By default, a computed property is a getter only.&lt;/strong&gt; 😲 However, we have the option to also &lt;a href=&quot;https://vuejs.org/v2/guide/computed.html#Computed-Setter&quot; target=&quot;_blank&quot;&gt;add a setter&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Inside of the computed property, we have a custom getter function called &lt;code&gt;get()&lt;/code&gt; directly retrieving the state from our store. On the other hand, in order to alter the state, we use a setter called &lt;code&gt;set()&lt;/code&gt;. This calls a mutation handler and passes in the input value that we automatically receive.&lt;/p&gt;&lt;h2 id=&quot;review&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#review&quot; aria-label=&quot;review permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Review&lt;/h2&gt;&lt;p&gt;&lt;strong&gt;Pay attention to the structure of both solutions.&lt;/strong&gt; You&amp;#x27;ll see that they are both very similar. In the first one, you essentially have two functions, one as a computed property, the other as a method. In the second case, you have the &lt;em&gt;same&lt;/em&gt; functions, just named differently and part of a computed property. Hmmm... 🧐&lt;/p&gt;&lt;h2 id=&quot;using-a-library&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#using-a-library&quot; aria-label=&quot;using a library permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Using a library&lt;/h2&gt;&lt;p&gt;It might not come as a surprise to you that many &lt;strong&gt;others&lt;/strong&gt; have come across this problem and evidently &lt;strong&gt;tried to come up with a solution of their own.&lt;/strong&gt; The most popular library at the moment is &lt;a href=&quot;https://github.com/maoberlehner/vuex-map-fields&quot; target=&quot;_blank&quot;&gt;vuex-map-fields&lt;/a&gt;, with over a thousand stars at the time of this writing.&lt;/p&gt;&lt;p&gt;Its documentation is quite well written in my opinion, so I don&amp;#x27;t think a tutorial will be necessary (unless you ask me of course... 😋). Nonetheless, knowing someone&amp;#x27;s already made an easy to use solution &lt;strong&gt;should not discourage you from knowing the other alternative solutions&lt;/strong&gt; I presented to you above.&lt;/p&gt;&lt;p&gt;In fact, you&amp;#x27;ll sometimes run into a problem with the library and might have to resort to the old-fashioned solution, as I had to do once.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Vuetify Tutorial: How To Create Cards]]></title><description><![CDATA[Cards are a wonderful way to group together related content, no matter if on the web, your mobile phone or computer. Actually, once you…]]></description><link>https://dilshankelsen.com/vuetify-tutorial-how-to-create-cards/</link><guid isPermaLink="false">https://dilshankelsen.com/vuetify-tutorial-how-to-create-cards/</guid><pubDate>Sat, 02 Jan 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Cards are a wonderful way to group together related content, no matter if on the web, your mobile phone or computer. Actually, once you start watching out for them, you will realize that they&amp;#x27;re everywhere! 🤯 As such, knowing how to create them will serve as an indispensable tool in your arsenal. That is why in this Vuetify tutorial, I&amp;#x27;ll show you how to create cards.&lt;/p&gt;&lt;h2 id=&quot;the-layout&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-layout&quot; aria-label=&quot;the layout permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The layout&lt;/h2&gt;&lt;p&gt;A card in Vuetify is made up of &lt;strong&gt;five different components:&lt;/strong&gt; &lt;code&gt;v-card&lt;/code&gt;, &lt;code&gt;v-card-actions&lt;/code&gt;, &lt;code&gt;v-card-subtitle&lt;/code&gt;, &lt;code&gt;v-card-text&lt;/code&gt; and &lt;code&gt;v-card-title&lt;/code&gt;. Let&amp;#x27;s take a look at each one on its own.&lt;/p&gt;&lt;h2 id=&quot;the-container&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-container&quot; aria-label=&quot;the container permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The container&lt;/h2&gt;&lt;p&gt;As the name implies, &lt;code&gt;v-card&lt;/code&gt; serves as the body of your card, which gives it that distinctive card look. Technically, you could do without the other components and call it a day. 😂&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:464px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:83.50000000000001%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAARABQDASIAAhEBAxEB/8QAGAABAAMBAAAAAAAAAAAAAAAAAAIDBAH/xAAWAQEBAQAAAAAAAAAAAAAAAAABAAL/2gAMAwEAAhADEAAAAcs49cyUo1Xk4RH/xAAaEAACAwEBAAAAAAAAAAAAAAAAAQIREhAh/9oACAEBAAEFAtUabWEJUe1RAlz/xAAWEQEBAQAAAAAAAAAAAAAAAAAQARH/2gAIAQMBAT8B0h//xAAWEQEBAQAAAAAAAAAAAAAAAAAQARH/2gAIAQIBAT8Bwp//xAAdEAACAgEFAAAAAAAAAAAAAAAAAREhAhAxcYGh/9oACAEBAAY/AlCRcRwX4Sdm4zLT/8QAGxABAAMAAwEAAAAAAAAAAAAAAQAhQRExcbH/2gAIAQEAAT8h5XSu0hWSo0YLnSGdOSiLgWqfKdPE19n/2gAMAwEAAgADAAAAEEDQ/v/EABkRAAIDAQAAAAAAAAAAAAAAAAABEBEhMf/aAAgBAwEBPxBapRyM/8QAGBEAAgMAAAAAAAAAAAAAAAAAAREAECH/2gAIAQIBAT8QOGYrf//EAB4QAQADAAICAwAAAAAAAAAAAAEAETEhQWGhsfDx/9oACAEBAAE/EOYc0pbx7g1FeKVUGQS6eIKgScxL42vMWl1E0D3+zkNlbbMmPrpPc/BH3p//2Q==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Meme about continuing to read the article Vuetify tutorial how to create cards&quot; title=&quot;Meme about continuing to read the article Vuetify tutorial how to create cards&quot; src=&quot;/static/4bc33260b0bca5ecda6f139b6ed1b6f5/c9a08/meme-reading.jpg&quot; srcSet=&quot;/static/4bc33260b0bca5ecda6f139b6ed1b6f5/e07e9/meme-reading.jpg 200w,/static/4bc33260b0bca5ecda6f139b6ed1b6f5/066f9/meme-reading.jpg 400w,/static/4bc33260b0bca5ecda6f139b6ed1b6f5/c9a08/meme-reading.jpg 464w&quot; sizes=&quot;(max-width: 464px) 100vw, 464px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Not so fast... buddy!&lt;/figcaption&gt;&lt;p&gt;As with all Vuetify components, it&amp;#x27;s &lt;a href=&quot;https://vuetifyjs.com/en/api/v-card/&quot; target=&quot;_blank&quot;&gt;very customizable&lt;/a&gt;. The main way to customize components is through props. For the newbies among you, it&amp;#x27;s basically a custom CSS attribute with it&amp;#x27;s own value. One example can be seen below:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;card-1&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;elevation&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;outlined&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;shaped&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;This is an empty &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;v-card&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    with some props styling&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:441px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:31.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAAAsTAAALEwEAmpwYAAAApUlEQVQY042Q2wrDIBBE8/9fGPpQoReNkIiuEoNrtBObQkL70IPCIjuzs3alFGZeG6VRa0WVa91O+UHOmRsdLhFJKY0xwzAopa5C+Glc5T2JS9Lq8TyBBrjDYlmWLqXknOv7XghhrYXRZEwMfh01yxtb44i89+ixDRQncQgBM7XWyFP/YxfHGJH8/cQHsFZO6bPdCcyAcp7nfWfXoAP+AH2BsPjgFxRbWfcZWlG3AAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Vuetify card visual example 1&quot; title=&quot;Vuetify card visual example 1&quot; src=&quot;/static/82882285fd36c57a598fa15fc615f68d/efc6e/image-1.png&quot; srcSet=&quot;/static/82882285fd36c57a598fa15fc615f68d/772e8/image-1.png 200w,/static/82882285fd36c57a598fa15fc615f68d/e17e5/image-1.png 400w,/static/82882285fd36c57a598fa15fc615f68d/efc6e/image-1.png 441w&quot; sizes=&quot;(max-width: 441px) 100vw, 441px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;Here, we are passing 3 different props: &lt;code&gt;elevation&lt;/code&gt;, &lt;code&gt;outlined&lt;/code&gt; and &lt;code&gt;shaped&lt;/code&gt;. The first receives a number value of &lt;code&gt;4&lt;/code&gt;. The latter two, while we don&amp;#x27;t explicitly pass values to them, are the equivalent of &lt;code&gt;outlined=true&lt;/code&gt; and &lt;code&gt;shaped=true&lt;/code&gt;. Be sure to keep that in mind whenever you see that. 🤓&lt;/p&gt;&lt;p&gt;By the way, any &lt;strong&gt;examples I show you&lt;/strong&gt; throughout the tutorial &lt;strong&gt;can be played around with &lt;a href=&quot;https://codepen.io/dkelsen/pen/LYZvoOP&quot; target=&quot;_blank&quot;&gt;in CodePen&lt;/a&gt;&lt;/strong&gt;. Have fun!&lt;/p&gt;&lt;h3 id=&quot;how-to-read-vuetify-documentation&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#how-to-read-vuetify-documentation&quot; aria-label=&quot;how to read vuetify documentation permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;How to read Vuetify documentation&lt;/h3&gt;&lt;p&gt;Now that you know what props are and how they&amp;#x27;re inserted into the code, reading the documentation will not be such a challenge. Let&amp;#x27;s take a look at the &lt;strong&gt;&lt;a href=&quot;https://vuetifyjs.com/en/api/v-card/#props&quot; target=&quot;_blank&quot;&gt;documentation&lt;/a&gt;&lt;a href=&quot;&quot;&gt;&lt;/a&gt; in the context of our previous examples.&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:16%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAADCAIAAAAcOLh5AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAZUlEQVQI11WOyxLCMAwD8//fCqVpiyM/lGBS2hl2fJAO9ro09WAfEw9lj29QJwk0AGamkwzjn1IlYDyLueT0QbWAx7Juj+drvai1tgsRyXPlQIpvcwvazxzcD9n2NybnQpIf3eYP6bmvph5vJ9EAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Vuetify cards documentation of three props&quot; title=&quot;Vuetify cards documentation of three props&quot; src=&quot;/static/31d4ae7c0c2b089efcc2fc8d7bbbe17e/5a190/image-2.png&quot; srcSet=&quot;/static/31d4ae7c0c2b089efcc2fc8d7bbbe17e/772e8/image-2.png 200w,/static/31d4ae7c0c2b089efcc2fc8d7bbbe17e/e17e5/image-2.png 400w,/static/31d4ae7c0c2b089efcc2fc8d7bbbe17e/5a190/image-2.png 800w,/static/31d4ae7c0c2b089efcc2fc8d7bbbe17e/d0143/image-2.png 1025w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;Squinting your eyes won&amp;#x27;t make this bigger 😅&lt;/p&gt;&lt;p&gt;The first column is the name of the props, nothing difficult here. The second column defines the value type that is to be passed into the props. &lt;code&gt;outlined&lt;/code&gt; and &lt;code&gt;shaped&lt;/code&gt; both receive boolean values, so either &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;. On the other hand, &lt;code&gt;elevation&lt;/code&gt; can receive either a &lt;code&gt;number&lt;/code&gt; or a &lt;code&gt;string&lt;/code&gt;, i.e. a &lt;code&gt;string&lt;/code&gt; that contains a number. So doing &lt;code&gt;elevation=4&lt;/code&gt; or &lt;code&gt;elevation=&amp;quot;4&amp;quot;&lt;/code&gt; has the same effect.&lt;/p&gt;&lt;p&gt;The third column shows the &lt;strong&gt;default value&lt;/strong&gt; of each props. This simply means, &lt;strong&gt;if you haven&amp;#x27;t inserted that specific props&lt;/strong&gt; yourself onto the component, it will use the default value. As an example, &lt;code&gt;&amp;lt;v-card&amp;gt;&lt;/code&gt; without any props would use the default values of all its possible props. Basically the equivalent of the code below:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;active-class&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;// ... etc&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Last but not least, there&amp;#x27;s the last column. This one has a... description of the props. 😆 Not much else to say here, am I right?&lt;/p&gt;&lt;h2 id=&quot;the-text&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-text&quot; aria-label=&quot;the text permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The text&lt;/h2&gt;&lt;p&gt;When it comes to writing text in your cards, &lt;code&gt;v-card-subtitle&lt;/code&gt;, &lt;code&gt;v-card-text&lt;/code&gt; and &lt;code&gt;v-card-title&lt;/code&gt; are your go-tos. However, there&amp;#x27;s not too much to tell y&amp;#x27;all here besides the basic theme styling 🎨 being applied to each component. There aren&amp;#x27;t even any props to use! It should go without saying that &lt;strong&gt;these components can only be placed in a card component.&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Here&amp;#x27;s an example of these components in a card. Don&amp;#x27;t forget, you can play around with the code in this &lt;a href=&quot;https://codepen.io/dkelsen/pen/LYZvoOP&quot; target=&quot;_blank&quot;&gt;CodePen example&lt;/a&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-title&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Chunk Bytes&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-title&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-subtitle&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Learn Web Development In Chunks&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-subtitle&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-text&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Joke of the Day: debugging is removing the needles from the haystack.&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-text&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:443px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:44.99999999999999%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAzklEQVQoz42R2Q6CQAxF+f8fg4SEB/7BkWHfN0E8tgRNDOpJaDqlt70Dzrquy7IcUSG//YI2ZxiGaZr6vh/Hseu6QUB8/wpKJA7KKIo8zwvDMAgC13V936/rets2mrYTELPJ6QVrbRzHVmAWMU3TPM+zLONIg85SXmIevcM8z7iYBZJF0Dqtp5u5JBuapimKoixLPLM2EqqqygRy9WKMocj+XYyMd0mS4PYqUNS1+uU1PxKKu7ht205oBQYRD//vTILmmKXt+avMB5c/wN0DGdUDlSgX7QYAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Vuetify card visual example 2&quot; title=&quot;Vuetify card visual example 2&quot; src=&quot;/static/efacefeb0ae75d520068dc439a0ab681/a120c/image-3.png&quot; srcSet=&quot;/static/efacefeb0ae75d520068dc439a0ab681/772e8/image-3.png 200w,/static/efacefeb0ae75d520068dc439a0ab681/e17e5/image-3.png 400w,/static/efacefeb0ae75d520068dc439a0ab681/a120c/image-3.png 443w&quot; sizes=&quot;(max-width: 443px) 100vw, 443px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;h2 id=&quot;the-action-section&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-action-section&quot; aria-label=&quot;the action section permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The action section&lt;/h2&gt;&lt;p&gt;The final component on our list is &lt;code&gt;v-card-action&lt;/code&gt;. This is where the action happens 💥 and &lt;strong&gt;users interact with your card through buttons&lt;/strong&gt;, such as &lt;code&gt;v-btn&lt;/code&gt; or &lt;code&gt;v-menu&lt;/code&gt;. However, you may of course place non-interactive components like &lt;code&gt;v-icon&lt;/code&gt; if you feel like a real rebel. 😎&lt;/p&gt;&lt;p&gt;As with the text components, this one can&amp;#x27;t be customized either. Instead, it serves to apply some basic styling, such as resizing and alignment.&lt;/p&gt;&lt;p&gt;Once again, let&amp;#x27;s take a look at an example!&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;outlined&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-title&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Chunk Bytes&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-title&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-subtitle&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Learn Web Development In Chunks&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-subtitle&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-text&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Joke of the Week: How many programmers does it take to change a light bulb? None, that&amp;#x27;s a hardware problem.&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-text&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-actions&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-btn&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;red lighten-2&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;ripple&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      Don&amp;#x27;t Click Me&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-btn&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-actions&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:420px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:61.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA+ElEQVQoz41Sa4+DIBDs//9/vv1iL/UBioKJifLwRhavTa+Xc4LrADvLBPZmrd1/wV0AhDdjTFmWSZJkWZbneRRFiOu6Uon9bwRx13VFUUBTVVUcx2maLsuCPa31tm2IyPksxqeUmucZUQiBOE0TyjHGhEfbtlLKz2J4Q2E6AYBhcHMCGUR+Emj3Ke77vmkaznnrQcd2J2oPrGORMpWUdGeHbXgbxxHOew9waIgPw4CiSACHeRDmAVUQY/J1v3PGDn9a2xfQk7zhafuY4GLPpesIYvx3rY8BfSD2mthY3Jh9PJxSII4zW9dOCGq0/09GShi+p9zl9vwGXJy5Xf75xhIAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Vuetify card visual example 3&quot; title=&quot;Vuetify card visual example 3&quot; src=&quot;/static/f2fe74e456540fc7103bb529f318cba6/d10fb/image-4.png&quot; srcSet=&quot;/static/f2fe74e456540fc7103bb529f318cba6/772e8/image-4.png 200w,/static/f2fe74e456540fc7103bb529f318cba6/e17e5/image-4.png 400w,/static/f2fe74e456540fc7103bb529f318cba6/d10fb/image-4.png 420w&quot; sizes=&quot;(max-width: 420px) 100vw, 420px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;h2 id=&quot;adding-some-basic-spacing&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#adding-some-basic-spacing&quot; aria-label=&quot;adding some basic spacing permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Adding some basic spacing&lt;/h2&gt;&lt;p&gt;While we&amp;#x27;re at the topic of &lt;code&gt;v-card-actions&lt;/code&gt;, let me quickly show you a simple way to space your components. Everybody... meet &lt;code&gt;v-spacer&lt;/code&gt;! 💃 It&amp;#x27;s a &lt;a href=&quot;/learn-vuetify-grid-system/&quot;&gt;Vuetify grid&lt;/a&gt; component that takes up as much space as possible in a row without pushing other components out of view or in a new line. In case you didn&amp;#x27;t know, &lt;strong&gt;&lt;code&gt;v-card-actions&lt;/code&gt; and &lt;code&gt;v-card-title&lt;/code&gt; are based on Flexbox,&lt;/strong&gt; which is why we can use it!&lt;/p&gt;&lt;p&gt;In this example, we place &lt;code&gt;v-spacer&lt;/code&gt; in the middle and push components on either side away.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;hover&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-title&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Chunk Bytes&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-title&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-subtitle&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Learn Web Development In Chunks&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-subtitle&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-text&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Joke of the Year: I never tell the same joke twice. I have a DRY sense of humor.&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-text&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-actions&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-icon&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;mdi-email&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-icon&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-spacer&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-btn&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;blue lighten-3&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;I&amp;#x27;m fake&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-btn&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card-actions&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;v-card&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:440px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:57.50000000000001%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAACXBIWXMAAAsTAAALEwEAmpwYAAABOUlEQVQoz41STU+EMBTk9+rdv6BH7/4FExMP3oxZD0BCSLx4wxAXhAKF8E3LR+vYko3ZDa5zaCZ9zLzpexhCCCmlUDiQf8KY53kcRzCQaZpAlmU5+kieQH9mRFFkmqZt267rWpa12+1839dluY1VzBijlGZZliQJiWNCSEZpWZZVVdV1XRQFOEIdBVnFyNy2LSxwDsOAs2maruv6vuecdwqnKVYx3omyFkMAPW40gQuao1orVAraaxVDE8dxnudBEKRpitiIikGA4LL+BXjhJYgkDp3RROeEK7huBUcQNN+elxKfneo6JCH6SVAui1Hmk6y5EjuOE4bh37tZVOX5k18+7q+e9hcPH3dvzc9P4nkeVoUa9rFsABWYW0F/80puTXL98nX/XqK1gRrGi+ex8xiwPM7UwQYIvwE/P7L95cwoAQAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Vuetify card visual example 4&quot; title=&quot;Vuetify card visual example 4&quot; src=&quot;/static/568ed040a8aa25ce2cb2bc331250d697/48c0e/image-5.png&quot; srcSet=&quot;/static/568ed040a8aa25ce2cb2bc331250d697/772e8/image-5.png 200w,/static/568ed040a8aa25ce2cb2bc331250d697/e17e5/image-5.png 400w,/static/568ed040a8aa25ce2cb2bc331250d697/48c0e/image-5.png 440w&quot; sizes=&quot;(max-width: 440px) 100vw, 440px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;h2 id=&quot;overriding-styles&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#overriding-styles&quot; aria-label=&quot;overriding styles permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Overriding styles&lt;/h2&gt;&lt;p&gt;On a final note, while you may not have any options to style the &lt;code&gt;v-card-...&lt;/code&gt; components through props, you may still &lt;strong&gt;override their default styles using &lt;a href=&quot;https://vuetifyjs.com/en/styles/text-and-typography/#typography&quot; target=&quot;_blank&quot;&gt;classes&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;For example, 💁 we could override the default styling of &lt;code&gt;v-card-title&lt;/code&gt; by inserting a class &lt;code&gt;.text-h2&lt;/code&gt;. This doesn&amp;#x27;t actually turn it into an &lt;code&gt;h2&lt;/code&gt; tag, but simple gives it that styling.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Learn Vuex Through A Tutorial]]></title><description><![CDATA[In my previous blog post " Introduction To Vuex And State Management ", I explained how state management with Vuex works. If you haven't…]]></description><link>https://dilshankelsen.com/learn-vuex-through-tutorial/</link><guid isPermaLink="false">https://dilshankelsen.com/learn-vuex-through-tutorial/</guid><pubDate>Sat, 26 Dec 2020 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In my previous blog post &amp;quot;&lt;a href=&quot;/introduction-vuex-state-management/&quot;&gt;Introduction To Vuex And State Management&lt;/a&gt;&amp;quot;, I explained how state management with Vuex works. If you haven&amp;#x27;t read it, I highly recommend you do since this will build on top of it. It&amp;#x27;s a quick read and will make this tutorial easier to digest, in which we&amp;#x27;ll learn Vuex through a walk-through tutorial. 👨‍💻&lt;/p&gt;&lt;h2 id=&quot;vuex-3-or-vuex-4&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#vuex-3-or-vuex-4&quot; aria-label=&quot;vuex 3 or vuex 4 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Vuex 3 or Vuex 4&lt;/h2&gt;&lt;p&gt;Are you using version 3 or 4? You&amp;#x27;re in luck! No matter the version, &lt;strong&gt;they are pretty much identical.&lt;/strong&gt; This means any code we go through in this article works for both of them.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:426px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:76.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAPABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAQFAgP/xAAUAQEAAAAAAAAAAAAAAAAAAAAC/9oADAMBAAIQAxAAAAFvklkOkSxH/8QAGxAAAgIDAQAAAAAAAAAAAAAAAQIAEQQSFCH/2gAIAQEAAQUC6PTlkTrlaK4YwKa//8QAFxEBAAMAAAAAAAAAAAAAAAAAAAECEf/aAAgBAwEBPwGKsf/EABURAQEAAAAAAAAAAAAAAAAAABAS/9oACAECAQE/AaP/xAAeEAABAwQDAAAAAAAAAAAAAAAAAREhEBIxM3KRof/aAAgBAQAGPwJrPTXHI1r2I7qYin//xAAbEAEAAwEAAwAAAAAAAAAAAAABABEhMUGBkf/aAAgBAQABPyHNjWdB7pZr5AJkIrGzzLrF33EB7P/aAAwDAQACAAMAAAAQWN//xAAYEQACAwAAAAAAAAAAAAAAAAAAEQExQf/aAAgBAwEBPxBljRh//8QAFxEAAwEAAAAAAAAAAAAAAAAAAAFREf/aAAgBAgEBPxBpTVT/xAAZEAEAAwEBAAAAAAAAAAAAAAABABEhUUH/2gAIAQEAAT8QGeUNncf26pRSltMJeNfRORguXPROFEIKC2LvCf/Z&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Meme about learning Vuex through a tutorial using different versions.&quot; title=&quot;Meme about learning Vuex through a tutorial using different versions.&quot; src=&quot;/static/cb20fb3cd511e8e64fb349b502219cee/823c4/meme-version.jpg&quot; srcSet=&quot;/static/cb20fb3cd511e8e64fb349b502219cee/e07e9/meme-version.jpg 200w,/static/cb20fb3cd511e8e64fb349b502219cee/066f9/meme-version.jpg 400w,/static/cb20fb3cd511e8e64fb349b502219cee/823c4/meme-version.jpg 426w&quot; sizes=&quot;(max-width: 426px) 100vw, 426px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;The devil is in the detail! 😈&lt;/figcaption&gt;&lt;p&gt;Any differences will be clearly be highlighted, so don&amp;#x27;t worry about that. 😋&lt;/p&gt;&lt;h2 id=&quot;setting-up-vuex&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#setting-up-vuex&quot; aria-label=&quot;setting up vuex permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Setting up Vuex&lt;/h2&gt;&lt;p&gt;Talking about differences, here&amp;#x27;s already one! 😂 It concerns the setup of the library. However, &lt;em&gt;there is good news&lt;/em&gt;. &lt;strong&gt;This is where the differences end...&lt;/strong&gt; at least for anything discussed in this blog post. For a full list, I recommend you consult this &lt;a href=&quot;https://next.vuex.vuejs.org/guide/migrating-to-4-0-from-3-x.html&quot; target=&quot;_blank&quot;&gt;Vuex web page&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Ahem... so back to the topic. How exactly do they differ in their setup?&lt;/p&gt;&lt;h3 id=&quot;vuex-3&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#vuex-3&quot; aria-label=&quot;vuex 3 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Vuex 3&lt;/h3&gt;&lt;p&gt;In version 3, we install Vuex plugin with the &lt;code&gt;use()&lt;/code&gt; function in line 5. Then, we create a store instance by calling the constructor &lt;code&gt;Vuex.Store()&lt;/code&gt;. Next, this store gets passed into Vue, which allows us to then access it within our Vue application.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;Vue&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;vue&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;Vuex&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;vuex&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;./App.vue&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;Vue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;Vuex&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; store &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Vuex&lt;/span&gt;&lt;span class=&quot;token class-name punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Store&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;mutations&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;getters&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Vue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  store&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// &amp;lt;-- Pass it in here&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;$mount&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;#app&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;vuex-4&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#vuex-4&quot; aria-label=&quot;vuex 4 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Vuex 4&lt;/h3&gt;&lt;p&gt;In version 4, the first difference is that we use a function called &lt;code&gt;createStore()&lt;/code&gt; to instantiate the Vuex store. However, we still pass the same store configuration as an argument as we did in version 3. Likewise, we install the store with &lt;code&gt;use()&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; createApp &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;vue&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; createStore &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;vuex&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;./App.vue&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; store &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;createStore&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;mutations&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;actions&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;getters&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; app &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;createApp&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;store&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// &amp;lt;-- Use it here&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;mount&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;#app&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;keeping-things-consistent&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#keeping-things-consistent&quot; aria-label=&quot;keeping things consistent permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Keeping things consistent&lt;/h3&gt;&lt;p&gt;In order to keep things consistent throughout the tutorial and avoid confusion as to whether I&amp;#x27;m using version 3 or 4 syntax, I&amp;#x27;ll be using the following layout below. That is, &lt;strong&gt;I will be declaring any state, mutations, etc. outside of the store object.&lt;/strong&gt; In general, you should do this as it lends to more readable and cleaner code. 🤓&lt;/p&gt;&lt;p&gt;In case you didn&amp;#x27;t know it yet, &lt;strong&gt;the store is&lt;/strong&gt; basically &lt;strong&gt;a JavaScript object that contains all your getters, mutations, actions and state.&lt;/strong&gt;&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; mutations &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; actions &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; getters &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Vuex v3 store&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; store &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Vuex&lt;/span&gt;&lt;span class=&quot;token class-name punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Store&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  state&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  mutations&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  actions&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  getters&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Vuex v4 store&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; store &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;createStore&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  state&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  mutations&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  actions&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  getters&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;declaring-and-fetching-the-state&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#declaring-and-fetching-the-state&quot; aria-label=&quot;declaring and fetching the state permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Declaring and fetching the state&lt;/h2&gt;&lt;p&gt;This entire tutorial will revolve around one simple task: &lt;strong&gt;incrementing and decrementing a number.&lt;/strong&gt; Pretty simple, right? Of course we won&amp;#x27;t use local storage, otherwise this tutorial would be over on a whim. 😅&lt;/p&gt;&lt;p&gt;We&amp;#x27;ll first start of with declaring our &lt;code&gt;counter&lt;/code&gt; variable in the state as follows:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The next thing on our to-do list is to fetch the value using a computed property and display it in our &lt;code&gt;App.vue&lt;/code&gt; component.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;&amp;lt;!-- App.vue --&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Increment&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Decrement&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;{{ counter }}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;computed&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript function&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;$store&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;On line 15, you may have noticed that we&amp;#x27;re using &lt;code&gt;$store&lt;/code&gt;. It is essentially a Vue property that is available anywhere throughout the app, which &lt;strong&gt;we can use to access any properties and methods in our store.&lt;/strong&gt; Notice we also added an interpolation to display &lt;code&gt;counter&lt;/code&gt; on line 7.&lt;/p&gt;&lt;p&gt;An alternative to directly reading &lt;code&gt;counter&lt;/code&gt; from our central state would be using a getter function. So how would we do that and &lt;em&gt;why&lt;/em&gt;? 🧐&lt;/p&gt;&lt;h2 id=&quot;using-getters&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#using-getters&quot; aria-label=&quot;using getters permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Using Getters&lt;/h2&gt;&lt;p&gt;To retrieve our value using a getter function, we&amp;#x27;d first have to create one in our store.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; getters &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getCounter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; state&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As you can see on line 2, getters automatically have access to the state from which we can return our &lt;code&gt;counter&lt;/code&gt; value. Back in our &lt;code&gt;App.vue&lt;/code&gt; file, we call the getter function &lt;code&gt;getCounter&lt;/code&gt; in the computed 🖥 property named &lt;code&gt;counter&lt;/code&gt;, replacing the line of code we wrote earlier.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;computed&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript function&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;$store&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;getters&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;getCounter&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So again, what&amp;#x27;s the advantage of using a getter? Well, &lt;strong&gt;DRY code&lt;/strong&gt; my friend! 😊 A getter is for all intents and purposes &lt;strong&gt;a centralized computed property&lt;/strong&gt; that can be shared among all your Vue components. If you ever need to make changes to a getter, you only need to do it once! However, there&amp;#x27;s nothing really stopping you from just using computed properties in your App that all duplicate the same logic. 😕&lt;/p&gt;&lt;h2 id=&quot;manipulating-the-state&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#manipulating-the-state&quot; aria-label=&quot;manipulating the state permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Manipulating the state&lt;/h2&gt;&lt;p&gt;Our next objective is to make the buttons work, so whenever we click on one, our number is either incremented or decremented. As you&amp;#x27;ll (hopefully) know from reading my &lt;a href=&quot;/introduction-vuex-state-management/&quot;&gt;previous Vuex article&lt;/a&gt;, &lt;strong&gt;any changes we make to our state should be done through a mutation&lt;/strong&gt;, which itself is called by an action.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; mutations &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    state&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;decrement&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    state&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; actions &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;commit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;increment&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;decrement&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;commit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;decrement&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Starting with mutations, we have two functions for incrementing and decrementing &lt;code&gt;counter&lt;/code&gt;. Mutations automatically have access to the state which allow us to directly change the state.&lt;/p&gt;&lt;p&gt;Next, we have to create two action functions &lt;code&gt;increment&lt;/code&gt; ➕ and &lt;code&gt;decrement&lt;/code&gt; ➖, which call, aka &lt;em&gt;commit&lt;/em&gt;, the mutation functions of the same name. Contrary to mutations, &lt;strong&gt;actions automatically have access to a &lt;code&gt;context&lt;/code&gt; object&lt;/strong&gt;, which let&amp;#x27;s us use this &lt;code&gt;commit&lt;/code&gt; method.&lt;/p&gt;&lt;p&gt;The only thing left to do now is to call, aka &lt;em&gt;dispatch&lt;/em&gt;, the action functions from our &lt;code&gt;App.vue&lt;/code&gt; file. Similarly, &lt;strong&gt;&lt;code&gt;$store&lt;/code&gt; gives us access to the dispatch functions&lt;/strong&gt; to do so.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;methods&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;$store&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript method function property-access&quot;&gt;dispatch&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token script language-javascript string&quot;&gt;&amp;#x27;increment&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript function&quot;&gt;decrement&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript property-access&quot;&gt;$store&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token script language-javascript method function property-access&quot;&gt;dispatch&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token script language-javascript string&quot;&gt;&amp;#x27;decrement&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;computed&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript spread operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Don&amp;#x27;t forget to add &lt;code&gt;@click=&amp;quot;increment()&amp;quot;&lt;/code&gt; and &lt;code&gt;@click=&amp;quot;decrement()&amp;quot;&lt;/code&gt; to each respective button, otherwise nothing will happen. Et voilà, we&amp;#x27;ve made it! 🤓&lt;/p&gt;&lt;p&gt;But the action doesn&amp;#x27;t end here yet, buddy...&lt;/p&gt;&lt;h2 id=&quot;simplifying-the-code&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#simplifying-the-code&quot; aria-label=&quot;simplifying the code permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Simplifying the code&lt;/h2&gt;&lt;p&gt;Let&amp;#x27;s imagine you not only have to call two actions, but perhaps a few dozen, thousands or millions 🤯 (okay, maybe I&amp;#x27;m exaggerating, but you get the point)! It&amp;#x27;s not hard to imagine that you will quickly lose oversight of what you&amp;#x27;re doing. But worry not, for there is a solution called &lt;code&gt;mapActions&lt;/code&gt;. Similarly, equivalent &lt;em&gt;maps&lt;/em&gt; exist for the other three.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;&amp;lt;!-- Make sure to change the function names --&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;@click&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;incrementAction()&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Increment&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;@click&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;decrementAction()&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Decrement&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;{{ counter }}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript imports&quot;&gt; mapActions&lt;/span&gt;&lt;span class=&quot;token script language-javascript imports punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token script language-javascript imports&quot;&gt; mapState &lt;/span&gt;&lt;span class=&quot;token script language-javascript imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript string&quot;&gt;&amp;#x27;vuex&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;computed&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript spread operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token script language-javascript method function property-access&quot;&gt;mapState&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token script language-javascript string&quot;&gt;&amp;#x27;counter&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;methods&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript spread operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token script language-javascript method function property-access&quot;&gt;mapActions&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;incrementAction&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript string&quot;&gt;&amp;#x27;increment&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;decrementAction&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript string&quot;&gt;&amp;#x27;decrement&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;First of all, we have to import the relevant helpers into our Vue component, which are &lt;code&gt;mapActions&lt;/code&gt; and &lt;code&gt;mapState&lt;/code&gt;. &lt;strong&gt;Upon calling these helpers, we get access to Objects containing our state and actions.&lt;/strong&gt; Notice ‼️ that they both make use of an ES6 syntax called the &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax&quot; target=&quot;_blank&quot;&gt;object spread operator&lt;/a&gt; (the &lt;code&gt;...&lt;/code&gt; if you weren&amp;#x27;t sure what I&amp;#x27;m referring to).&lt;/p&gt;&lt;p&gt;You also have &lt;strong&gt;two options regarding how to reference your methods and properties&lt;/strong&gt;. You can do so directly, as seen in &lt;code&gt;mapState&lt;/code&gt; (pay attention to the square brackets), or indirectly through a custom name, as seen in &lt;code&gt;mapActions&lt;/code&gt; (pay attention to the curly braces).&lt;/p&gt;&lt;h2 id=&quot;adding-function-inputs&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#adding-function-inputs&quot; aria-label=&quot;adding function inputs permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Adding function inputs&lt;/h2&gt;&lt;p&gt;What if we want to pass in some arguments that our mutations or actions can mess around with? 🤾‍♂️ How would we do that in Vuex? It&amp;#x27;s quite simple, really. Going back to our store file, we do the following changes:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; mutations &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; payload&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    state&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; payload&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;decrement&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; payload&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    state&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; payload&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; actions &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; payload&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;commit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;increment&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; payload&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;decrement&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; payload&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    context&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;commit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;decrement&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; payload&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;We simply added another parameter called &lt;em&gt;payload&lt;/em&gt; in each function.&lt;/strong&gt; Naturally, we changed the code a bit in the mutations to make use of our new input. Next, we dispatch the actions giving an input with &lt;code&gt;@click=&amp;quot;incrementAction(2)&amp;quot;&lt;/code&gt; and &lt;code&gt;@click=&amp;quot;decrementAction(2)&amp;quot;&lt;/code&gt;. In this scenario, whenever we click on a button, our counter will be either incremented or decremented by two.&lt;/p&gt;&lt;p&gt;In case you were wondering 🤔 how to &lt;strong&gt;pass in multiple inputs&lt;/strong&gt; into our actions and mutations, you would simply pass &lt;strong&gt;in an object&lt;/strong&gt; in the following manner: &lt;code&gt;@click=&amp;quot;incrementAction({ value: 2 })&amp;quot;&lt;/code&gt;. Now, to get the value in your mutations, you would simply reference it with &lt;code&gt;payload.value&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;If you&amp;#x27;ve made it this far, congratulations! 🎉 You&amp;#x27;re well on your way to mastering Vuex. However, it shouldn&amp;#x27;t come as a surprise that &lt;strong&gt;Vuex provides many other features&lt;/strong&gt; I wasn&amp;#x27;t able to cover in this article, so why not go and &lt;a href=&quot;https://vuex.vuejs.org/&quot; target=&quot;_blank&quot;&gt;check it out&lt;/a&gt;?&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Introduction To Vuex And State Management]]></title><description><![CDATA[State Management can be a hard topic to grasp at first. However, once you start building larger applications with more components, you'll…]]></description><link>https://dilshankelsen.com/introduction-vuex-state-management/</link><guid isPermaLink="false">https://dilshankelsen.com/introduction-vuex-state-management/</guid><pubDate>Sat, 19 Dec 2020 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;State Management can be a hard topic to grasp at first. However, once you start building larger applications with more components, you&amp;#x27;ll realize that it is an indispensable tool in many of your projects. Fortunately, I have found Vuex&amp;#x27;s implementation of &lt;em&gt;Flux&lt;/em&gt; to be a bit easier to learn than other alternatives, such as Redux for React. So stay strapped and let me give you an introduction to Vuex and the world of state management. 😎&lt;/p&gt;&lt;h2 id=&quot;the-importance-of-state-management&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-importance-of-state-management&quot; aria-label=&quot;the importance of state management permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The importance of state management&lt;/h2&gt;&lt;p&gt;When you first start out building your own Single Page Applications (SPA), you learn about the concepts of props and events to send data between various components. Initially, everything is fine and dandy. However, &lt;strong&gt;things can get really messy&lt;/strong&gt; (and buggy) &lt;strong&gt;after you have dozens of components&lt;/strong&gt; scattered throughout the applications &lt;strong&gt;sharing and modifying data.&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;You get a headache just thinking about it. 😓 Even worse, a global event bus in Vue doesn&amp;#x27;t really offer a proper long-term solution to your problem.&lt;/p&gt;&lt;p&gt;Just imagine this issue happening on a large scale. Something as big as Facebook... because it DID in fact happen! Luckily, &lt;strong&gt;Facebook&lt;/strong&gt; already suffered for our sins and &lt;strong&gt;came up with a solution of their own: the &lt;a href=&quot;https://facebook.github.io/flux/&quot; target=&quot;_blank&quot;&gt;Flux&lt;/a&gt; architecture.&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Vuex is merely one of many other implementations of this solution.&lt;/p&gt;&lt;h2 id=&quot;the-principles&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-principles&quot; aria-label=&quot;the principles permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The principles&lt;/h2&gt;&lt;p&gt;Before we dive into the logic of Vuex, let&amp;#x27;s first try to understand the principles behind the Flux pattern. What exactly makes it so great?&lt;/p&gt;&lt;p&gt;First, there&amp;#x27;s a &lt;strong&gt;single source of truth&lt;/strong&gt;. ☝️ That applies to shared data of course, which is stored in a centralized location. Whatever the state says, goes. Of course, this means that no components may save any copies locally.&lt;/p&gt;&lt;p&gt;Second, &lt;strong&gt;changes&lt;/strong&gt; to this single source of truth &lt;strong&gt;are unidirectional&lt;/strong&gt;! No &lt;a href=&quot;https://stackoverflow.com/questions/13504906/what-is-two-way-binding&quot; target=&quot;_blank&quot;&gt;two-way binding&lt;/a&gt;, you filthy peasant! If you wish to alter the state, you must go through a standard process that can be easily traced back, if needed.&lt;/p&gt;&lt;p&gt;This is the opposite to the traditional &lt;a href=&quot;https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&quot; target=&quot;_blank&quot;&gt;MVC design pattern&lt;/a&gt; where two-way binding isn&amp;#x27;t uncommon. But again, this gives rise to scaling issues that Facebook encountered.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:375px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:129%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAaABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAMEAgH/xAAXAQADAQAAAAAAAAAAAAAAAAAAAgMB/9oADAMBAAIQAxAAAAFFCHA8nF2rG52KDhOv/8QAHBAAAgICAwAAAAAAAAAAAAAAAQIAEgMREzFC/9oACAEBAAEFAuRTAbHdRjAUksFZczR1NZqeTB1//8QAFxEBAAMAAAAAAAAAAAAAAAAAAQAQEv/aAAgBAwEBPwECZv8A/8QAFhEAAwAAAAAAAAAAAAAAAAAAABAS/9oACAECAQE/ASn/AP/EAB4QAAICAgIDAAAAAAAAAAAAAAABESECEDEyIpGi/9oACAEBAAY/AqTIWNnTL0NvGVAvFnX6Jma4IuDhaev/xAAeEAEAAwACAgMAAAAAAAAAAAABABEhMUFRcZGh0f/aAAgBAQABPyEoGgDqLlAc0QoH1I8ERvZMAUy2t+IkN8KyBV3NX8R5t6ZTzS15dRtbjU7P/9oADAMBAAIAAwAAABBcAT//xAAYEQADAQEAAAAAAAAAAAAAAAAAASERcf/aAAgBAwEBPxBCOHDJuiq0/8QAGBEBAQEBAQAAAAAAAAAAAAAAAQARMUH/2gAIAQIBAT8QV6Q/UtcyS//EAB0QAQEAAwEAAwEAAAAAAAAAAAERACExQVFhgZH/2gAIAQEAAT8QRLIAkaJ8YrPYUI/smMvytpcda9fR3Z/T8xOCgOgvynwDGwgRZUPX7wzCzCEmoDyve/WAToEq3snb5rE9Kj3lw2CdOv0YxFUCN5txwUs6uf/Z&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Meme of Flux&amp;#x27;s state management principle&quot; title=&quot;Meme of Flux&amp;#x27;s state management principle&quot; src=&quot;/static/6307ec9d36893b1130a4fddbe86e7fdf/bf173/meme-flux-direction.jpg&quot; srcSet=&quot;/static/6307ec9d36893b1130a4fddbe86e7fdf/e07e9/meme-flux-direction.jpg 200w,/static/6307ec9d36893b1130a4fddbe86e7fdf/bf173/meme-flux-direction.jpg 375w&quot; sizes=&quot;(max-width: 375px) 100vw, 375px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;What you should take away 😝&lt;/figcaption&gt;&lt;h2 id=&quot;how-vuex-works&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#how-vuex-works&quot; aria-label=&quot;how vuex works permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;How Vuex works&lt;/h2&gt;&lt;p&gt;Alright, with the introduction to state management behind us, it&amp;#x27;s time we start pounding Vuex into your brain. 🤪&lt;/p&gt;&lt;p&gt;The idea of Vuex is really simple and really similar: store your data in a centralized location that all your components can easily access and mutate. However, components can&amp;#x27;t just access and mutate the global state willy nilly. Rather, they need to follow a strict set of steps in order to do so. This will make any state changes predictable and traceable.&lt;/p&gt;&lt;p&gt;Vuex works with the following concepts: &lt;strong&gt;State, Getters, Mutations, Actions&lt;/strong&gt; and &lt;strong&gt;Modules&lt;/strong&gt;. Although, Modules will be left out in this article for some other time as they aren&amp;#x27;t necessary to the Vuex workflow.&lt;/p&gt;&lt;h3 id=&quot;state&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#state&quot; aria-label=&quot;state permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;State&lt;/h3&gt;&lt;p&gt;The State essentially acts as that &amp;quot;single source of truth&amp;quot; I mentioned earlier in which your application data is stored. It&amp;#x27;s the equivalent of the data property in a Vue instance, but on a global level instead.&lt;/p&gt;&lt;h3 id=&quot;getters&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#getters&quot; aria-label=&quot;getters permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Getters&lt;/h3&gt;&lt;p&gt;Getters, as the name implies, allow you to get any stored data from the State and are identical to computed properties. This means they are reactive to any dependency changes and can manipulate the output, but not the data itself.&lt;/p&gt;&lt;h3 id=&quot;mutations&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#mutations&quot; aria-label=&quot;mutations permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Mutations&lt;/h3&gt;&lt;p&gt;On the other hand, Mutations are functions that are able to update the Vuex state and are equivalent to method properties in a Vue instance (Hmmm, are you starting to see a pattern here? 🤔). However, we have a teeny tiny problem: Mutations only work synchronously.&lt;/p&gt;&lt;h3 id=&quot;actions&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#actions&quot; aria-label=&quot;actions permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Actions&lt;/h3&gt;&lt;p&gt;So it seems that the previously discussed concepts strongly resemble Vue instance properties that we already know and love, but Actions is not one of them. Rather, it addresses the previously mentioned problem by allowing asynchronous operations, or any computations for that matter. Subsequently, Actions call and pass down any data changes to Mutations, as only the latter can mutate the state.&lt;/p&gt;&lt;p&gt;It is generally regarded as a best practice to have an action call a mutation even if it isn&amp;#x27;t necessary (e.g. you aren&amp;#x27;t performing any asynchronous operations).&lt;/p&gt;&lt;h2 id=&quot;come-again&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#come-again&quot; aria-label=&quot;come again permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Come again?&lt;/h2&gt;&lt;p&gt;If you didn&amp;#x27;t understand everything on your first read, don&amp;#x27;t worry. Before I let you off the hook, &lt;strong&gt;I&amp;#x27;ve got a pretty graph&lt;/strong&gt; to make things easier to understand. 😄&lt;/p&gt;&lt;p&gt;In Image 1, the whole process starts off with a Vue Component calling, aka &amp;quot;dispatching&amp;quot;, an action function. Said function might be doing some asynchronous API calls and computations with some data from the state. Next, the action calls a mutation function and &amp;quot;commits&amp;quot; any changes it made to it, i.e. it passes on the new values to it.&lt;/p&gt;&lt;p&gt;Finally, the mutation function updates the state with these changes, or in other words it&amp;#x27;s &amp;quot;mutating&amp;quot; the state. These changes are automatically updated and rendered in any dependent components.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:76.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAIAAABr+ngCAAAACXBIWXMAAAsTAAALEwEAmpwYAAACA0lEQVQoz4VTXWsTQRTdf+Pf8B/okwqCgk+CiL4EREQQX8xDK9JYpYjYprYitDZFX1LUthKwHyIqtUFsRUObEtNsdmd25/Pe8e5uGps2pZflMnt3zpl77pz13L5ARMq1euPF67fT5YXnpbn1jd/d+uHw9r8AAOWVb9XLtwdy+UeXbuTLH1aoYtP6MeAuBeOR324HIWsLrsG6I6IPWIOxDg0d6HBXhkwLwlNRgTG9RAfBiKBJoGqpzZfAa1mp0xEC4fuDtbFNPwjCMLZOf7lnSyfs8jWpbasd8CimbkjOAQkeJpRIX3gcS62FEtw483Mcyift2qAmIq2lMRJoFyprKEMa/0+mklams0ASC8g2nRXZV9XcbS5WDGPa9dyZF+g4tzp5fbmYW524svSsUC2TNqKwmHTk0t3bD4arp87WR8eJbHJ2rlCcLhRfbe389VqKX6g8PrMwdH7x4en5+3e/zqSYvSPSdePp2I9zFxtTM8Lh0OjUzYGRW4NPftXqSduRUS3Jd0I/tEJYGjb6zejdm/XtP37GYEIWrFWtlBohNULHcl3NTinNWET2UKBmJz7fuVoaK1RiIX3ySiw0zanvVWGCJTKMYiGkItLvn7ZG8vMf328kfcU0bOtScHZVuCfqkEkcEj0RMS4sAj3gaHhA3lKpyY6xZ7aVMJRNujYAHaLe3+sfco1KrCAPVMsAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;A graphical introduction to how Vuex and its state management works.&quot; title=&quot;A graphical introduction to how Vuex and its state management works.&quot; src=&quot;/static/19b7b94c59e0aecca0a2c07f512686e9/5a190/figure-1.png&quot; srcSet=&quot;/static/19b7b94c59e0aecca0a2c07f512686e9/772e8/figure-1.png 200w,/static/19b7b94c59e0aecca0a2c07f512686e9/e17e5/figure-1.png 400w,/static/19b7b94c59e0aecca0a2c07f512686e9/5a190/figure-1.png 800w,/static/19b7b94c59e0aecca0a2c07f512686e9/b2cef/figure-1.png 847w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;&lt;strong&gt;Image 1. &lt;/strong&gt;Vuex Process Visualised&lt;/figcaption&gt;</content:encoded></item><item><title><![CDATA[How To Access Localhost On Another Device]]></title><description><![CDATA[So there you are, tapping away on your computer and running the next Facebook locally. You're in the final stages of development and you're…]]></description><link>https://dilshankelsen.com/how-to-access-localhost-on-another-device/</link><guid isPermaLink="false">https://dilshankelsen.com/how-to-access-localhost-on-another-device/</guid><pubDate>Sat, 12 Dec 2020 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;So there you are, tapping away on your computer and running the next Facebook locally. You&amp;#x27;re in the final stages of development and you&amp;#x27;re about to do some testing on a few devices. However, then you realize: &lt;em&gt;&amp;quot;How do I access my localhost on another device?&amp;quot;&lt;/em&gt;. 🤔 Well, you&amp;#x27;re at the right spot, buddy (or budette...?). In this article I&amp;#x27;ll show you how this can be done not only through your local WIFI network, but through the internet as well! And the answer to the latter isn&amp;#x27;t &lt;em&gt;staging&lt;/em&gt;...&lt;/p&gt;&lt;h2 id=&quot;what-is-localhost&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#what-is-localhost&quot; aria-label=&quot;what is localhost permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;What is localhost?&lt;/h2&gt;&lt;p&gt;Some of you may know it already, others may not. It&amp;#x27;s something we use quite often as developers, but never really try to understand in depth. 😥 But don&amp;#x27;t get me wrong... I don&amp;#x27;t want to bore you with the details. There&amp;#x27;s a &lt;a href=&quot;https://en.wikipedia.org/wiki/Localhost&quot; target=&quot;_blank&quot;&gt;Wikipedia page&lt;/a&gt; for that. I simply want to give you a rough idea of what&amp;#x27;s going on in that beautiful computer of yours.&lt;/p&gt;&lt;p&gt;So starting off, did you know that &lt;code&gt;localhost&lt;/code&gt; is a domain name just like &lt;code&gt;chunkbytes&lt;/code&gt;? And just like any other domain name, it&amp;#x27;s a human readable version of its IP address, aka the location (i.e. the server) of your website. With that said, the IP address of &lt;code&gt;localhost&lt;/code&gt; tends to be &lt;code&gt;127.0.0.1&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Whenever you look up a domain in your browser, it makes a call to a server over the internet, but this isn&amp;#x27;t the case with &lt;code&gt;localhost&lt;/code&gt;. Oh no, my friend! 😛 Instead, the call gets looped back to the computer itself. So essentially, &lt;code&gt;localhost&lt;/code&gt; is pointing to your own computer.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Fun fact time:&lt;/strong&gt; Any IP that starts with &lt;code&gt;127&lt;/code&gt; automatically loops back to your own device.&lt;/p&gt;&lt;span class=&quot;text-center&quot;&gt;&lt;p&gt;  &lt;img src=&quot;/static/not-paying-attention-35a7b0e3ee61efac69b4e13fc8d053f5.gif&quot; alt=&quot;Falling asleep while reading the technical section of how to access your localhost on another device.&quot;/&gt;&lt;/p&gt;&lt;/span&gt;&lt;figcaption&gt;Reading through the networking section be like...&lt;/figcaption&gt;&lt;h2 id=&quot;through-a-local-network&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#through-a-local-network&quot; aria-label=&quot;through a local network permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Through a local network&lt;/h2&gt;&lt;p&gt;So where were we? Oh yeah, accessing localhost on another device. The solution is quite simple, really... If &lt;code&gt;localhost&lt;/code&gt; represents, what I like to call, the &lt;em&gt;&amp;quot;internal&amp;quot;&lt;/em&gt; IP address for your PC to access itself, then we simply need the &lt;em&gt;&amp;quot;external&amp;quot;&lt;/em&gt; IP for other devices. 💁‍♂️&lt;/p&gt;&lt;p&gt;And how do we find this out? Alright boys and girls, whip out your terminals and run the following command:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Windows: &lt;code&gt;ipconfig&lt;/code&gt;&lt;/li&gt;&lt;li&gt;Linux/Mac: &lt;code&gt;ifconfig&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Below are two examples of the terminal output with the IP address that we&amp;#x27;re after: &lt;code&gt;192.168.2.50&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;powershell&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-powershell&quot;&gt;POWERSHELL&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-powershell&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;/* Windows */&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Wireless LAN adapter Wi-Fi:&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;   Connection-specific DNS Suffix  . : home&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;   IPv6 Address. . . . . . . . . . . : 2a02:8388:8881:c200...&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;   Temporary IPv6 Address. . . . . . : 2a02:8388:8881:c200...&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;   Link-local IPv6 Address . . . . . : fe80::1dc5:f047...&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;   IPv4 Address. . . . . . . . . . . : 192.168.2.50&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;   [...]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Unfortunately, I couldn&amp;#x27;t try this out on a Mac. 🍎 Nonetheless, look out for the keyword &lt;em&gt;WiFi&lt;/em&gt; and the IP shouldn&amp;#x27;t be too far away.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;/* Linux */&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;wifi0: &lt;/span&gt;&lt;span class=&quot;token assign-left variable&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;416&lt;/span&gt;&lt;span class=&quot;token operator file-descriptor important&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;UP,BROADCAST,RUNNING,MULTICAST&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;  mtu &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1500&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    inet &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;192.168&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;.0.45  netmask &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;244.244&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;.244.0 &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    inet6 2a02:8388:8881:c200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;. &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    inet6 2a02:8388:8881:c200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So back the to the main question: &lt;em&gt;&amp;quot;How do I access localhost on another device?&amp;quot;&lt;/em&gt;. Knowing that the IP in this example is &lt;code&gt;192.168.2.50&lt;/code&gt; and assuming you&amp;#x27;re running your project on port 3000, you can then access your project on another device 📱 with &lt;code&gt;192.168.2.50:3000&lt;/code&gt; in your browser. That&amp;#x27;s it!&lt;/p&gt;&lt;h2 id=&quot;through-the-internet&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#through-the-internet&quot; aria-label=&quot;through the internet permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Through the internet&lt;/h2&gt;&lt;p&gt;What if I told you... you can also share your localhost through the internet? Not joking! 😁 There are several tools, paid and free, that allow you to accomplish this marvelous feat of technology. The most well-known tool is &lt;a href=&quot;https://ngrok.com/&quot; target=&quot;_blank&quot;&gt;ngrok&lt;/a&gt;, which is also the one we&amp;#x27;ll be using in this tutorial.&lt;/p&gt;&lt;p&gt;And how does it work? Again, without boring you with the technical details: tools like ngrok basically expose your localhost to the internet, which can then be accessed via a subdomain of &lt;code&gt;ngrok.com&lt;/code&gt;. Even though ngrok has paid plans, their free version should be plenty for most developer needs.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:390px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:92.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAATABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAME/8QAFQEBAQAAAAAAAAAAAAAAAAAAAgH/2gAMAwEAAhADEAAAAdRU3IqRVCohH//EABwQAAICAgMAAAAAAAAAAAAAAAABAxECMhASMf/aAAgBAQABBQIXFdhYIk28E3cm/wD/xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAEDAQE/AR//xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAECAQE/AR//xAAZEAACAwEAAAAAAAAAAAAAAAAQIQARImH/2gAIAQEABj8CU0OSqQR//8QAHBAAAwEAAgMAAAAAAAAAAAAAAAERMRAhQVGB/9oACAEBAAE/IWqpo84S+8KHucJ12JiZFweX0o0tb00P/9oADAMBAAIAAwAAABAPP/8A/8QAFhEBAQEAAAAAAAAAAAAAAAAAARAR/9oACAEDAQE/EMIz/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQAQEf/aAAgBAgEBPxDrhF//xAAdEAEAAgICAwAAAAAAAAAAAAABABExUSFxQWGx/9oACAEBAAE/EApVCNbhbOABSdVERbzAMB4FNQJl0QmGABiANbibm+VeYTKGiXOInr5P/9k=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Meme about ngrok being free&quot; title=&quot;Meme about ngrok being free&quot; src=&quot;/static/f0f7a5ba606e2f46cd87a0ac2258e66c/e52b6/meme-free-stuff.jpg&quot; srcSet=&quot;/static/f0f7a5ba606e2f46cd87a0ac2258e66c/e07e9/meme-free-stuff.jpg 200w,/static/f0f7a5ba606e2f46cd87a0ac2258e66c/e52b6/meme-free-stuff.jpg 390w&quot; sizes=&quot;(max-width: 390px) 100vw, 390px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Don&amp;#x27;t we all love free stuff? 😂&lt;/figcaption&gt;&lt;h3 id=&quot;sign-up&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#sign-up&quot; aria-label=&quot;sign up permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Sign up&lt;/h3&gt;&lt;p&gt;Sorry lads (and ladettes...?), but even using their free version will require you to &lt;a href=&quot;https://dashboard.ngrok.com/signup&quot; target=&quot;_blank&quot;&gt;create an account&lt;/a&gt; with them. But hey, at least you don&amp;#x27;t need to give them your credit card details, am I right? 😆&lt;/p&gt;&lt;h3 id=&quot;download-ngrok&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#download-ngrok&quot; aria-label=&quot;download ngrok permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Download ngrok&lt;/h3&gt;&lt;p&gt;Next, &lt;a href=&quot;https://dashboard.ngrok.com/get-started/setup&quot; target=&quot;_blank&quot;&gt;download&lt;/a&gt; the actual program that allows this magic goodness to happen and extract it from the &lt;code&gt;zip&lt;/code&gt; file.&lt;/p&gt;&lt;h3 id=&quot;setting-it-up&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#setting-it-up&quot; aria-label=&quot;setting it up permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Setting it up&lt;/h3&gt;&lt;p&gt;With the ngrok program extracted, navigate your terminal&amp;#x27;s directory to the same location. Then run the following command with your authentication token as the last parameter. This will basically save your authentication key to ngrok&amp;#x27;s &lt;code&gt;yml&lt;/code&gt; configuration file.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;./ngrok authtoken 1kK5xphsYvZGLpWsK2JVAB&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;.&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;share-your-localhost&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#share-your-localhost&quot; aria-label=&quot;share your localhost permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Share your localhost&lt;/h3&gt;&lt;p&gt;Almost there! Now, with your terminal in the same directory as before, run the final command below. The last parameter is the port you want to expose to the internet. In our example, it&amp;#x27;s &lt;code&gt;3000&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;./ngrok http &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3000&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This will launch ngrok and display some information in your terminal. One of them will be the link under which you can access your localhost. Look out for this line: 👇&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Forwarding      http://5fb075d.ngrok.io -&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; http://localhost:3000&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And there you go, you can now access your local app through the internet with the help of ngrok! 🎉🥳&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Install WordPress Using XAMPP]]></title><description><![CDATA[Do you want to install WordPress on your local machine using XAMPP? Do you need a simple and straightforward installation guide? Well, look…]]></description><link>https://dilshankelsen.com/how-to-install-wordpress-using-xampp/</link><guid isPermaLink="false">https://dilshankelsen.com/how-to-install-wordpress-using-xampp/</guid><pubDate>Sat, 05 Dec 2020 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Do you want to install WordPress on your local machine using XAMPP? Do you need a simple and straightforward installation guide? Well, look no further! I&amp;#x27;ve got you covered. Just follow the steps below and you&amp;#x27;ll have WordPress up and running in no time. ✊&lt;/p&gt;&lt;p&gt;In case you plan on following this guide &lt;strong&gt;on Windows, I recommend&lt;/strong&gt; you opt for a more powerful and convenient development setup: &lt;strong&gt;Laragon.&lt;/strong&gt; &lt;a href=&quot;/laragon-wordpress-development-environment/&quot;&gt;I have an article&lt;/a&gt; explaining its advantages over conventional tools like XAMPP. It even has a brief setup guide to get you started. Unfortunately, this tool is only available on Windows. Sorry Mac users... 😞&lt;/p&gt;&lt;h2 id=&quot;step-1-download-and-install-xampp&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-1-download-and-install-xampp&quot; aria-label=&quot;step 1 download and install xampp permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 1: Download and Install XAMPP&lt;/h2&gt;&lt;p&gt;Alright! First thing to do: &lt;a href=&quot;https://www.apachefriends.org/index.html&quot; target=&quot;_blank&quot;&gt;download&lt;/a&gt; XAMPP and install it. The installation process is quite straightforward, so you&amp;#x27;ll be on your own for that part. 😉 Just remember: when in doubt, leave the default installation settings as is.&lt;/p&gt;&lt;p&gt;Ignore any mentions of Bitnami that you encounter.&lt;/p&gt;&lt;h2 id=&quot;step-2-launch-xampp&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-2-launch-xampp&quot; aria-label=&quot;step 2 launch xampp permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 2: Launch XAMPP&lt;/h2&gt;&lt;h3 id=&quot;on-mac&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#on-mac&quot; aria-label=&quot;on mac permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;On Mac&lt;/h3&gt;&lt;p&gt;After it has been installed, &lt;strong&gt;start the application&lt;/strong&gt; and launch the server by &lt;strong&gt;clicking &amp;quot;Start&amp;quot;&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:515px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:89.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAYAAABb0P4QAAAACXBIWXMAABYlAAAWJQFJUiTwAAACYElEQVQ4y6VU207bQBD13/exv9Bf6CuItgKSAlIJhSSOSXxbe+Nbbg6J1Kc9zFnHISRCldqHIx/P5cx6dsZO0f1uHoZf8XNyb7Iohop8JIIwDBFFEXzfx+3tLe7u7hAEQWMPQ0MuMAdg7B+nLAqUZWEEGMUzPEVzDOIFplkOnabiKzGdTpEKr6oKeZ4L1+SGvsLmlxb0ObPZzGg9xawqzJebF3w6f8HnH1t4voI7HNggpRT6/b5NSoQPBgMrliTKiM9kRWGkKG3GqRoHg0RgaE/CpCRJLFc7rrXe89ZuEcdIPM/QLsWNw0oMvrj4hrOzc3sSJU4G2KdNThoxFmvtSbp/T4dDQy6fLyeUvtD42LvHTecanctLBOMxVBggDggfWsVI48jyxnYEnlI02E+nms2gnx5Qy81udCJQqFWIVUwEWCcxwodfUE+/LW/sb/5abMtRH9pzkcvhGkG3j0U4wUpFWDJQni3qVEap/4jUHdjkQ9+KhVOFxdiFHnvIpX37T8619GCqUej3KLMpEpk9osqyEz/B3LdP3gkWnCXhZbmD8ExGJpThDkRsIgPuS79yzt1RHG0ngplU58wdggHcFm4HN4bNp+04jrkngnzO53PIoFuQFxJMIWIsNz+Z+DbpOK4Z8iPBxWKB1Wq1R13XyGTlPM+zQ//8/AzXde0a0rdcLvexFD0RpGO9Xu/BpO12a33DoWtFmbDZbKzvMJbi7wS5BVwnbswh0t0msHfsZbuCH8XZn0P7B2n/GB+hsjf7d7/DCiTtKf4HvDyn1+thNBqh2+3i+lp2udP5Z1xdXeEV46IhJYzWzOwAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Install Wordpress Using XAMPP: XAMPP General Interface&quot; title=&quot;Install Wordpress Using XAMPP: XAMPP General Interface&quot; src=&quot;/static/3060209334c484edcb9c7543e93012c3/fbdcb/image-1.png&quot; srcSet=&quot;/static/3060209334c484edcb9c7543e93012c3/772e8/image-1.png 200w,/static/3060209334c484edcb9c7543e93012c3/e17e5/image-1.png 400w,/static/3060209334c484edcb9c7543e93012c3/fbdcb/image-1.png 515w&quot; sizes=&quot;(max-width: 515px) 100vw, 515px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Time to start the engine, boys and girls! 🚗&lt;/figcaption&gt;&lt;p&gt;This should &lt;strong&gt;automatically start the 3 required services&lt;/strong&gt; (Apache, MySQL, ProFTPD) in the &lt;em&gt;Services&lt;/em&gt; tab. However, if they haven&amp;#x27;t, make sure to start them manually. Next, you want to &lt;strong&gt;choose a port&lt;/strong&gt; for your server in the &lt;em&gt;Network&lt;/em&gt; tab. I choose you, number 8080...&lt;/p&gt;&lt;p&gt;Lastly, in the &lt;em&gt;Volumes&lt;/em&gt; tab, you want to &lt;strong&gt;mount the data volume&lt;/strong&gt; &lt;code&gt;/opt/lampp&lt;/code&gt;. At this point, if you visit &lt;code&gt;localhost:8080&lt;/code&gt; in your browser, you&amp;#x27;ll be greeted by a beautiful XAMPP page. 😍&lt;/p&gt;&lt;h3 id=&quot;on-windows&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#on-windows&quot; aria-label=&quot;on windows permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;On Windows&lt;/h3&gt;&lt;p&gt;For the Windows folks, it&amp;#x27;s a little different...&lt;/p&gt;&lt;p&gt;First, make sure you &lt;strong&gt;start the XAMPP Control Panel as an admin.&lt;/strong&gt; This can be done by right clicking on the program and choosing &lt;code&gt;Run as administrator&lt;/code&gt;. Next, you&amp;#x27;ll be greeted by the following window.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:62%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAA7EAAAOxAGVKw4bAAACIUlEQVQoz41T686aQBTk/R+jT9AnaNMfTb74eQG5CQqiogICclNR1Onsam1MmrQkk0Pg7OzMnlml9oZItlukaYY4ihBFMfIsR7SNkMQJ4jhGUZS4Xq/oLhd0XfeGM7/lRYW8rFE1RyjJ9y+IfQtlc4BtmTAtC47jwGI1DEO+z4MAZVWRuHhhv9+jZE3SHD80F1+/9WB5IZTfC3Vdlwt930MYhqjKEsvlEjPXxeSzB0/VkNBBnDxUJ8+apSk+DQ0/Pz7oLoKyXq+x2WwQkExUsXOe548qlPCbo2mwVRV6rwfTtmGZplQuhIhNbdNAfzBAxA0kocBisSDhFiWVCTIJYY8KZmx2+33ooxEsEtp05VK5cBcECwSehdFwQGdrKEKVgO/7CEn8RvgkdR3anjwU2SQ0qVAQmqYlhUy9AKo2ljwvwjBcyVoUgih7oSgyeJ6L2cwlGa26NpwJ4TwVknDO/6o6wpbJUGQ8ki18b4/NuuHBnrmgw9S9YDbteD4XnllLmyeYxokqW/5v2dfidGrRtmccDzXqqpTRU0TusixmU4FwdWYWd1BHUxjjuaxjdc6dKyyCHR2UXAwcj/jrUzFaihh1mkY8p70kTOIQ6nAIfaxRmcH4FM/2+xtutzvu9z8QwRfDVUSW8jyhxZKWO+ziDTR1DFO35DCutxv+57mxb7VaQRFTbZqa020o+YC6ruVVK/aP29AcDrR4/CcO7NvtdvgF2f16c8ofzlAAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;XAMPP Control Panel Interface&quot; title=&quot;XAMPP Control Panel Interface&quot; src=&quot;/static/1ad13129a4404c4a07fbcbd2eb82ff15/5a190/image-2.png&quot; srcSet=&quot;/static/1ad13129a4404c4a07fbcbd2eb82ff15/772e8/image-2.png 200w,/static/1ad13129a4404c4a07fbcbd2eb82ff15/e17e5/image-2.png 400w,/static/1ad13129a4404c4a07fbcbd2eb82ff15/5a190/image-2.png 800w,/static/1ad13129a4404c4a07fbcbd2eb82ff15/2bef9/image-2.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;The classic windows look we all know! 🤭&lt;/figcaption&gt;&lt;p&gt;To launch the server, &lt;strong&gt;simply click on&lt;/strong&gt; &lt;code&gt;Start&lt;/code&gt; for both the Apache and MySQL modules, as shown in the image above. If you now visit &lt;code&gt;localhost:80&lt;/code&gt;, you should be greeted by a XAMPP web page as well.&lt;/p&gt;&lt;p&gt;But wait! &lt;strong&gt;What if you want to change the port number&lt;/strong&gt; like on Mac? Well... on Windows it&amp;#x27;s not as simple as a button click (as usual). 🙄 For the purpose of not having the explanation take up half the article, a detailed step by step guide 📃 can be found &lt;a href=&quot;https://stackoverflow.com/questions/11294812/how-to-change-xampp-apache-server-port&quot; target=&quot;_blank&quot;&gt;on Stack Overflow&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;However, for the time being, I recommend you stick with the default port &lt;code&gt;80&lt;/code&gt;.&lt;/p&gt;&lt;h3 id=&quot;pitstop&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#pitstop&quot; aria-label=&quot;pitstop permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Pitstop&lt;/h3&gt;&lt;p&gt;Bonus tip (for Mac) 🤓 - &lt;strong&gt;Something already occupying your chosen port?&lt;/strong&gt; You can kill it by running &lt;code&gt;kill -9 &amp;lt;&amp;lt;PID number&amp;gt;&amp;gt;&lt;/code&gt; in your terminal. You can find out the PID number with &lt;code&gt;lsof -i @localhost:&amp;lt;&amp;lt;port number&amp;gt;&amp;gt;&lt;/code&gt;.&lt;/p&gt;&lt;h2 id=&quot;step-3-download-and-set-up-wordpress&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-3-download-and-set-up-wordpress&quot; aria-label=&quot;step 3 download and set up wordpress permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 3: Download and set up WordPress&lt;/h2&gt;&lt;p&gt;On to the next task! We now have to &lt;a href=&quot;https://wordpress.org/&quot; target=&quot;_blank&quot;&gt;download&lt;/a&gt; WordPress and set it up. For the Windows folks, make sure unzip the file after downloading.&lt;/p&gt;&lt;p&gt;The installation procedure isn&amp;#x27;t as straightforward since all you get is a bunch of files and folders. For this part, you want to return to your XAMPP application interface and &lt;strong&gt;click &amp;quot;Explore&amp;quot;&lt;/strong&gt; in the &lt;em&gt;Volumes&lt;/em&gt; tab. &lt;strong&gt;Windows users&lt;/strong&gt;, however, will want to simply click the &lt;strong&gt;&amp;quot;📂 Explorer&amp;quot;&lt;/strong&gt; button (no &lt;em&gt;Volumes&lt;/em&gt; tab needed).&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:514px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:89.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAYAAABb0P4QAAAACXBIWXMAABYlAAAWJQFJUiTwAAABuUlEQVQ4y+VUTW/TQBDNj+fElX/CESGhNolV2yVOaO2IxvbaXq+/Ns0FEBKCecysvFAQF0J74vA8O+udN+/Zq1lMwZsvSfYSV3lCWjWkKoWyLImBqqpQFAXCMEQUx5wr6KrwIKVKf9adZ3xe2GnCNI2MCcMwQGtN4zjO69btd10H02o0ncVOnfCuOuG2PmEYR5pc/U8sjscjSQGTkGlbStNUyMgYg/1+D2utIy7eZ4juLJ69+ojnrz/gxeUndlOjaRrq+95xCJcQIj8cKLwKsd1usdvtEEcxYrZ4HV87u5vNxuVpeou+M9BNI0TihpW3qCVnZ9LcKczzHJcXF7RcLh2pUgqyJ1G+oaCua7RcXPJas3qb36G/SVBsYtibhAzn9v7eKSQpiKMIEUMsi3zXnQuFSIj9XpZlOBQlBibQ27fIghVMEsOUOeyRCaeJvxF3lp8g6NhSM1vytgQ+lyaitJ73DTdq5D3DWZaHPyxqPMnD+Cdi/aCpf/8LoS84B/8nIc2E9JgK6WkV/iNI4g9CudC/T42/hJs6bjjIzZdELrXMvzNBc/y2SJLEjakgCLBarbBer88BzfHrd8lNF/b/7xRgAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Install Wordpress Using XAMPP: XAMMP Volumes Interface&quot; title=&quot;Install Wordpress Using XAMPP: XAMMP Volumes Interface&quot; src=&quot;/static/00cb851774ea2305d0437940e921ff20/dea13/image-3.png&quot; srcSet=&quot;/static/00cb851774ea2305d0437940e921ff20/772e8/image-3.png 200w,/static/00cb851774ea2305d0437940e921ff20/e17e5/image-3.png 400w,/static/00cb851774ea2305d0437940e921ff20/dea13/image-3.png 514w&quot; sizes=&quot;(max-width: 514px) 100vw, 514px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Time to explore the unknown... or not?! 👀&lt;/figcaption&gt;&lt;p&gt;You&amp;#x27;ll now find yourself in the XAMPP (or &lt;em&gt;Lampp&lt;/em&gt;) directory. From here, you want to open the &lt;code&gt;htdocs&lt;/code&gt; folder and &lt;strong&gt;create a new folder&lt;/strong&gt; in it named after your website. How about we name it &lt;code&gt;tutorial&lt;/code&gt;, shall we? And it is in this newly created folder where you want to &lt;strong&gt;copy all your WordPress files and folders&lt;/strong&gt; that you downloaded.&lt;/p&gt;&lt;p&gt;Here&amp;#x27;s what it should look like afterwards: 👇&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:320px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:204.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAApCAIAAAC6D54pAAAACXBIWXMAABYlAAAWJQFJUiTwAAAD9ElEQVRIx41Vy47jVBD1z4zEgh9gj5DYs+FnRkisWDFISCzYDQukEQsWgEADYsRj1Mykk+7Ejt/2tR07fsaOff2KnYTjeGjS03kdXUWK5LpVdercU8yU529Hw8D3sixLd0guBmOZBqcS4oaB7zuO43leHF8cnC3C22j1uxZyN8Pb8USW5fJiMBVN+aT9iyx829rusNlstpeBiQPvlVv8qoaqMBUEIVrETdPkeV5cAKZt23LVBnGCSNSsqqrn++vLwORZaqeV5MaKJGqEeK4bRVF9GTrCroPqBVnYREvSDA2j7EuDizTh4uZPEtmG3m426AL14Ir+9zSYdBHe+MUfeihOboejmziOq7IsissI6+8o63oRObqmiqKoKoqmqqZp8KJI8xwjOVYFgyRV1SVbRP50OuU4TlFVSZJ834dUaU7PjSqnQVYOo42mKFNuqiiKLCt1verJO4FdcFGE8fI1j3wSgnl+qshK07TNOTCdRqs6jcP54HknEVWzLMswDIjsvLah45JSe1n84m0Noodh1MsbHJ09TN6hyGjuR8ubMXv1eoiBoQBcnFOanwTz6LH63mfk/S+tj5/5Hz7RPvqKfPC5/uix8u6nxjufaKcP8/Tv8Nur8Nkg+mkafz9Kvntpff2z8MWP8pMfxKcvw29OHma7bd+cTbNe13jWROJcQwxmymbdQCD/f/DgME3b3h1KU9u2BRFjBut6mmVNu97/4K3D/KcWiLn0QpvoymBwzbJQGsvzfEbpeW33aNoNbJDoBIAxQDM8L1BK++Ed1HaBqUDGQRD8ZpesZimiwLKsIIgYBmQPneLpHjQmpjcUiLRpVla2Muw5LAUJNZ0YpmnNZo5t497+bb1tQ6s9FFU9n8+hEFEUJFmeTCZ4oegcLYRhiPyr+2CqPSRFhWTiDrvKO0vkdpiMx2ivuo99wta0WrmuS3TdIARsoWwZxqDhpSog8qErMHdCBQFpXlrILIiSJKLm0WikaXpnFf0wH2q73w/oJ02XL/yNYLkm3EjVMOm56552QqZ/mT318BNzNoORaN3D7szozHvuPQG10SwTk0afOYosGYYpip2x9E581Em6n7btg18FtWQ6uipLMpQiQGfdvl6mmMrh4Du59JXYsxm/AxjGtOCmYC5JlnetHdV2Vjeu66FhjBej1nXS03mUsL2L8oSWpmUJPD8ed9qaTNgoWhTH1y2zr1VaNxiPrmmIRHJCjDfqObZi97deWlQwA6wbBHerQ5ad+TxNMxB2eEvu/1kWNYI765/C+nnomWO5ZZo2x4LvlV2tPNfTdX237kCb4jjzdbvup32g7GwPUZZDWEgIqv65uhoMBsPrIXwcybND+BfXB+qx4dh7QgAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Install Wordpress Using XAMPP: XAMPP Wordpress Folder Structure&quot; title=&quot;Install Wordpress Using XAMPP: XAMPP Wordpress Folder Structure&quot; src=&quot;/static/9397fcd2d32b8e3e4abfdf4a616a509e/72799/image-4.png&quot; srcSet=&quot;/static/9397fcd2d32b8e3e4abfdf4a616a509e/772e8/image-4.png 200w,/static/9397fcd2d32b8e3e4abfdf4a616a509e/72799/image-4.png 320w&quot; sizes=&quot;(max-width: 320px) 100vw, 320px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Lo and behold!&lt;/figcaption&gt;&lt;h2 id=&quot;step-4-set-up-the-database&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-4-set-up-the-database&quot; aria-label=&quot;step 4 set up the database permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 4: Set up the database&lt;/h2&gt;&lt;p&gt;So far so good! Before we move on, we have to set up a mySQL database on which we would like to install our WordPress website. We can access the interface at &lt;code&gt;http://localhost:&amp;lt;&amp;lt;port&amp;gt;&amp;gt;/phpmyadmin/&lt;/code&gt;. We then &lt;strong&gt;create a new database&lt;/strong&gt; by &lt;strong&gt;clicking on &amp;quot;New&amp;quot;&lt;/strong&gt; in the left-hand column. &lt;strong&gt;Give your database a name and then click &amp;quot;Create&amp;quot;&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:51%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABdElEQVQoz52S63KCMBCFef8nq1W0VUemraNiuIQkikKQm+R0A9b2RzvtlJkzJ2Hhy+5mnUQe4a+fkcYeOI/gvawxX3pwZ094nLi9P4xdjKdTTFzSdIaxO2g0HuKjyRCbLxZwmLdAsN0izQoIqZDlOcqqwn8fx1utsCRyHEVQSuEYMhwChvKUoq5rNE3Tf9hcLigEh7le0RmDrutgSF/dyhFCQkqJXGso8tD3IeMYihQngtrAcTpnqAoNrQTobxg6wBD0OzmaQFY5lSopww2LsPYDhDFHwBg2tJ6t3nAphzZ0P4DuwCzLYGWBtuStzyizIWvf3yGIOF53DPWt9F97+BUoCJIIAXE4IuIJdnsGoY50URpnip/JM13cvawbVKTypooOtUBzAxoLZARa7yNEQiEhMFcHcHlATPuYpsDu7dq+SzONNC+sjPWTLoxjM/uApmnaN7xpr/+ZGGph95mh9TAMIajkllJv27Yfmb/IdPY6eqJ5Byrx/C6Wb+4FAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Install Wordpress Using XAMPP: PhpMyAdmin Interface&quot; title=&quot;Install Wordpress Using XAMPP: PhpMyAdmin Interface&quot; src=&quot;/static/5ed9fe8738aaaef1bdccb7b75acf6301/5a190/image-5.png&quot; srcSet=&quot;/static/5ed9fe8738aaaef1bdccb7b75acf6301/772e8/image-5.png 200w,/static/5ed9fe8738aaaef1bdccb7b75acf6301/e17e5/image-5.png 400w,/static/5ed9fe8738aaaef1bdccb7b75acf6301/5a190/image-5.png 800w,/static/5ed9fe8738aaaef1bdccb7b75acf6301/2bef9/image-5.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Here ya go!&lt;/figcaption&gt;&lt;p&gt;Best not to touch anything else or you might end up messing things up... 😅&lt;/p&gt;&lt;h2 id=&quot;step-5-install-wordpress&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#step-5-install-wordpress&quot; aria-label=&quot;step 5 install wordpress permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Step 5: Install WordPress&lt;/h2&gt;&lt;p&gt;The final step is upon us where we install WordPress on XAMPP! Now that we have a server up and running as well as a database set up for our website, we can finally start installing our WordPress site by &lt;strong&gt;accessing it on&lt;/strong&gt; &lt;code&gt;localhost:&amp;lt;&amp;lt;port&amp;gt;&amp;gt;/&amp;lt;&amp;lt;website name&amp;gt;&amp;gt;&lt;/code&gt; (in our case &lt;code&gt;localhost:8080/tutorial&lt;/code&gt;). You&amp;#x27;ll be greeted by a beautiful page to choose your language. 🔠&lt;/p&gt;&lt;p&gt;At one point, you&amp;#x27;ll be greeted by the following page:&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:59.00000000000001%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAA50lEQVQoz82SMW+DMBCF8///TueOWduhQ5ZUyVAJBowNNtg4wIvfRY6iSglEWXLS01mH7+Md3Kaua9yTUjVsY7DdHfH9+5fOWmqPejb/C1prGGNEWht41+Lja4/PnwOCs1LLz1cBm6aBsw7OuWuOvkfoe7SthbUXee/lrlJqGcjMBsJCCBhiTLAWZVnKBKwPwyC1RWAeZ5omaRrHEfM8IyYo64TlyC9fdKhTJqzrOskE8lwUhbikgg/rHPJSn74XHZ7iSTKDwPyHq6oSx6uAdEggRyXsVrdB16tGzqvzaNee2sNX9f7AM9GPkgHAdDKkAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Install Wordpress Using XAMPP: WordPress Setup Interface&quot; title=&quot;Install Wordpress Using XAMPP: WordPress Setup Interface&quot; src=&quot;/static/2ed3b279c664b94041e3c8766370e2b6/5a190/image-6.png&quot; srcSet=&quot;/static/2ed3b279c664b94041e3c8766370e2b6/772e8/image-6.png 200w,/static/2ed3b279c664b94041e3c8766370e2b6/e17e5/image-6.png 400w,/static/2ed3b279c664b94041e3c8766370e2b6/5a190/image-6.png 800w,/static/2ed3b279c664b94041e3c8766370e2b6/2bef9/image-6.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;The final boss...🤖&lt;/figcaption&gt;&lt;p&gt;Here you&amp;#x27;ll want to you &lt;strong&gt;enter the name of the database&lt;/strong&gt; you previously created &lt;strong&gt;as well as the admin&amp;#x27;s username and password&lt;/strong&gt;. By default, an admin with the username &lt;code&gt;root&lt;/code&gt; and no password exists. The rest you can safely ignore.&lt;/p&gt;&lt;p&gt;Bonus tip 🤓 - In case you run into a &amp;quot;&lt;strong&gt;Sorry, but I can’t write the wp-config.php file&lt;/strong&gt;&amp;quot; error, you want to have the &lt;code&gt;wp-config.php&lt;/code&gt; file placed in your website folder. In my situation, it would be &lt;code&gt;htdocs/tutorial&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Alright, you did it! Beyond this point it&amp;#x27;s smooth sailing. I hope you enjoyed this guide on how to install WordPress using XAMPP. Good luck on your WordPress adventures and beyond. 😄&lt;/p&gt;</content:encoded></item><item><title><![CDATA[An Introduction To Styled Components]]></title><description><![CDATA[There are a couple of ways to style your React components. You may either choose to do inline styling, import your CSS from an external…]]></description><link>https://dilshankelsen.com/an-introduction-to-styled-components/</link><guid isPermaLink="false">https://dilshankelsen.com/an-introduction-to-styled-components/</guid><pubDate>Sat, 28 Nov 2020 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;There are a couple of ways to style your React components. You may either choose to do inline styling, import your CSS from an external style sheet, or even include it in your JavaScript file. Particularly, in this article I will cover the last method with an introduction to the React library &lt;a href=&quot;https://styled-components.com/&quot; target=&quot;_blank&quot;&gt;Styled Components&lt;/a&gt;. 💅 Let&amp;#x27;s get styling, mi amigos!&lt;/p&gt;&lt;h2 id=&quot;why-use-styled-components&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#why-use-styled-components&quot; aria-label=&quot;why use styled components permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Why use Styled Components&lt;/h2&gt;&lt;p&gt;Every library has its purpose and so does this one: improving the developer experience of styling React components! That&amp;#x27;s... kind of vague, right? 😅 So let&amp;#x27;s dig a bit deeper. What are its benefits?&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Only relevant CSS is loaded&lt;/strong&gt; as the styling is bound to its own respective component. So for instance, if you have a web page that doesn&amp;#x27;t have any button (component), you will not find any CSS concerning buttons. Naturally, this translates into less code on your live site. As a counter example, imagine loading a single style sheet that contains all your CSS, even if the page you&amp;#x27;re on only uses a fraction of it.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;No more class name related bugs&lt;/strong&gt; such as duplication and misspelling. Instead, class names are generated for you! 😎&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Dynamic styling&lt;/strong&gt; based on &lt;code&gt;props&lt;/code&gt; is a simple, yet powerful option that gives way to many possibilities. Easily one of my favorite reasons to use this library!&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Easier maintenance&lt;/strong&gt; of your CSS code since you know exactly where the styles affecting your components are.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Automatic vendor prefixing&lt;/strong&gt;... booyah! 🤩&lt;/li&gt;&lt;/ul&gt;&lt;h2 id=&quot;the-basics&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-basics&quot; aria-label=&quot;the basics permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The Basics&lt;/h2&gt;&lt;p&gt;In order to use this library in your project, run either &lt;code&gt;npm install --save styled-components&lt;/code&gt; or &lt;code&gt;yarn add styled-components&lt;/code&gt;. Afterwards, import it into your pages with &lt;code&gt;import styled from &amp;#x27;styled-components&amp;#x27;&lt;/code&gt;... you know the drill. 😉&lt;/p&gt;&lt;p&gt;Alright, time to dig into the code by taking a look at an example:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;Title&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; styled&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;font-size&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css number&quot;&gt;1.5&lt;/span&gt;&lt;span class=&quot;token template-string css language-css unit&quot;&gt;em&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;text-align&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; center&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css color&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In this code snippet, we declare a variable &lt;code&gt;Title&lt;/code&gt; as an &lt;code&gt;H1&lt;/code&gt; tag with some CSS using the imported object &lt;code&gt;styled&lt;/code&gt; and its method &lt;code&gt;h1&lt;/code&gt;. There is an equivalent method for every HTML tag you can think of, e.g. &lt;code&gt;p&lt;/code&gt;, &lt;code&gt;button&lt;/code&gt;, &lt;code&gt;section&lt;/code&gt;, &lt;code&gt;div&lt;/code&gt;, etc. However, what follows afterwards with the back ticks &lt;code&gt;``&lt;/code&gt; may seem strange to some of you. 🤨 Many of you have probably seen it in the context of multi line strings. This feature is called template literals, or template strings, and was introduced with ES6.&lt;/p&gt;&lt;p&gt;Unfortunately, I won&amp;#x27;t be going into the details as its functionality merits a blog post of its own. However, for those who need to quench their undesirable thirst for JS knowledge, I recommend the blog post &lt;a href=&quot;https://mxstbr.blog/2016/11/styled-components-magic-explained/&quot; target=&quot;_blank&quot;&gt;The magic behind styled-components&lt;/a&gt; by Max Stoiber 😁. For the... uhm, laid back people of us, here&amp;#x27;s what you can take away: the string in back ticks is actually passed as an argument to the function &lt;code&gt;h1&lt;/code&gt;, which then parses its content and transforms it to pure CSS, including any JavaScript from interpolations (i.e. &lt;code&gt;${}&lt;/code&gt;).&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:558px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:71.50000000000001%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAOABQDASIAAhEBAxEB/8QAGAABAAMBAAAAAAAAAAAAAAAAAAIDBAX/xAAWAQEBAQAAAAAAAAAAAAAAAAABAAP/2gAMAwEAAhADEAAAAUbGVhdAn//EABoQAAICAwAAAAAAAAAAAAAAAAECABMDISL/2gAIAQEAAQUCxlAXK28zZlYZqUaf/8QAFREBAQAAAAAAAAAAAAAAAAAAEDH/2gAIAQMBAT8Bp//EABgRAAIDAAAAAAAAAAAAAAAAAAACARES/9oACAECAQE/AWWNFH//xAAYEAEBAQEBAAAAAAAAAAAAAAABABExAv/aAAgBAQAGPwI5uSppcvCytrf/xAAaEAEAAgMBAAAAAAAAAAAAAAABABEhMXFh/9oACAEBAAE/IWrPYXeZzrIqdjjDwFRUKxY2Wz//2gAMAwEAAgADAAAAENMf/8QAFxEBAQEBAAAAAAAAAAAAAAAAAREAIf/aAAgBAwEBPxAvGSM3/8QAFhEBAQEAAAAAAAAAAAAAAAAAABGR/9oACAECAQE/EMBD/8QAHBABAAICAwEAAAAAAAAAAAAAAREhADFBgZGh/9oACAEBAAE/EBVIj6APc3sAlhK47xwsmwOPuXYakC9Mw7yeJoE7qt84DTw//9k=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Spongebob meme about template literals used in Styled Components&quot; title=&quot;Spongebob meme about template literals used in Styled Components&quot; src=&quot;/static/65cef1cb7ee23d0d8e8c64b9d06210f7/bb41d/meme-template-literal.jpg&quot; srcSet=&quot;/static/65cef1cb7ee23d0d8e8c64b9d06210f7/e07e9/meme-template-literal.jpg 200w,/static/65cef1cb7ee23d0d8e8c64b9d06210f7/066f9/meme-template-literal.jpg 400w,/static/65cef1cb7ee23d0d8e8c64b9d06210f7/bb41d/meme-template-literal.jpg 558w&quot; sizes=&quot;(max-width: 558px) 100vw, 558px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;All hail the magic conch shell!&lt;/figcaption&gt;&lt;p&gt;Next, &lt;code&gt;Title&lt;/code&gt; can be used as any other React component you&amp;#x27;re used to.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Title&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      All Hail Chunk Bytes!&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Title&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;dynamic-styling&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#dynamic-styling&quot; aria-label=&quot;dynamic styling permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Dynamic Styling&lt;/h2&gt;&lt;p&gt;One powerful feature of Styled Components is its ability to allow for styling based on props you pass to it. Let&amp;#x27;s build on the previous example, shall we?&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Title&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;primary&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;  All Hail Chunk Bytes!&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Title&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Remember, passing the props &lt;code&gt;primary&lt;/code&gt; without a value, is the equivalent to &lt;code&gt;primary={true}&lt;/code&gt;. This props is then available in our template literal through interpolation.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;Title&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; styled&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;font-size&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css number&quot;&gt;1.5&lt;/span&gt;&lt;span class=&quot;token template-string css language-css unit&quot;&gt;em&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;text-align&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; center&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation parameter&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; props&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation property-access&quot;&gt;primary&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation string&quot;&gt;&amp;quot;red&amp;quot;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation string&quot;&gt;&amp;quot;black&amp;quot;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  // Or with &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;destructuring&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation parameter punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation parameter&quot;&gt; primary &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation parameter punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; primary &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation string&quot;&gt;&amp;quot;red&amp;quot;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation string&quot;&gt;&amp;quot;black&amp;quot;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;For the CSS property &lt;code&gt;color&lt;/code&gt;, we check if &lt;code&gt;primary&lt;/code&gt; exists and then set the respective color accordingly. We do this &lt;code&gt;IF&lt;/code&gt; statement with the help of a ternary operator. However, this isn&amp;#x27;t only restricted to one property. You may even conditionally render multiple CSS properties depending on the prop. 👆&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt;styled&lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; css &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;styled-components&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;Title&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; styled&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;font-size&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css number&quot;&gt;1.5&lt;/span&gt;&lt;span class=&quot;token template-string css language-css unit&quot;&gt;em&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;text-align&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; center&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css color&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation parameter punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation parameter&quot;&gt; primary &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation parameter punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; primary &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; css&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css interpolation template-string css language-css&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation template-string css language-css property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation template-string css language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation template-string css language-css color&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css interpolation template-string css language-css&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation template-string css language-css property&quot;&gt;text-align&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation template-string css language-css&quot;&gt; left&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css interpolation template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In the example above, we load two additional properties if &lt;code&gt;primary&lt;/code&gt; exists. However, to be able to use a template literal inside an interpolation, we need to import and use the function &lt;code&gt;css&lt;/code&gt;. [Insert template literal Inception joke here] 😆&lt;/p&gt;&lt;h2 id=&quot;reusing-styled-components&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#reusing-styled-components&quot; aria-label=&quot;reusing styled components permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Reusing styled components&lt;/h2&gt;&lt;p&gt;Don&amp;#x27;t Repeat Yourself - The philosophy all coders (hopefully) love to follow, and this isn&amp;#x27;t any different here! Styled Components allows us to reuse existing components and apply another layer of styling on top. Let&amp;#x27;s work with the most recent example, but implement the changes for a &lt;code&gt;primary&lt;/code&gt; title another way.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;PrimaryTitle&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;styled&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;Title&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css color&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;text-align&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; left&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Et voilá! 🥐 We created another component called &lt;code&gt;PrimaryTitle&lt;/code&gt; using &lt;code&gt;Title&lt;/code&gt; and its styling as the base. For this, we simply need to pass it as an argument to &lt;code&gt;styled&lt;/code&gt;.&lt;/p&gt;&lt;h2 id=&quot;good-to-know&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#good-to-know&quot; aria-label=&quot;good to know permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Good to know&lt;/h2&gt;&lt;p&gt;Before this tutorial comes to an end, there are a couple more things I&amp;#x27;d like to show you that will really supercharge your Styled Components in your future project.&lt;/p&gt;&lt;h3 id=&quot;pseudo-classes-elements-and-nesting&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#pseudo-classes-elements-and-nesting&quot; aria-label=&quot;pseudo classes elements and nesting permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Pseudo-classes, elements and nesting&lt;/h3&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;Title&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; styled&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;font-size&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css number&quot;&gt;1.5&lt;/span&gt;&lt;span class=&quot;token template-string css language-css unit&quot;&gt;em&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;text-align&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; center&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css color&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css selector&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css selector pseudo-class&quot;&gt;:hover&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css color&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can apply pseudo classes and elements, by taking advantage of the ampersand (&amp;amp;), much like in Sass. Even better, &lt;strong&gt;you can nest your CSS logic&lt;/strong&gt; just like in Sass. But don&amp;#x27;t got crazy! 😝&lt;/p&gt;&lt;p&gt;In case you&amp;#x27;re curious what other (S)CSS features Styled Components has to offer, I recommend checking out the &lt;a href=&quot;https://github.com/thysultan/stylis.js&quot; target=&quot;_blank&quot;&gt;preprocessor Stylis&lt;/a&gt;, which this library is based on.&lt;/p&gt;&lt;h3 id=&quot;substituting-the-tag&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#substituting-the-tag&quot; aria-label=&quot;substituting the tag permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Substituting the tag&lt;/h3&gt;&lt;p&gt;What if I want to make the H1 component from our examples an H2? Easy peasy! Just pass your desired HTML tag as a props with &lt;code&gt;as&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Title&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;primary&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;as&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;h2&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;  All Hail Chunk Bytes!&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Title&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;adding-a-theme&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#adding-a-theme&quot; aria-label=&quot;adding a theme permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Adding a theme&lt;/h3&gt;&lt;p&gt;No coding project is complete without its own theme. Keep it DRY, remember? Styled Components provides a wrapper component called &lt;code&gt;ThemeProvider&lt;/code&gt;. Via the &lt;a href=&quot;https://reactjs.org/docs/context.html&quot; target=&quot;_blank&quot;&gt;Context API&lt;/a&gt; from React, it propagates your theme through all its children 👶 in the application. The theme can then be accessed via props in the styled component.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt;styled&lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;ThemeProvider&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;styled-components&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;Title&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; styled&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;font-size&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css number&quot;&gt;1.5&lt;/span&gt;&lt;span class=&quot;token template-string css language-css unit&quot;&gt;em&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;text-align&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; center&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token template-string css language-css property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation parameter punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation parameter&quot;&gt; theme &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation parameter punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation&quot;&gt; theme&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation property-access&quot;&gt;colors&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation property-access&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;token template-string css language-css interpolation interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token template-string css language-css punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token template-string css language-css&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token template-string template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; theme &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;colors&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;blue&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;#4064d7&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;ThemeProvider&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;theme&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;theme&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Title&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      All Hail Chunk Bytes!&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Title&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;ThemeProvider&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Hopefully this introduction to Styled Components has shown you the power of writing CSS in JavaScript and its possibilities. Maybe after this, you&amp;#x27;ll even become addicted to this way of writing CSS. 😂&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:500px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:100%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGQABAAMBAQAAAAAAAAAAAAAAAAIDBQEE/8QAFwEAAwEAAAAAAAAAAAAAAAAAAAEDAv/aAAwDAQACEAMQAAAB0qOxxX1IHPPuGWAX/8QAGRAAAwEBAQAAAAAAAAAAAAAAAAIDARES/9oACAEBAAEFAnqpKusYw04s/Jz3ODp2qJ4RD//EABgRAAIDAAAAAAAAAAAAAAAAAAERAiAh/9oACAEDAQE/AY6Eqf/EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQIBAT8BH//EAB4QAAEEAQUAAAAAAAAAAAAAAAEAEBEhAhIxMkFx/9oACAEBAAY/Ao1QjLXv6oxBtuRVdt//xAAbEAACAwEBAQAAAAAAAAAAAAAAAREhMUFRYf/aAAgBAQABPyFN7njL9w0sXN82EkefDkWLj30mm6W8EKm7ayUO4viR/9oADAMBAAIAAwAAABCo4EP/xAAYEQEAAwEAAAAAAAAAAAAAAAABABARIf/aAAgBAwEBPxAajTHkFxr/xAAYEQACAwAAAAAAAAAAAAAAAAAAARBBcf/aAAgBAgEBPxB6IuP/xAAcEAEBAAIDAQEAAAAAAAAAAAABEQAxIUFhUbH/2gAIAQEAAT8QQcuT0NmChRmNltxHYx+4fC6gd22c7x6uLGP7jUajseMiIhBAF31gahRYKToZrHWQWwAcHUz/2Q==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Meme about loving Styled Components too much compared to regular CSS styling&quot; title=&quot;Meme about loving Styled Components too much compared to regular CSS styling&quot; src=&quot;/static/76ff801b4b92a42ab1e3b174bda812cc/41099/meme-styled-components.jpg&quot; srcSet=&quot;/static/76ff801b4b92a42ab1e3b174bda812cc/e07e9/meme-styled-components.jpg 200w,/static/76ff801b4b92a42ab1e3b174bda812cc/066f9/meme-styled-components.jpg 400w,/static/76ff801b4b92a42ab1e3b174bda812cc/41099/meme-styled-components.jpg 500w&quot; sizes=&quot;(max-width: 500px) 100vw, 500px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;There&amp;#x27;s no turning back 😛&lt;/figcaption&gt;</content:encoded></item><item><title><![CDATA[React Routing Without React Router]]></title><description><![CDATA[Keep things simple, stupid! The motto of every developer, I hope... This can also be applied to the use of external libraries, which offer a…]]></description><link>https://dilshankelsen.com/react-routing-without-react-router/</link><guid isPermaLink="false">https://dilshankelsen.com/react-routing-without-react-router/</guid><pubDate>Sat, 07 Nov 2020 00:00:00 GMT</pubDate><content:encoded>&lt;a href=&quot;&quot; target=&quot;_blank&quot;&gt;&lt;/a&gt;&lt;p&gt;Keep things simple, stupid! The motto of every developer, I hope... This can also be applied to the use of external libraries, which offer a plethora of awesome features and make our developer hearts go fuzzy. Yet, more often than not, we might not be able to take advantage of even a fraction of them. In that case, is it even worth including them at the expense of a more bloated web application? Well, not always... 😟 In this article, I&amp;#x27;d like to demonstrate how &lt;strong&gt;simple&lt;/strong&gt; React routing can be done in React using a custom solution instead of using an external library such as &lt;a href=&quot;/an-introduction-to-react-router/&quot;&gt;React Router&lt;/a&gt;.&lt;/p&gt;&lt;h2 id=&quot;a-word-of-caution&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#a-word-of-caution&quot; aria-label=&quot;a word of caution permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;A word of caution&lt;/h2&gt;&lt;p&gt;Hey there! Before we move on, did you see how I made the word &lt;em&gt;simple&lt;/em&gt; earlier 👆 &lt;strong&gt;bold&lt;/strong&gt;? Well, that&amp;#x27;s what this solution is intended for. If you&amp;#x27;ve only got a couple of pages you need routing for, then you&amp;#x27;ll surely benefit from a custom solution. But anything more complex than that? You&amp;#x27;ll probably want to start considering a more powerful tool 💪 like &lt;em&gt;React Router&lt;/em&gt; where you can take advantage of its powerful features. Capiche?!&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:475px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:100%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAIEAQP/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIQAxAAAAHf6RZmcCQkH//EABoQAAIDAQEAAAAAAAAAAAAAAAECAxESABP/2gAIAQEAAQUCWNc4XpkXSKfNVNSgWshC+7Drsf/EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQMBAT8BH//EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQIBAT8BH//EAB4QAAICAgIDAAAAAAAAAAAAAAABAjERIlGRITNh/9oACAEBAAY/AlpHo9cehaKuCOZSo8ykLaVci+JGMK2I/8QAHRABAAICAgMAAAAAAAAAAAAAAQARIWEQMVFxof/aAAgBAQABPyG9qFHekqov3WYYMFggTizx6iF0VO98CymKwD5CuKdkzCF1P//aAAwDAQACAAMAAAAQ4MhA/8QAFREBAQAAAAAAAAAAAAAAAAAAEAH/2gAIAQMBAT8QKf/EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQIBAT8QH//EAB0QAQABBQEBAQAAAAAAAAAAAAERACExUWFBgcH/2gAIAQEAAT8QTITsMpCZYz9rOAFwFzc/lPWTKgC8vK8Vhiwo3KuwcCNco0SDYqfXlJQKihxC3QkoTSbLvtLmwmt7qrOe1//Z&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Meme about people using React Router&quot; title=&quot;Meme about people using React Router&quot; src=&quot;/static/a8ed5c677e9f480d4e7137ad0b15e76e/55feb/meme-billy.jpg&quot; srcSet=&quot;/static/a8ed5c677e9f480d4e7137ad0b15e76e/e07e9/meme-billy.jpg 200w,/static/a8ed5c677e9f480d4e7137ad0b15e76e/066f9/meme-billy.jpg 400w,/static/a8ed5c677e9f480d4e7137ad0b15e76e/55feb/meme-billy.jpg 475w&quot; sizes=&quot;(max-width: 475px) 100vw, 475px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Billy, such a savage... 😏&lt;/figcaption&gt;&lt;h2 id=&quot;the-setup&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-setup&quot; aria-label=&quot;the setup permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The setup&lt;/h2&gt;&lt;p&gt;The example I will go through in this article can be accessed on my &lt;a href=&quot;https://github.com/dkelsen/simple-react-router&quot; target=&quot;_blank&quot;&gt;GitHub repository&lt;/a&gt;. The project was created from the &lt;code&gt;Create React App&lt;/code&gt; template and consists of three different pages: Home, About and Contact.&lt;/p&gt;&lt;h3 id=&quot;the-routing-logic&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-routing-logic&quot; aria-label=&quot;the routing logic permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The routing logic&lt;/h3&gt;&lt;p&gt;In &lt;code&gt;Routing.js&lt;/code&gt;, the main part of our routing logic can be seen. Take note ⚠️ that this custom router uses &lt;a href=&quot;https://reactjs.org/docs/hooks-intro.html&quot; target=&quot;_blank&quot;&gt;React hooks&lt;/a&gt; like &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useMemo&lt;/code&gt; as well as the &lt;a href=&quot;https://reactjs.org/docs/context.html&quot; target=&quot;_blank&quot;&gt;Context API&lt;/a&gt;. If you aren&amp;#x27;t familiar with them, I recommend you brush up your knowledge about them before tackling the code below.&lt;/p&gt;&lt;p&gt;Now, without getting too hung up on the details 🧐, what are the main takeaways?&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Routing.js&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; useState&lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; useMemo&lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; createContext &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;react&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;/* Simple object which we use to set and &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt; * identify the pages in our router &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt; */&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; pagesMapping &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;home&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;home&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;about&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;about&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;contact&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;contact&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;With the help of &lt;code&gt;useState&lt;/code&gt;, we keep track of the current page we need to route to. This is stored in the variable &lt;code&gt;page&lt;/code&gt;, which by default will be set to whatever is specified in the URL path. In addition to that, we have the function &lt;code&gt;setPage&lt;/code&gt; with which we can dynamically change the page, such as in a navigation bar.&lt;/p&gt;&lt;p&gt;Next ⏭, we store these the aforementioned variable and function within an Object Literal in a variable called &lt;code&gt;value&lt;/code&gt;. You will notice that we do so using the &lt;code&gt;useMemo&lt;/code&gt; hook. This makes our app a bit more efficient in reacting to any changes in our state.&lt;/p&gt;&lt;p&gt;Lastly, we want to make our state &lt;code&gt;value&lt;/code&gt; (i.e. &lt;code&gt;page&lt;/code&gt; and &lt;code&gt;setPage&lt;/code&gt;) accessible to our entire application. We do so using the Context API. When we create the context &lt;code&gt;RoutingContext&lt;/code&gt;, we &lt;em&gt;can&lt;/em&gt; pass a default value, though you may leave it blank if you chose to do so (unless you use TypeScript! 😆).&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Routing.js&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;RoutingContext&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;createContext&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; pagesMapping&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;home&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function maybe-class-name&quot;&gt;Router&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; children &lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;/* Read the urlPath, e.g. &amp;#x27;/about&amp;#x27; or &amp;#x27;/&amp;#x27; */&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; urlPath &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;location&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;pathname&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;toLowerCase&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;/* Set the default page to Home if not specified otherwise in the URL */&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; setPage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;urlPath &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; pagesMapping&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;home&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; value &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useMemo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; page&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; setPage &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; setPage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;RoutingContext.Provider&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;children&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;RoutingContext.Provider&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Our component &lt;code&gt;Router&lt;/code&gt; will return this context, which will be made accessible to all its children through the props &lt;code&gt;value&lt;/code&gt;. And how do we ensure it&amp;#x27;s truly accessible to all components in the application? We insert it at the very root of the app, of course! 😋 This can be done in the &lt;code&gt;index.js&lt;/code&gt; file as seen below.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// index.js&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;Router&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;./context/Routing&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;ReactDOM&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;React.StrictMode&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Router&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Router&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;React.StrictMode&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;root&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;All clear so far?&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:425px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:142.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAdABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAIEAwH/xAAVAQEBAAAAAAAAAAAAAAAAAAADAv/aAAwDAQACEAMQAAABmrm3uOiCnjSugrwUSP/EABsQAAICAwEAAAAAAAAAAAAAAAECABEDEiEi/9oACAEBAAEFAsS8UKQRE1niXMYGiss5FoGxGNH/xAAXEQEBAQEAAAAAAAAAAAAAAAAAERIT/9oACAEDAQE/AYyjm//EABcRAQEBAQAAAAAAAAAAAAAAAAAREhP/2gAIAQIBAT8BrSuj/8QAHBAAAgICAwAAAAAAAAAAAAAAACEBEBIxEYHB/9oACAEBAAY/AsiGI30eiMuSEKtV/8QAHRABAAICAwEBAAAAAAAAAAAAAQARMUEhUZFh0f/aAAgBAQABPyG18esxIa9wR5WR4tz4ZWA8hoxrhcMlR6rUTVeNbiJaUdQ0AzUQ08PycAX7P//aAAwDAQACAAMAAAAQvOkC/8QAGhEBAAMAAwAAAAAAAAAAAAAAAQARMSFBYf/aAAgBAwEBPxBFt9xd6R37FXwz/8QAGBEBAAMBAAAAAAAAAAAAAAAAAQARMSH/2gAIAQIBAT8QsgF8YGsZVTmQFZP/xAAgEAEBAAEEAgMBAAAAAAAAAAABEQAhMVFxQWGBkaHB/9oACAEBAAE/EFUZGBFNNWYpWl2WHAYlOHUj9YZLgTad0zSI0SmPWXhBvYmWlrBL9d5o5BUQbucamV0TZ8422pNj59nvGBCURQMAQxPNfzP/2Q==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Meme about explaining React routing solution without React Router&quot; title=&quot;Meme about explaining React routing solution without React Router&quot; src=&quot;/static/a7442f729f6a6e1f7c612181c3aae4a6/b9721/meme-explanation.jpg&quot; srcSet=&quot;/static/a7442f729f6a6e1f7c612181c3aae4a6/e07e9/meme-explanation.jpg 200w,/static/a7442f729f6a6e1f7c612181c3aae4a6/066f9/meme-explanation.jpg 400w,/static/a7442f729f6a6e1f7c612181c3aae4a6/b9721/meme-explanation.jpg 425w&quot; sizes=&quot;(max-width: 425px) 100vw, 425px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Seriously though, leave any questions in the comment section 😊&lt;/figcaption&gt;&lt;h3 id=&quot;displaying-the-right-pages&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#displaying-the-right-pages&quot; aria-label=&quot;displaying the right pages permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Displaying the right pages&lt;/h3&gt;&lt;p&gt;Alrighty then, we&amp;#x27;ve go the routing logic set up. Now, what about showing the right pages depending on what the router wants? Easy peasy! For this we shall take a look at &lt;code&gt;App.js&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;To gain access to our &lt;code&gt;RoutingContext&lt;/code&gt; we created earlier, we need to use &lt;code&gt;useContext&lt;/code&gt;. From it, we can can destructure the variable &lt;code&gt;page&lt;/code&gt;. Just to clarify, this is the same variable we passed into &lt;code&gt;value&lt;/code&gt; along with the function &lt;code&gt;setPage&lt;/code&gt;. Equipped with our omniscient power on the state of our app, we can then conditionally render relevant components, i.e. pages, depending on the value of &lt;code&gt;page&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// App.js&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; useContext &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;react&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;Home&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;./pages/Home&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;About&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;./pages/About&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;Contact&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;./pages/Contact&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; pagesMapping&lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;RoutingContext&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;./context/Routing&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;./App.css&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function maybe-class-name&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; page &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useContext&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;RoutingContext&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;pagesMapping&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;home&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; page&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Home&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;pagesMapping&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;about&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; page&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;About&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;pagesMapping&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;contact&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; page&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Contact&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;App&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;That&amp;#x27;s all, really! Not much else to explain here. 😛&lt;/p&gt;&lt;h3 id=&quot;manually-changing-the-page&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#manually-changing-the-page&quot; aria-label=&quot;manually changing the page permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Manually changing the page&lt;/h3&gt;&lt;p&gt;How do we go about changing the page manually, say by clicking on an element? To explain this, let&amp;#x27;s take a look &lt;code&gt;Home.js&lt;/code&gt; as an example. Just as in the previous section, we need to gain access to our all mighty Context API with &lt;code&gt;useContext&lt;/code&gt;. Instead of retrieving &lt;code&gt;page&lt;/code&gt; like last time, we retrieve the function &lt;code&gt;setPage&lt;/code&gt; instead.&lt;/p&gt;&lt;p&gt;The rest of the code should be quite familiar for you. We simply invoke the function with &lt;code&gt;onClick&lt;/code&gt; and set it to the desired page using a property from &lt;code&gt;pagesMapping&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Home.js&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; useContext &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;react&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;RoutingContext&lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; pagesMapping &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;../context/Routing&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt;logo&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;../logo.svg&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function maybe-class-name&quot;&gt;Home&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; setPage &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;useContext&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;RoutingContext&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;App-header&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;img&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;logo&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;App-logo&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;alt&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;logo&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;          Welcome to the &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Home&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt; page.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;App-link&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;onClick&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript function&quot;&gt;setPage&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt;pagesMapping&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript property-access&quot;&gt;about&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;          About&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;just-the-beginning&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#just-the-beginning&quot; aria-label=&quot;just the beginning permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Just the beginning&lt;/h2&gt;&lt;p&gt;Yay! You&amp;#x27;ve made it to the end of this article. 😁 Once you&amp;#x27;ve understood the routing setup in &lt;code&gt;Routing.js&lt;/code&gt;, the rest shouldn&amp;#x27;t be much of a challenge to understand. This custom example is by no means a complete solution. For example, I&amp;#x27;ve not included a 404 page for an invalid URL path. Neither does the URL change when switching between pages. These are all things that can be included if you&amp;#x27;d like to.&lt;/p&gt;&lt;p&gt;I hope with these basic building blocks you can do React routing without a library like React Router when the situation arises. 😉&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Install PHPUnit for WordPress on Windows]]></title><description><![CDATA[Unit testing is an indispensable tool for any developer, regardless of the language they use. The same goes for WordPress plugin development…]]></description><link>https://dilshankelsen.com/how-to-install-phpunit-for-wordpress-on-windows/</link><guid isPermaLink="false">https://dilshankelsen.com/how-to-install-phpunit-for-wordpress-on-windows/</guid><pubDate>Tue, 12 Nov 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Unit testing is an indispensable tool for any developer, regardless of the language they use. The same goes for WordPress plugin development. However, I&amp;#x27;m not here to regurgitate its importance, benefits and bla bla bla... I would like to talk to you about a more pressing issue instead: getting the damn thing set up on Windows! The fact that Windows isn&amp;#x27;t a Unix based system can sometimes make even the simplest of tasks a nightmare. Likewise, attempting to install PHPUnit for WordPress on Windows was no exception. Fortunately for you, I&amp;#x27;m about to share my super-duper installation guide, so you don&amp;#x27;t have to suffer through the same thing as I did. 🤪&lt;/p&gt;&lt;p&gt;Please note that this installation was performed on Windows 10. You may try it out on another version, but I can&amp;#x27;t guarantee it will work.&lt;/p&gt;&lt;h2 id=&quot;requirements&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#requirements&quot; aria-label=&quot;requirements permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Requirements&lt;/h2&gt;&lt;p&gt;No one should come to a battle unprepared and you shouldn&amp;#x27;t either. In order to pull this whole thing off, &lt;strong&gt;you will need the following tools:&lt;/strong&gt; 🔨&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Composer&lt;/li&gt;&lt;li&gt;Apache Subversion&lt;/li&gt;&lt;li&gt;WP-CLI&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;I&amp;#x27;m going to assume you already have Composer installed, as any good PHP developer should have. If not... 🤨 you better &lt;a href=&quot;https://getcomposer.org/doc/00-intro.md&quot; target=&quot;_blank&quot;&gt;download it&lt;/a&gt; before I whoop your ass. As for the other two, let me show you how it&amp;#x27;s done!&lt;/p&gt;&lt;h3 id=&quot;installing-apache-subversion&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#installing-apache-subversion&quot; aria-label=&quot;installing apache subversion permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Installing Apache Subversion&lt;/h3&gt;&lt;p&gt;&lt;strong&gt;WordPress uses a source version control system called Apache Subversion&lt;/strong&gt; and you&amp;#x27;re going to need it to set up the test environment of your plugin. If you&amp;#x27;ve already got it installed, color me impressed. You&amp;#x27;re a real tech wizard 🧙‍♂️, bruh! If not, follow these simple steps.&lt;/p&gt;&lt;p&gt;First off, &lt;a href=&quot;https://www.visualsvn.com/downloads/&quot; target=&quot;_blank&quot;&gt;download Apache Subversion command line tools&lt;/a&gt;. Next, extract the contents of the zip file into a folder called &lt;code&gt;apache-svn&lt;/code&gt; and place it into the directory &lt;code&gt;C:\Program Files&lt;/code&gt;. Now, open your control panel and head over to &lt;code&gt;System and Security\System&lt;/code&gt; and &lt;strong&gt;click on &amp;quot;Advanced system settings&amp;quot;&lt;/strong&gt; on the left-hand side. You should be greeted by the following window:&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:543px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:105%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAVCAIAAADJt1n/AAAACXBIWXMAABJ0AAASdAHeZh94AAACKklEQVQ4y41UiXKiQBTk//9NETEVddflnnu4RDkUkh7QqButpMsahzfzju73wFJKcSG01pSSoig/fofz+VzXtSWFjKMoiZM4TgQXlFCldJ7laZrmec4YE5xjlUJIKbEpy7Kqqv0IC5nhSWJCCdNKR2FonPMCQYuiIAnsFCunjHOeJMm+qprawDh3XYe/FB6onDH8IS2ekIomBPlTbX5ZqjkXWZ4j035fofKmaay2bVFW4PmE0KZpT1cg6OkRsHStuQDCwzAYzngoUR44RzE4cwb5BKMMpTLGsYEF1UpzBF6mRIRFZuOMzD9qO4yrqrrFNlxsglWgju25ncoej82F4QX6wZzKsnH/hM7ae/PFxRk80BJuSuSg0Pc9KPXPMAzDrZYvzlqrIAjAGV1BCGieao32pmn24PDI5ca5H5Mh59loeQEs97chNtqppCryAkcvBftOG8bD8bhZr1fuWxRFqHfsc9dm0ySgVq2LIp/offncZLvSniS4cKaU7v7ufM+PwigMQgwW5hTlwU4IQfMZoVKqe/43zhNbTM6pu05SN41TezgcsI5PRgyUOtkvav9mSCYg0Mp1Z7O55/lI0Yx97iY+zzFqjjvji1QfRyDKRTAcvJzKq2Z4be25vXSWS8e1Z3PbXkCRH5zvo/y3AcDXwpdh/w0wIhukxqcD/cM3ZNqjCdhgHMMwRC+s7BnggNdwu93udv9wG1dX7ur9fR3H8WazsRe24zi+738C46CxZjUXh9wAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Screenshot of system properties window&quot; title=&quot;Screenshot of system properties window&quot; src=&quot;/static/e22dacb6ee020f72180af6a9b68a9102/29579/image-1.png&quot; srcSet=&quot;/static/e22dacb6ee020f72180af6a9b68a9102/772e8/image-1.png 200w,/static/e22dacb6ee020f72180af6a9b68a9102/e17e5/image-1.png 400w,/static/e22dacb6ee020f72180af6a9b68a9102/29579/image-1.png 543w&quot; sizes=&quot;(max-width: 543px) 100vw, 543px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;The first step of many...&lt;/figcaption&gt;&lt;p&gt;In the &amp;quot;Advanced&amp;quot; tab, &lt;strong&gt;click on &amp;quot;Environment Variables&lt;/strong&gt;&amp;quot;. This will prompt you another window where you will &lt;strong&gt;select &amp;quot;Path&amp;quot; in the &amp;quot;System variables&amp;quot; section and click &amp;quot;Edit...&amp;quot;.&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:703px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:110.00000000000001%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAWCAIAAABPIytRAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABxElEQVQ4y8VUTW/TQBTcHwxHLhwQFZf+kd7hwAEa+AOlSE3iuqGy08T2ftrrraF2ktprZu0kakCBRBwYjaLR2zf7LL+JiTFGZ5nOc6nkQ1m2rW0OQ1VVJMsySmmSJLSD1rjnIGAqUUolazgzSvcHAE3wE3Rba9sjAQuuIJhf143d4Ghz0/zD5P9hLgqY87Isqz3oj8pd9JX1296uTv+CPI+iaDgaB0EA4XleEIYQo9HI9/00VS4kUkrVwfwGnAohcNS3pWmKO7uKdJM550EQ3jlM9yQJeTB9pDph+goebCck9gAg1eiEmVLm3vZy1SxrC656NmC7l7WFW0gVzSNicmOPW5Vr/l7cM8aISPXpZ/nsLX/+zhHixXvx5pN6fe746qM8GXR6oE7OOw7Uyw/y7EtaGE0ybYasuogXW14miyu2GHL3e0U7so3uxFe6uBU/tM5cSJpm2barHdonbOu2fey4FbYqH7A/Z9YuHk+Yb2hMHMfetR9Op1GcXPsQdxBjz5tMvmHnpF/gPqADXwmkAiHhfJ0WITgqSAsxf0NRFPgD3czE+HY2DmlMGdI6j+brbP8Z/aMFsboJoslMMC4QxjhJMPknpmPai9goGjcAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Screenshot of environment variables window&quot; title=&quot;Screenshot of environment variables window&quot; src=&quot;/static/feed386b49a9220e120e6894fa23b8c8/242e2/image-2.png&quot; srcSet=&quot;/static/feed386b49a9220e120e6894fa23b8c8/772e8/image-2.png 200w,/static/feed386b49a9220e120e6894fa23b8c8/e17e5/image-2.png 400w,/static/feed386b49a9220e120e6894fa23b8c8/242e2/image-2.png 703w&quot; sizes=&quot;(max-width: 703px) 100vw, 703px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Some privacy please 😆&lt;/figcaption&gt;&lt;p&gt;Almost there, buddy! Hang on... In the &amp;quot;Edit environment variable&amp;quot; window, &lt;strong&gt;create a new variable&lt;/strong&gt; by clicking on &amp;quot;New&amp;quot;. Then give it the following name: &lt;code&gt;C:\Program Files\apache-svn\bin&lt;/code&gt;. There you go! Mission complete.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:375px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:134.50000000000003%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAbABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAwAEAv/EABYBAQEBAAAAAAAAAAAAAAAAAAIEAP/aAAwDAQACEAMQAAAB1oXOOuKnRH2FKaxwH//EABoQAAIDAQEAAAAAAAAAAAAAAAABAhIxIRH/2gAIAQEAAQUCwiaXTV1F3kPpNdvE9Hh//8QAFxEBAAMAAAAAAAAAAAAAAAAAAAMQEf/aAAgBAwEBPwGpGv/EABcRAQEBAQAAAAAAAAAAAAAAAAEAERP/2gAIAQIBAT8BdSyLmX//xAAcEAACAQUBAAAAAAAAAAAAAAAAARAgMTJBUWH/2gAIAQEABj8CXp0wFG4Zd0f/xAAdEAACAgIDAQAAAAAAAAAAAAABEQAhMUFRcZEQ/9oACAEBAAE/ISRBkSqanJPIUh44geAkHqNsHxNK2hAZB1Gao1F3Dt8//9oADAMBAAIAAwAAABCcM07/xAAXEQADAQAAAAAAAAAAAAAAAAAAAREh/9oACAEDAQE/EMTKJtLP/8QAGhEBAQACAwAAAAAAAAAAAAAAAQARIUFhkf/aAAgBAgEBPxAoCzOT1trub//EABsQAQEAAwEBAQAAAAAAAAAAAAERACFBMXEQ/9oACAEBAAE/EIFS1DusRrYj172YS1U1uMClBsqN5oF0p6yEgprQw0BVNPuHMcFQEk59wEK55PMTnoaMxFV238//2Q==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Running away balloon meme&quot; title=&quot;Running away balloon meme&quot; src=&quot;/static/3bbefaa64b5d3aafc1fbcf716b8b25d9/bf173/meme-steps.jpg&quot; srcSet=&quot;/static/3bbefaa64b5d3aafc1fbcf716b8b25d9/e07e9/meme-steps.jpg 200w,/static/3bbefaa64b5d3aafc1fbcf716b8b25d9/bf173/meme-steps.jpg 375w&quot; sizes=&quot;(max-width: 375px) 100vw, 375px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Step #1 completed!&lt;/figcaption&gt;&lt;h3 id=&quot;installing-wp-cli&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#installing-wp-cli&quot; aria-label=&quot;installing wp cli permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Installing WP-CLI&lt;/h3&gt;&lt;p&gt;Looking at the subheading, you&amp;#x27;d think this step is simple, right? Well, whatever you&amp;#x27;re thinking, wrong! Hint: it&amp;#x27;s not... 😬 &lt;strong&gt;If you&amp;#x27;re using a server setup like XAMPP, WAMP&lt;/strong&gt; or whatnot, I recommend checking out this &lt;a href=&quot;https://deluxeblogtips.com/install-wp-cli-windows/&quot; target=&quot;_blank&quot;&gt;awesome blog post by Anh Tran&lt;/a&gt;. He&amp;#x27;ll guide you through getting WP-CLI installed on your Windows machine.&lt;/p&gt;&lt;p&gt;On the other hand, &lt;strong&gt;if you&amp;#x27;re a Laragon user like me&lt;/strong&gt;, there&amp;#x27;s a &lt;a href=&quot;https://forum.laragon.org/topic/1039/tutorial-how-to-add-wp-cli-to-laragon&quot; target=&quot;_blank&quot;&gt;super brief tutorial on the Laragon forum&lt;/a&gt; on how to install it.&lt;/p&gt;&lt;h2 id=&quot;setting-up-plugin-unit-tests&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#setting-up-plugin-unit-tests&quot; aria-label=&quot;setting up plugin unit tests permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Setting up plugin unit tests&lt;/h2&gt;&lt;p&gt;At the time of this writing, WordPress is &lt;a href=&quot;https://make.wordpress.org/core/handbook/testing/automated-testing/phpunit/#setup&quot; target=&quot;_blank&quot;&gt;not yet compatible with PHPUnit&amp;#x27;s latest version 8.x&lt;/a&gt;, but &lt;strong&gt;version 7.x&lt;/strong&gt; instead. So don&amp;#x27;t go downloading version 8, only to complain that nothing works (like I did), ya hear me? ಠ_ಠ&lt;/p&gt;&lt;h3 id=&quot;installing-phpunit&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#installing-phpunit&quot; aria-label=&quot;installing phpunit permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Installing PHPUnit&lt;/h3&gt;&lt;p&gt;To install PHPUnit, &lt;code&gt;cd&lt;/code&gt; to the root of your plugin directory and &lt;strong&gt;run the following command:&lt;/strong&gt;&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;composer&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; require --dev phpunit/phpunit:7.*&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This will install the latest version of the 7.x release tree. 🌲 Then &lt;strong&gt;verify it&amp;#x27;s properly installed&lt;/strong&gt; with the command&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ ./vendor/bin/phpunit --version&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Please note that it&amp;#x27;s &lt;a href=&quot;https://phpunit.readthedocs.io/en/8.4/installation.html#global-installation&quot; target=&quot;_blank&quot;&gt;not considered good practice to install PHPUnit globally&lt;/a&gt;. Instead, PHPUnit &lt;em&gt;should&lt;/em&gt; be managed as a project-local dependency. Although, there&amp;#x27;s nothing stopping you from you doing otherwise... 😐&lt;/p&gt;&lt;h3 id=&quot;creating-the-plugin-test-files&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#creating-the-plugin-test-files&quot; aria-label=&quot;creating the plugin test files permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Creating the plugin test files&lt;/h3&gt;&lt;p&gt;Alright, still in your plugin directory? Great! This time we&amp;#x27;re going to &lt;strong&gt;use WP-CLI to create the plugin test files.&lt;/strong&gt;&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ wp scaffold plugin-tests &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;insert-plugin-name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;By default, &lt;a href=&quot;https://developer.wordpress.org/cli/commands/scaffold/plugin-tests/&quot; target=&quot;_blank&quot;&gt;the following files are generated&lt;/a&gt;: 📁&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;phpunit.xml.dist&lt;/code&gt; - the configuration file for PHPUnit&lt;/li&gt;&lt;li&gt;&lt;code&gt;.travis.yml&lt;/code&gt; - the configuration file for Travis CI&lt;/li&gt;&lt;li&gt;&lt;code&gt;bin/install-wp-tests.sh&lt;/code&gt; - configures the WordPress test suite and a test database&lt;/li&gt;&lt;li&gt;&lt;code&gt;tests/bootstrap.php&lt;/code&gt; - the file that makes the current plugin active when running the test suite&lt;/li&gt;&lt;li&gt;&lt;code&gt;tests/test-sample.php&lt;/code&gt; - a sample file containing the actual tests&lt;/li&gt;&lt;li&gt;&lt;code&gt;.phpcs.xml.dist&lt;/code&gt; - a collection of PHP_CodeSniffer rules&lt;/li&gt;&lt;/ul&gt;&lt;h3 id=&quot;creating-a-test-database&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#creating-a-test-database&quot; aria-label=&quot;creating a test database permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Creating a test database&lt;/h3&gt;&lt;p&gt;Next, we are going to use the previously generated &lt;code&gt;bin/install-wp-tests.sh&lt;/code&gt; file to create a test database.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ bin/install-wp-tests.sh wordpress_db_test root &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; localhost latest&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here&amp;#x27;s what you need to know about the command above. To start off, &lt;code&gt;wordpress_db_test&lt;/code&gt; will be &lt;strong&gt;the name of your test database.&lt;/strong&gt; You may name it however you like... well, except after the names that already exist. 😆 Furthermore, and in most cases, &lt;strong&gt;the default database user&lt;/strong&gt; tends to be called &lt;code&gt;root&lt;/code&gt; and has no password, hence &lt;code&gt;root &amp;#x27;&amp;#x27;&lt;/code&gt;. You may need to change this if it isn&amp;#x27;t the case for you.&lt;/p&gt;&lt;p&gt;Now... &lt;strong&gt;if everything went well&lt;/strong&gt;, run &lt;code&gt;./vendor/bin/phpunit&lt;/code&gt; in your plugin directory. This will execute any PHPUnit tests.&lt;/p&gt;&lt;h2 id=&quot;the-problems&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-problems&quot; aria-label=&quot;the problems permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The problems&lt;/h2&gt;&lt;p&gt;Chances are, in the previous section, everything didn&amp;#x27;t go as smoothly as you anticipated... But that&amp;#x27;s OK! I got you covered, mi hombre. 😏&lt;/p&gt;&lt;h3 id=&quot;oh-laragon-what-are-thou-doing&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#oh-laragon-what-are-thou-doing&quot; aria-label=&quot;oh laragon what are thou doing permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Oh Laragon, what are thou doing?&lt;/h3&gt;&lt;p&gt;If you&amp;#x27;re a Laragon user, you&amp;#x27;ll want to pay close attention to this section. If not... wait! You may find a golden nugget or two to solve your problems. 😀&lt;/p&gt;&lt;p&gt;Whenever I attempted to create the test database through the Cmder console emulator, &lt;strong&gt;Laragon would use Git Bash in the background&lt;/strong&gt; to execute the &lt;code&gt;install-wp-tests.sh&lt;/code&gt; file. As a result, the execution would fail because Git Bash couldn&amp;#x27;t recognize the command &lt;code&gt;mysqladmin&lt;/code&gt;. Solution? &lt;strong&gt;Add it to the PATH environment variable&lt;/strong&gt;s. ➕&lt;/p&gt;&lt;p&gt;Remember when you added Apache Subversion to the PATH environment variables? 🤓 You&amp;#x27;re going to want to do the same, but specify &lt;code&gt;C:\laragon\bin\mysql\mysql-5.7.24-winx64\bin&lt;/code&gt; as the path instead (or wherever your &lt;code&gt;mysqladmin.exe&lt;/code&gt; executable is located).&lt;/p&gt;&lt;p&gt;Furthermore, &lt;strong&gt;I recommend that you use Git Bash&lt;/strong&gt; instead of Cmder to create the test database, as the latter still appears to not execute it properly. Just remember to restart Git Bash after adding a new environment variable. And while we&amp;#x27;re at it, before rerunning the command for the test database, make sure to delete the folders &lt;code&gt;wordpress&lt;/code&gt; and &lt;code&gt;wordpress-test-lib&lt;/code&gt; in &lt;code&gt;C:\Users\&amp;lt;username&amp;gt;\AppData\Local\Temp&lt;/code&gt;. Drop the test database as well if it was created.&lt;/p&gt;&lt;h3 id=&quot;require_once-errors&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#require_once-errors&quot; aria-label=&quot;require_once errors permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;require_once(...) errors&lt;/h3&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-text&quot;&gt;TEXT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-text&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Warning: require_once(/tmp/wordpress//wp-includes/class-phpmailer.php): failed to open stream: No such file or directory in C:\Users\dilshan\AppData\Local\Temp\wordpress-tests-lib\includes\mock-mailer.php on line 2&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;Fatal error: require_once(): Failed opening required &amp;#x27;/tmp/wordpress//wp-includes/class-phpmailer.php&amp;#x27; (include_path=&amp;#x27;.;C:/laragon/etc/php/pear&amp;#x27;) in C:\Users\dilshan\AppData\Local\Temp\wordpress-tests-lib\includes\mock-mailer.php on line 2&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you&amp;#x27;ve got an error ❌ similar to that above, you can fix it by doing the following change. In &lt;code&gt;C:\Users\&amp;lt;username&amp;gt;\AppData\Local\Temp\wordpress-tests-lib\wp-tests-config.php&lt;/code&gt;, on line 7, replace the relative path of &lt;code&gt;/tmp/wordpress/&lt;/code&gt; with an absolute path: &lt;code&gt;C:/Users/&amp;lt;username&amp;gt;/AppData/Local/Temp/wordpress/&lt;/code&gt;. After making the change, it should look like below.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-php&quot;&gt;PHP&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-php&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;define( &amp;#x27;ABSPATH&amp;#x27;, &amp;#x27;C:/Users/dilshan/AppData/Local/Temp/wordpress/&amp;#x27; );&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Run &lt;code&gt;./vendor/bin/phpunit&lt;/code&gt; and... it wor- oh no! A warning. 😨&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:719px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:62.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAABJ0AAASdAHeZh94AAABg0lEQVQoz4WS3XKDIBCFfZk2PwqigIgoKGjS5oVykbbP3gO0mbYXzcwOc2Dc3W/PWvjVbadltNrNg7Wa1oeK7Cq6I3RPyD7qeI2CkPT4I4rFT4Pp9CCUamXXsKaUHYM2RupBCllzQYVk0NpI1XPVt70WnWqhi9mPl8tp3ZbT2a/bPAHBGe/ttkUilE5Q5vWyhdWlNrJpq5aTmpXFelrCahc/4jy/hG2bQ3A+TD5Y1AppqHkxSnOkmVGZhIlAiQLdgYpAMWCDEJxSNThbQVpBuag7Fd9rdjyWT2X1XFY7nHFmIAF1ctq6Aa3ACdvQwTkNnHFSSI4u9Jxz+tcwjDEYjEHQueWUNVXDs44BNnyEFSDQOUUJ/ZUM68apRxochk7wDGYmq1l6oVpjyGw1Rych6iqtrcCnGDXvIG0LV6Y1R1reOaHf207xCzvz3M873p3tnygIPdCoDlnknFzicTLGiwPH/waoHD7jr8Jgfc8fJ3982Ld3e7tN1+sku2NF0Hb/MC3HJzbVnNtZ9L9UAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Screenshot of executed PHPUnit tests&quot; title=&quot;Screenshot of executed PHPUnit tests&quot; src=&quot;/static/baf7430cda118c571db601cf8118b1c3/073e9/image-3.png&quot; srcSet=&quot;/static/baf7430cda118c571db601cf8118b1c3/772e8/image-3.png 200w,/static/baf7430cda118c571db601cf8118b1c3/e17e5/image-3.png 400w,/static/baf7430cda118c571db601cf8118b1c3/073e9/image-3.png 719w&quot; sizes=&quot;(max-width: 719px) 100vw, 719px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Finally finished... or not?&lt;/figcaption&gt;&lt;p&gt;Fortunately, this is only a warning, which I (&lt;em&gt;think&lt;/em&gt; I) fixed by adding the attribute &lt;code&gt;name=&amp;quot;...&amp;quot;&lt;/code&gt; to &lt;code&gt;&amp;lt;testsuite&amp;gt;&lt;/code&gt; in the &lt;code&gt;phpunit.xml.dist&lt;/code&gt; file. But at this stage, I&amp;#x27;m happy to have finally installed PHPUnit for my WordPress plugin, and I hope you are too! 😋&lt;/p&gt;</content:encoded></item><item><title><![CDATA[An Introduction To The Sage WordPress Starter Theme]]></title><description><![CDATA[WordPress is an awesome and powerful tool. It's not without reason that  more than a third of all websites  are powered by it. Unfortunately…]]></description><link>https://dilshankelsen.com/introduction-to-sage-wordpress-starter-theme/</link><guid isPermaLink="false">https://dilshankelsen.com/introduction-to-sage-wordpress-starter-theme/</guid><pubDate>Tue, 05 Nov 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;WordPress is an awesome and powerful tool. It&amp;#x27;s not without reason that &lt;a href=&quot;https://w3techs.com/technologies/details/cm-wordpress/all/all&quot; target=&quot;_blank&quot;&gt;more than a third of all websites&lt;/a&gt; are powered by it. Unfortunately, its development workflow feels quite outdated and clunky when compared to PHP Laravel and many front-end frameworks. This can be mainly attributed to the core development team striving to &lt;a href=&quot;https://make.wordpress.org/core/handbook/about/release-cycle/version-numbering/&quot; target=&quot;_blank&quot;&gt;&lt;em&gt;never&lt;/em&gt; break backwards compatibility&lt;/a&gt;. 🙄 Fortunately, a group of talented developers, working under the company name of &lt;a href=&quot;https://roots.io/&quot; target=&quot;_blank&quot;&gt;Roots&lt;/a&gt;, have taken it upon themselves to make WordPress development much more modern and streamlined. In this article, I&amp;#x27;ll be giving you an introduction to the Sage WordPress starter theme, their most popular project.&lt;/p&gt;&lt;p&gt;I&amp;#x27;ll be discussing what Sage has to offer, how to install it and how to read its folder structure.&lt;/p&gt;&lt;h2 id=&quot;what-sage-offers&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#what-sage-offers&quot; aria-label=&quot;what sage offers permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;What Sage offers&lt;/h2&gt;&lt;p&gt;If you&amp;#x27;re coming from a more traditional web development background, you&amp;#x27;ll be familiar with some of the tools Sage offers.&lt;/p&gt;&lt;p&gt;First, Sage is &lt;strong&gt;based on Webpack&lt;/strong&gt;. For those of you who are unaware of what it its: it&amp;#x27;s a module blunder. In layman&amp;#x27;s terms, &lt;strong&gt;it&amp;#x27;s a front-end tool&lt;/strong&gt; that takes your interdependent assets (e.g. JavaScript, images, HTML, CSS, …) and processes it into something usable by the client &lt;em&gt;aka website visitor&lt;/em&gt;. 🧍‍♂️ This might involve processing Sass, checking JavaScript for errors, optimizing images, etc. But honestly, you rarely ever need to mess with Webpack, so it&amp;#x27;s something you may safely ignore for the time being.&lt;/p&gt;&lt;p&gt;Second, it comes &lt;a href=&quot;https://getbootstrap.com/&quot; target=&quot;_blank&quot;&gt;preconfigured with Bootstrap&lt;/a&gt;, the most popular UI framework, making web design much easier for us grouchy developers. However, if Bootstrap isn&amp;#x27;t your jam, you can easily configure it with other frameworks such as Bulma, Foundation, etc. Or for the daring among us, you may not use one at all! 😱&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:550px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:50%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAKABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAQBAgP/xAAVAQEBAAAAAAAAAAAAAAAAAAAAAf/aAAwDAQACEAMQAAABys8kLkln/8QAGhABAAIDAQAAAAAAAAAAAAAAAQACERIiQv/aAAgBAQABBQLfLngBlqmnln//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/AT//xAAVEQEBAAAAAAAAAAAAAAAAAAAQEf/aAAgBAgEBPwGH/8QAGxAAAgEFAAAAAAAAAAAAAAAAAREAECExMoH/2gAIAQEABj8CTXYb4mwhKFf/xAAbEAEBAQACAwAAAAAAAAAAAAABEQAhMUFRgf/aAAgBAQABPyFERfpjAp5eW4ij/c1jM7mN5S7/2gAMAwEAAgADAAAAEFvP/8QAFREBAQAAAAAAAAAAAAAAAAAAEQD/2gAIAQMBAT8QSL//xAAXEQADAQAAAAAAAAAAAAAAAAAAARFh/9oACAECAQE/EFRNP//EABwQAQEAAgIDAAAAAAAAAAAAAAERACExgVFh4f/aAAgBAQABPxA+GjQJ8c46ysKD1v5kWoZuZ04CAqIKd42N8pcIkKzlz//Z&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Spiderman meme that says: Developer uses Bootstrap. You know, I&amp;#x27;m something of a designer myself.&quot; title=&quot;Spiderman meme that says: Developer uses Bootstrap. You know, I&amp;#x27;m something of a designer myself.&quot; src=&quot;/static/ddbd1ccfcdfab470c1ab38ac9cb650c5/d7854/meme-spiderman-scientist.jpg&quot; srcSet=&quot;/static/ddbd1ccfcdfab470c1ab38ac9cb650c5/e07e9/meme-spiderman-scientist.jpg 200w,/static/ddbd1ccfcdfab470c1ab38ac9cb650c5/066f9/meme-spiderman-scientist.jpg 400w,/static/ddbd1ccfcdfab470c1ab38ac9cb650c5/d7854/meme-spiderman-scientist.jpg 550w&quot; sizes=&quot;(max-width: 550px) 100vw, 550px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;You wish... 😉&lt;/figcaption&gt;&lt;p&gt;Last but not least, Sage uses &lt;a href=&quot;https://laravel.com/docs/8.x/blade&quot; target=&quot;_blank&quot;&gt;PHP Laravel&amp;#x27;s templating engine Blade&lt;/a&gt; for DRY (Don&amp;#x27;t Repeat Yourself) code. Now, while WordPress does provide &lt;strong&gt;DRY templating&lt;/strong&gt; to a certain extent, I believe Blade&amp;#x27;s focus on creating more maintainable projects just make it a better option over its vanilla counterpart.&lt;/p&gt;&lt;p&gt;An example of the templating language can be seen below.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-php&quot;&gt;PHP&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-php&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;@extends(&amp;#x27;layouts.base&amp;#x27;)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;@section(&amp;#x27;content&amp;#x27;)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  @while(have_posts())&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    {!! the_post() !!}&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    @include(&amp;#x27;partials/content-single&amp;#x27;)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  @endwhile&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;@endsection&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;installation&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#installation&quot; aria-label=&quot;installation permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Installation&lt;/h2&gt;&lt;p&gt;Before you can install the Sage starter theme, you first need to &lt;strong&gt;make sure you meet the following dependencies&lt;/strong&gt; in your development environment:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;https://wordpress.org/&quot; target=&quot;_blank&quot;&gt;WordPress&lt;/a&gt; &amp;gt;= 4.7&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://php.net/manual/en/install.php&quot; target=&quot;_blank&quot;&gt;PHP&lt;/a&gt; &amp;gt;= 7.1.3 (with &lt;a href=&quot;http://php.net/manual/en/book.mbstring.php&quot; target=&quot;_blank&quot;&gt;php-mbstring&lt;/a&gt; enabled)&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://getcomposer.org/download/&quot; target=&quot;_blank&quot;&gt;Composer&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://nodejs.org/&quot; target=&quot;_blank&quot;&gt;Node.js&lt;/a&gt; &amp;gt;= 8.0.0&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://yarnpkg.com/en/docs/install&quot; target=&quot;_blank&quot;&gt;Yarn&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;My preferred WordPress development environment uses Laragon, which comes with all these dependencies installed automatically. 😎 You may &lt;a href=&quot;/laragon-wordpress-development-environment/&quot;&gt;take a look at an article I wrote&lt;/a&gt; if you&amp;#x27;re interested in learning more about it!&lt;/p&gt;&lt;p&gt;Ahem, alright.... moving on. In your terminal, head over to the &lt;code&gt;wp-contentthemes&lt;/code&gt; directory of your WordPress site. Then, &lt;strong&gt;execute the following command&lt;/strong&gt;, replacing &lt;code&gt;&amp;lt;your-theme-name&amp;gt;&lt;/code&gt; with a name of your choosing.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;composer&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; create-project roots/sage &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;your-theme-name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Following this, Sage will be installed and you&amp;#x27;ll be prompted to answer several statements.&lt;/p&gt;&lt;p&gt;Regarding the prompt &lt;code&gt;Path to theme directory (e.g., /wp-content/themes/tutorial) [/app/themes/sage]&lt;/code&gt;, &lt;strong&gt;make sure to enter the path of your new theme&lt;/strong&gt;, i.e. &lt;code&gt;/wp-content/themes/&amp;lt;your-theme-name&amp;gt;&lt;/code&gt;. By default, it will suggest a different path that is used with one of Roots&amp;#x27; other projects, namely &lt;a href=&quot;https://roots.io/bedrock/&quot; target=&quot;_blank&quot;&gt;Bedrock&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Last but not least&lt;/strong&gt;, in your new theme folder, run &lt;code&gt;yarn&lt;/code&gt; or &lt;code&gt;npm install&lt;/code&gt; to install all the dependencies of your theme. Et voilà, the installation is complete! 💃&lt;/p&gt;&lt;p&gt;Aannndd don&amp;#x27;t forget, &lt;strong&gt;to run your site&lt;/strong&gt;, type &lt;code&gt;yarn start&lt;/code&gt; or &lt;code&gt;npm run start&lt;/code&gt;.&lt;/p&gt;&lt;h3 id=&quot;tty-mode-is-not-supported-on-windows-platform&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#tty-mode-is-not-supported-on-windows-platform&quot; aria-label=&quot;tty mode is not supported on windows platform permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;TTY mode is not supported on Windows platform&lt;/h3&gt;&lt;p&gt;For my fellow &lt;strong&gt;developers who are on Windows&lt;/strong&gt; like I am, you&amp;#x27;ll likely encounter an error during the installation process: &lt;code&gt;TTY mode is not supported on Windows platform&lt;/code&gt;. As a result, you won&amp;#x27;t be prompted any questions. However, if that&amp;#x27;s not the case for you, awesome! One less thing to worry about. 😀&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:410px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:83.50000000000001%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAARABQDASIAAhEBAxEB/8QAGQABAAIDAAAAAAAAAAAAAAAAAAIEAQMF/8QAFgEBAQEAAAAAAAAAAAAAAAAAAAIB/9oADAMBAAIQAxAAAAGvKGdmTSZduCuUE//EABwQAAICAgMAAAAAAAAAAAAAAAABAhEDMRASQv/aAAgBAQABBQJZHEU2dbEq4ow69Pf/xAAWEQEBAQAAAAAAAAAAAAAAAAAQARH/2gAIAQMBAT8B0h//xAAWEQEBAQAAAAAAAAAAAAAAAAAQARH/2gAIAQIBAT8Bwp//xAAcEAACAgIDAAAAAAAAAAAAAAAAAREhECIxcYH/2gAIAQEABj8C1SJcIuuiT05Y8M//xAAbEAEAAgMBAQAAAAAAAAAAAAABABEhQXGxof/aAAgBAQABPyEE6LlMAHN0xOTE9QzbLqWgmYFuHm9ms+uf/9oADAMBAAIAAwAAABDI3z//xAAZEQACAwEAAAAAAAAAAAAAAAAAARARITH/2gAIAQMBAT8QWqUcDP/EABgRAAMBAQAAAAAAAAAAAAAAAAABESEx/9oACAECAQE/EHish0I//8QAIBABAAEDBAMBAAAAAAAAAAAAAREAITFBYaGxEFFx8f/aAAgBAQABPxAZQRMDdLXvTUIIEER6mkLWGAhkz9qQi2DpvTh8JgnX9q4ZSyiY28ceR6rne6//2Q==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Office space meme that says: Windows, if you could not be such a pain in the ass that would be great.&quot; title=&quot;Office space meme that says: Windows, if you could not be such a pain in the ass that would be great.&quot; src=&quot;/static/34d557a91eb1c1c792f93fd1a6b53d00/0d3fb/meme-office.jpg&quot; srcSet=&quot;/static/34d557a91eb1c1c792f93fd1a6b53d00/e07e9/meme-office.jpg 200w,/static/34d557a91eb1c1c792f93fd1a6b53d00/066f9/meme-office.jpg 400w,/static/34d557a91eb1c1c792f93fd1a6b53d00/0d3fb/meme-office.jpg 410w&quot; sizes=&quot;(max-width: 410px) 100vw, 410px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Windows strikes, again!&lt;/figcaption&gt;&lt;p&gt;To solve this problem, &lt;code&gt;cd&lt;/code&gt; into your newly generated theme and simply run the following commands one by one:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ vendorbinsage meta&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ vendorbinsage config&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ vendorbinsage preset&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;folder-overview&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#folder-overview&quot; aria-label=&quot;folder overview permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Folder Overview&lt;/h2&gt;&lt;p&gt;Using any new tool can be daunting at first, 😥 which is why &lt;strong&gt;this section is dedicated to giving you an overview of Sage&amp;#x27;s project structure&lt;/strong&gt;. To make this as digestible as possible, I&amp;#x27;ll go through each group of files one by one. And to make things more visually appealing, I&amp;#x27;ll be using the &lt;a href=&quot;https://roots.io/sage/docs/theme-configuration-and-setup/#theme-structure&quot; target=&quot;_blank&quot;&gt;theme structure diagram from Roots&amp;#x27; website&lt;/a&gt;.&lt;/p&gt;&lt;h3 id=&quot;app&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#app&quot; aria-label=&quot;app permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;App&lt;/h3&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-text&quot;&gt;TEXT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-text&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;├── app/                  # → Theme PHP&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;│   ├── controllers/      # → Controller files&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;│   ├── admin.php         # → Theme customizer setup&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;│   ├── filters.php       # → Theme filters&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;│   ├── helpers.php       # → Helper functions&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;│   └── setup.php         # → Theme setup&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can consider the &lt;code&gt;app/&lt;/code&gt; directory as the &lt;strong&gt;PHP logic&lt;/strong&gt; 👩‍💻 &lt;strong&gt;of your site&lt;/strong&gt;. &lt;code&gt;controllers/&lt;/code&gt; contains your controllers... duh! But seriously, this is where your PHP logic belongs, e.g. sending a confirmation email after a visitor signs up for a newsletter. Now, the other files you can consider as a split up &lt;code&gt;functions.php&lt;/code&gt; file. Instead of having all your code in one place, they are grouped separately instead. 👨‍👩‍👧‍👦&lt;/p&gt;&lt;p&gt;You&amp;#x27;ll find yourself visiting this folder once in a while.&lt;/p&gt;&lt;h3 id=&quot;config&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#config&quot; aria-label=&quot;config permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Config&lt;/h3&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-text&quot;&gt;TEXT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-text&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;├── config/&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The name is &lt;strong&gt;pretty self-explanatory&lt;/strong&gt;. This folder takes care of your theme&amp;#x27;s configuration, e.g. namespacing or the path to your assets. Frankly, you&amp;#x27;ll rarely ever touch this folder.&lt;/p&gt;&lt;h3 id=&quot;resources&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#resources&quot; aria-label=&quot;resources permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Resources&lt;/h3&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-text&quot;&gt;TEXT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-text&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;├── resources/            # → Theme assets and templates&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;│   ├── assets/           # → Front-end assets&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;│   │   ├── config.json   # → Settings for compiled assets&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;│   │   ├── build/        # → Webpack and ESLint config&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;│   │   ├── fonts/        # → Theme fonts&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;│   │   ├── images/       # → Theme images&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;│   │   ├── scripts/      # → Theme JS&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;│   │   └── styles/       # → Theme stylesheets&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;│   ├── functions.php     # → Composer autoloader, theme includes&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;│   ├── index.php         # → Never manually edit&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;│   ├── screenshot.png    # → Theme screenshot for WP admin&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;│   ├── style.css         # → Theme meta information&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;│   └── views/            # → Theme templates&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;│       ├── layouts/      # → Base templates&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;│       └── partials/     # → Partial templates&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Without a doubt, &lt;strong&gt;this is where you&amp;#x27;ll be spending most of your time&lt;/strong&gt;. 🕐 The &lt;code&gt;resources/&lt;/code&gt; directory contains all your PHP pages, JavaScript, HTML, etc of your site.&lt;/p&gt;&lt;p&gt;First of all, the &lt;code&gt;assets/&lt;/code&gt; folder &lt;strong&gt;contains your images, fonts, JavaScript and CSS/SCSS files&lt;/strong&gt;. The folder &lt;code&gt;build/&lt;/code&gt; you shouldn&amp;#x27;t touch at all and &lt;code&gt;config.json/&lt;/code&gt; can be safely ignored as you almost never need to edit it. On the other hand, you&amp;#x27;ll find yourself working in the &lt;code&gt;scripts/&lt;/code&gt; and &lt;code&gt;styles/&lt;/code&gt; folder quite often.&lt;/p&gt;&lt;p&gt;Next, &lt;code&gt;views/&lt;/code&gt; &lt;strong&gt;contains all your PHP view templates&lt;/strong&gt;, or to be specific the Laravel Blade template files that Sage uses. As a result, you&amp;#x27;ll see your files ending with &lt;code&gt;blade.php&lt;/code&gt;. It is common practice to use layouts and partials in Blade template development, hence the folders. The latter is similar to WordPress partials, too.&lt;/p&gt;&lt;p&gt;Apart from these two folders, there are a bunch of other files which you can safely ignore as well. Lot of ignoring, right? Well, you might want to change the &lt;code&gt;screenshot.png&lt;/code&gt; image, so your WordPress theme has a nice cover. 📷&lt;/p&gt;&lt;h3 id=&quot;miscellaneous&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#miscellaneous&quot; aria-label=&quot;miscellaneous permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Miscellaneous&lt;/h3&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-text&quot;&gt;TEXT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-text&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;├── composer.json         # → Autoloading for `app/` files&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;├── composer.lock         # → Composer lock file (never edit)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;├── dist/                 # → Built theme assets (never edit)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;├── node_modules/         # → Node.js packages (never edit)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;├── package.json          # → Node.js dependencies and scripts&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;└── vendor/               # → Composer packages (never edit)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Lastly, I would call this the &lt;em&gt;&lt;strong&gt;What?&lt;/strong&gt;&lt;/em&gt; group, because that&amp;#x27;s the only thing you need to know about them. And by that I mean know nothing. 👉 Insert obligatory &lt;em&gt;&amp;quot;You know nothing Jon Snow&amp;quot;&lt;/em&gt; joke here...&lt;/p&gt;&lt;p&gt;&lt;strong&gt;If you ever do need to edit&lt;/strong&gt; the &lt;code&gt;composer.json&lt;/code&gt; and &lt;code&gt;package.json&lt;/code&gt; files, there are plenty of tutorials out there on how to do so. These files aren&amp;#x27;t specific to the Sage starter theme and hence I won&amp;#x27;t go into them.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Laragon WordPress Development Environment - The Hidden Gem]]></title><description><![CDATA[As a newcomer to WordPress development, finding an easy to use yet powerful development setup is easier said than done. After doing some…]]></description><link>https://dilshankelsen.com/laragon-wordpress-development-environment/</link><guid isPermaLink="false">https://dilshankelsen.com/laragon-wordpress-development-environment/</guid><pubDate>Tue, 22 Oct 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;As a newcomer to WordPress development, finding an easy to use yet powerful development setup is easier said than done. After doing some research online, you&amp;#x27;re bound to run into the same tools over and over again. To name a few, there are DesktopServer, Vagrant, Local by Flywheel and any variation of the LAMP stack, such as XAMPP or MAMP. However, there&amp;#x27;s one tool in particular that I consider to be on par, if not better, than those aforementioned ones. Strangely enough, &lt;strong&gt;it&amp;#x27;s barely mentioned in any articles and videos&lt;/strong&gt;, which makes it hard for people to discover. I should know it, since I randomly stumbled upon it in a Reddit post. So what&amp;#x27;s this mysterious ❓ development tool I&amp;#x27;m talking about? Welcome to the Laragon WordPress development environment, my personal favorite among all of them!&lt;/p&gt;&lt;h2 id=&quot;a-versatile-tool&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#a-versatile-tool&quot; aria-label=&quot;a versatile tool permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;A versatile tool&lt;/h2&gt;&lt;p&gt;If there&amp;#x27;s one thing you should know from the start, it&amp;#x27;s that Laragon is not limited to WordPress alone. Yes, this article is about our all beloved CMS. 🙏 Yet, if you&amp;#x27;re occasionally developing in other technology stacks such as &lt;strong&gt;Node.js, Python, Java, Go&lt;/strong&gt; and &lt;strong&gt;Ruby&lt;/strong&gt;, Laragon is an option worth checking out. Even better though, it&amp;#x27;s also compatible with &lt;strong&gt;PostgreSQL&lt;/strong&gt; &amp;amp; &lt;strong&gt;MongoDB&lt;/strong&gt; right out of the box, providing you with even more options besides the usual &lt;strong&gt;MySQL/MariaDB&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;This certainly is a step up from tools such as Local by Flywheel and DesktopServer, which restrict the customizability of your server setup to provide a more user friendly experience. But as you&amp;#x27;ll see next, Laragon shows that you can have it both ways!&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:400px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:100%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGQABAAMBAQAAAAAAAAAAAAAAAAMEBQEC/8QAFwEAAwEAAAAAAAAAAAAAAAAAAAECA//aAAwDAQACEAMQAAABreu6M1lrYIro1AMa/8QAHBAAAgMBAAMAAAAAAAAAAAAAAQIAAxIRBBMh/9oACAEBAAEFAvY0XZNjHSZ6+bBZ44ZqoPs4J//EABQRAQAAAAAAAAAAAAAAAAAAACD/2gAIAQMBAT8BH//EABYRAAMAAAAAAAAAAAAAAAAAAAABEP/aAAgBAgEBPwGs/8QAHRAAAgICAwEAAAAAAAAAAAAAAAERMQIhEBKRUf/aAAgBAQAGPwKy9Gsn4dPWJJ0ThkoJjf3ikf/EAB4QAAICAQUBAAAAAAAAAAAAAAABESExQVFhcYHw/9oACAEBAAE/IW2l6HQbbwJQS4gZiozCxClUGJQ31GTdNJdrPqRyu7s//9oADAMBAAIAAwAAABCzIAL/xAAZEQEAAgMAAAAAAAAAAAAAAAABACEQETH/2gAIAQMBAT8Q0FMrByf/xAAYEQADAQEAAAAAAAAAAAAAAAAAAREhEP/aAAgBAgEBPxC3UbxnWf/EAB0QAQADAQACAwAAAAAAAAAAAAEAESExUWGRofD/2gAIAQEAAT8QLcmZDf1TT2ZZpTfEqZa3gfiGxqsUYvrxUExWMgD1AVoA2HGPYEqtgQ2eO1UrpKb1Fb9zdMt0W9n/2Q==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Curious African child meme with the words &amp;quot;So you&amp;#x27;re telling me Laragon is easy to use and customizable?&amp;quot;&quot; title=&quot;Curious African child meme with the words &amp;quot;So you&amp;#x27;re telling me Laragon is easy to use and customizable?&amp;quot;&quot; src=&quot;/static/967a5f3fcd9efc6c714fb2303ad6d620/066f9/meme-african-child.jpg&quot; srcSet=&quot;/static/967a5f3fcd9efc6c714fb2303ad6d620/e07e9/meme-african-child.jpg 200w,/static/967a5f3fcd9efc6c714fb2303ad6d620/066f9/meme-african-child.jpg 400w&quot; sizes=&quot;(max-width: 400px) 100vw, 400px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;The hidden gem!&lt;/figcaption&gt;&lt;h2 id=&quot;where-laragon-excels&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#where-laragon-excels&quot; aria-label=&quot;where laragon excels permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Where Laragon excels&lt;/h2&gt;&lt;p&gt;Now, by &lt;em&gt;easy to use&lt;/em&gt; I don&amp;#x27;t mean that your grandpa would know how to use it. Rather, i&lt;strong&gt;t makes many tedious processes much easier to execute along with fewer potential bugs and headaches!&lt;/strong&gt; For example, Laragon offers a &lt;em&gt;Quick app&lt;/em&gt; option that allows you to set up WordPress effortlessly. Need to add Yarn, MongoDB or Python to your development environment? Easy! 😎 Just click a button under &lt;em&gt;Quick add&lt;/em&gt; et voilà. This alone will allow you to focus more on your development and less on setting up and tweaking your server to your liking.&lt;/p&gt;&lt;p&gt;Next to that, &lt;strong&gt;Laragon is fast&lt;/strong&gt; 🏃‍♀️... &lt;strong&gt;and very lightweight.&lt;/strong&gt; The core itself is less than 2MB and uses less than 4MB RAM when running. In an article by &lt;a href=&quot;https://webassist.xyz/laragon-local-development/&quot; target=&quot;_blank&quot;&gt;Athlone Harris-Compton&lt;/a&gt;, there&amp;#x27;s a wonderful speed comparison between Laragon, DesktopServer and Local by FlyWheel. It easily shows just how fast it really is. This alone may be reason enough for some to switch over.&lt;/p&gt;&lt;p&gt;There&amp;#x27;s a bunch of other awesome features you may be interested in, but instead of regurgitating everything in my article, why not check it out on &lt;a href=&quot;https://laragon.org/docs/#Features&quot; target=&quot;_blank&quot;&gt;their website&lt;/a&gt; instead?&lt;/p&gt;&lt;h2 id=&quot;installing-wordpress-with-laragon&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#installing-wordpress-with-laragon&quot; aria-label=&quot;installing wordpress with laragon permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Installing WordPress with Laragon&lt;/h2&gt;&lt;p&gt;While I&amp;#x27;m at it, let me show you how to get started with a local install of WordPress using Laragon. First off, head over to their site&amp;#x27;s &lt;a href=&quot;https://laragon.org/download/&quot; target=&quot;_blank&quot;&gt;download page&lt;/a&gt; and download the &lt;strong&gt;Laragon Full&lt;/strong&gt; edition. It comes jam-packed with a bunch of tools. Oh no! Did I forget to mention that it&amp;#x27;s available on Windows only? 😭 Sorry, mac users...&lt;/p&gt;&lt;p&gt;After launching the installer, you pretty much want to accept all the default settings and click next, next, next... ⏩ However, depending on your preferences, you may want to de-active certain options like auto start.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:507px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:78.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAIAAACZeshMAAAACXBIWXMAAAsTAAALEwEAmpwYAAABuUlEQVQoz32S2XaCMBCG8wR9pj5k36JP0PaqF72qVdwQF3YEBEwEkUV6pL9J4eBy/C6GZMhk/pkMkVU9y7KyLA+HQ1VVdV2fOoht1XA8HsWiPlW/x4JsGQ2jMIqi7XYbhiGlNI7j1sKTJEl9Ca58McvPsCS4xnXd+Xw+m81Go5GiKIvFYjqdwk4mk/F4rOv6ZrMJggBpTzzYzX+fXu3ntzWB5qIoYA+cdtFuQcLBIs/zIs/TLHtfBR8rn0AbYwwid5y4Ydeh62GcKk2KhBF4Pc+DTk3TVFUVssGqARUtl0v4TdPEGpG4grJdROk5GPXIsowTuEJpwDnf98/nuLSWVg78ROz3+30rTxwSMd1CrsqhIjMy9Pt9SZJ6vd5gMDAMw7ZtJIcctPqq/otgfPDClmWt12thoSJNU2EB+txVex2MSXAcB/k9jsXB45scPLIo6k6w6DbUYkKGwyFGBWqxhkUh8GBOEH8r/j8zQMN9TsjZNMCPq+G5H0w5bVW3TwLNPJLFScx/U/EE6BRhD0FaFLLSdGnpfA2U77ltuIHOZwlNIbuHQDYmz7Rs2fB+ZtpQ9Rw/si0TThT1B5d9b4Q4/2r7AAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Laragon setup installation window&quot; title=&quot;Laragon setup installation window&quot; src=&quot;/static/fa6fc0d956a23cd0b601207d4a9feb39/3ebb1/image-1.png&quot; srcSet=&quot;/static/fa6fc0d956a23cd0b601207d4a9feb39/772e8/image-1.png 200w,/static/fa6fc0d956a23cd0b601207d4a9feb39/e17e5/image-1.png 400w,/static/fa6fc0d956a23cd0b601207d4a9feb39/3ebb1/image-1.png 507w&quot; sizes=&quot;(max-width: 507px) 100vw, 507px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;The choice is yours ⏳&lt;/figcaption&gt;&lt;h3 id=&quot;the-dashboard-and-settings&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-dashboard-and-settings&quot; aria-label=&quot;the dashboard and settings permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The dashboard and settings&lt;/h3&gt;&lt;p&gt;The dashboard of Laragon isn&amp;#x27;t very intuitive at first since it lacks a traditional menu bar at the top. Instead, your menu is accessible via the word &lt;em&gt;Menu&lt;/em&gt; (who would&amp;#x27;ve thought that!) near the logo. On the other hand, your preferences are accessible through the cog icon in the top right corner.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:642px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:66%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAAAsTAAALEwEAmpwYAAABeElEQVQoz6WSW27cMAxFvf9NZAXdRxeQoEgw0xk/JHti2dT7acul5WYQTD4CtPygZErnXlJw9To5AUIrzYXsbyMDXDVwOYGYgLMZxgmsD0ve0prjsoa4xLg4H5kw1TPon9P7i5IpLd777dvI65ZSCB6kqd6Efbo2P7o+b1vwXgghpTTW4sYYgznEeFAppRijUlsoBRdzpYyLaJqR3RhjdV0TQsZx7LquaRpKKTIHrJRCuRC2eIdt3LFcjkMIWmts3jlnrc05f+4Xiyjk3BbTB+xSgcs1xFDelUCVL7At6ujxCO8ZAI62h2HAjJ3j/Ou6HvA8zyh6d0Lw0Xl/LWN0CdxgI3cYT5dlf5xH+B9ih32B14/x1hK5BC6H7d/PUr/P+N/OUlvCeEt7Kbh3tiZDS4hWUmh7JreubZSSHOByuVBK9pdQgt3O77eBK1uBtNcBLi1lE+BfcGr7303HuRiF+VX312s9gxjZ/HY6123HhQLOKTkR2k/C/AFutPBGMHWA2AAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Laragon dashboard&quot; title=&quot;Laragon dashboard&quot; src=&quot;/static/5b68263d364ef05c3c651a63bfb7c18a/1bba8/image-2.png&quot; srcSet=&quot;/static/5b68263d364ef05c3c651a63bfb7c18a/772e8/image-2.png 200w,/static/5b68263d364ef05c3c651a63bfb7c18a/e17e5/image-2.png 400w,/static/5b68263d364ef05c3c651a63bfb7c18a/1bba8/image-2.png 642w&quot; sizes=&quot;(max-width: 642px) 100vw, 642px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;Next, start your Laragon server by clicking on the &lt;em&gt;Start All&lt;/em&gt; button in the bottom left. By default, your server will run on Apache without an SSL certificate. However, you may adjust that under &lt;strong&gt;Preferences&lt;/strong&gt; (cog icon) &lt;strong&gt;&amp;gt; Services &amp;amp; Ports&lt;/strong&gt;.&lt;/p&gt;&lt;h3 id=&quot;installing-wordpress&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#installing-wordpress&quot; aria-label=&quot;installing wordpress permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Installing WordPress&lt;/h3&gt;&lt;p&gt;It&amp;#x27;s time for the real prize! 😏 To install a local WordPress copy, simply go to &lt;strong&gt;Menu&lt;/strong&gt; (or right click on the dashboard) &lt;strong&gt;&amp;gt; Quick app &amp;gt; WordPress&lt;/strong&gt;. Then, you&amp;#x27;ll be asked to name your project, which will also be used for your website&amp;#x27;s URL. Let Laragon do its magic... and done it is!&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:582px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:66.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAAAsTAAALEwEAmpwYAAABoUlEQVQoz4VSWY7UMBDt+x+BK3AE/hBiJDRCCCHEdHrSPVmcdOzEcbzGKxVnQEyzPTmW81zPVfXsw4Vqq02wVinFOddaG2N+zju8dzEGGMHD0gfnTcbhyPj9RAohgFJKxxRTyt/fEEJyDjLBoYdvdHlVXl63vdbryAShC56YccGGZFzULmzDwhy1tvjLwzyDdjvE+3BYlVqdhx84iU7TOI4E42VZ1nW9SRm9V00r1ZY7VxAOfl9mMc6QUjLGtNJrtJOiHb2iqceM3LSTxT7sFLQxDAN4loM27iQu7/CH98P9m/rt3dPH5JUz87Yb47M4vMwshAAbQ9zIWpKvrDpyVPCqVzgFHb3cO7gVW2sppXPGThqftE/CJutznbeu/yL+HZROp+I4jUPP5Kei+vyIFu3+UHb8gZDx3IgxYAF0IYwjTJJFWZ+Dc8B/Mv8bm1gK0WJat93CGLh1abumRVJweDCnumvqGkgw8nw+dx3abnEm164cBry9MCF4g2nVIMZmKLKsUVU3gi+E8aJCdfXEhYArLMsSoVYIOc+kR4/X6wDi73kd7Y/c5p2LAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Laragon dashboard after creating a WordPress project&quot; title=&quot;Laragon dashboard after creating a WordPress project&quot; src=&quot;/static/065a126704bc9718f542cd1b3c055705/7c1cd/image-3.png&quot; srcSet=&quot;/static/065a126704bc9718f542cd1b3c055705/772e8/image-3.png 200w,/static/065a126704bc9718f542cd1b3c055705/e17e5/image-3.png 400w,/static/065a126704bc9718f542cd1b3c055705/7c1cd/image-3.png 582w&quot; sizes=&quot;(max-width: 582px) 100vw, 582px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;After finishing your WordPress installation&lt;/figcaption&gt;&lt;p&gt;Now, your WordPress installation is ready to be set up by visiting your sites URL. Your database has already been configured to your installation, so that&amp;#x27;s one fewer problem to worry about during the setup. 😊&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Use Slick To Create A Carousel]]></title><description><![CDATA[My favorite go-to CSS framework is Bootstrap and I'm sure it is for many others, too. However, once in a while the components of such a…]]></description><link>https://dilshankelsen.com/how-to-use-slick-to-create-a-carousel/</link><guid isPermaLink="false">https://dilshankelsen.com/how-to-use-slick-to-create-a-carousel/</guid><pubDate>Tue, 08 Oct 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;My favorite go-to CSS framework is Bootstrap and I&amp;#x27;m sure it is for many others, too. However, once in a while the components of such a framework are too basic or don&amp;#x27;t fulfill your requirements adequately. In this case, it&amp;#x27;s best to use another more dedicated library which offers you more options. Recently, I came across such a situation while trying to build a carousel. I found a fantastic jQuery library called Slick, claiming it&amp;#x27;s &lt;em&gt;the last carousel you&amp;#x27;ll ever need&lt;/em&gt;. A pretty bold statement if you ask me! 👀 Well, in this tutorial, I&amp;#x27;ll be showing you how to use Slick to create a carousel as well as point out some of its quirks and errors that I encountered along the way.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:525px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:75%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAPABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAABAAD/8QAFAEBAAAAAAAAAAAAAAAAAAAAAf/aAAwDAQACEAMQAAABcArRNYwf/8QAHBAAAQMFAAAAAAAAAAAAAAAAAQACEQMSEyEj/9oACAEBAAEFAsm3OeCaii4RzX//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/AT//xAAVEQEBAAAAAAAAAAAAAAAAAAAAEf/aAAgBAgEBPwFH/8QAHRAAAQIHAAAAAAAAAAAAAAAAAAEiAhEhMTKBkf/aAAgBAQAGPwKortmQrukUyx//xAAcEAEAAwACAwAAAAAAAAAAAAABABEhMUFhgZH/2gAIAQEAAT8hzlR5muCtI25wzmI9mcZFr8lDePc//9oADAMBAAIAAwAAABBjH//EABYRAQEBAAAAAAAAAAAAAAAAAAABEf/aAAgBAwEBPxBtf//EABYRAQEBAAAAAAAAAAAAAAAAAAAhQf/aAAgBAgEBPxBev//EAB0QAQACAgIDAAAAAAAAAAAAAAERIQAxQVFxkfD/2gAIAQEAAT8Qg67EKgNBjBEgDZTpPMYjdo4ajs95VFELBuqB+jBlJ6VUHOjBKlJdJz//2Q==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Meme of man sitting on horse of a carousel, indicating the topic is actually about the library slick carousel and not a real carousel&quot; title=&quot;Meme of man sitting on horse of a carousel, indicating the topic is actually about the library slick carousel and not a real carousel&quot; src=&quot;/static/3aa342ca735c1c630439c1d4a631c8ec/7349d/meme-carousel.jpg&quot; srcSet=&quot;/static/3aa342ca735c1c630439c1d4a631c8ec/e07e9/meme-carousel.jpg 200w,/static/3aa342ca735c1c630439c1d4a631c8ec/066f9/meme-carousel.jpg 400w,/static/3aa342ca735c1c630439c1d4a631c8ec/7349d/meme-carousel.jpg 525w&quot; sizes=&quot;(max-width: 525px) 100vw, 525px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;You saw nothing... 👻&lt;/figcaption&gt;&lt;h2 id=&quot;installation&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#installation&quot; aria-label=&quot;installation permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Installation&lt;/h2&gt;&lt;p&gt;Every journey must start somewhere and Slick is no exception! For the installation, there are two options: either using a CDN or a package manager.&lt;/p&gt;&lt;h3 id=&quot;cdn&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#cdn&quot; aria-label=&quot;cdn permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;CDN&lt;/h3&gt;&lt;p&gt;Slick offers two option when it comes to using a CDN. Make a choice:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;https://cdnjs.com/libraries/slick-carousel&quot; target=&quot;_blank&quot;&gt;https://cdnjs.com/libraries/slick-carousel&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.jsdelivr.com/projects/jquery.slick&quot; target=&quot;_blank&quot;&gt;https://www.jsdelivr.com/projects/jquery.slick&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Afterwards, simply insert the CSS as links into the HTML page &lt;code&gt;head&lt;/code&gt; and the JavaScript just before your closing &lt;code&gt;body&lt;/code&gt; tag.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;link&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;rel&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;stylesheet&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;text/css&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;href&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;//cdn.jsdelivr.net/gh/kenwheeler/slick@1.8.1/slick/slick.css&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;&amp;lt;!-- Add the slick-theme.css if you want default styling --&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;link&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;rel&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;stylesheet&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;text/css&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;href&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;//cdn.jsdelivr.net/gh/kenwheeler/slick@1.8.1/slick/slick-theme.css&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;&amp;lt;!-- Some other code right here --&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;text/javascript&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;//cdn.jsdelivr.net/gh/kenwheeler/slick@1.8.1/slick/slick.min.js&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token script&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Et voilà! You&amp;#x27;re ready to use it. 😊&lt;/p&gt;&lt;h3 id=&quot;package-manager&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#package-manager&quot; aria-label=&quot;package manager permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Package manager&lt;/h3&gt;&lt;p&gt;If you prefer installing the library via a package manager, you&amp;#x27;ve got several options as well.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Yarn&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;yarn&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; slick-carousel&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# Bower&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;bower &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; --save slick-carousel&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;# NPM&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; slick-carousel&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Next, you have even more options! Optionsception... 🤯 First, you can insert the relevant CSS and JavaScript files into your HTML page the same way as in the previous section. Only this time, you would be fetching the files from your node modules folder.&lt;/p&gt;&lt;p&gt;On the other hand, my preferred way is to directly import the files into the main (S)CSS and JavaScript files of your project. This is especially recommended in conjunction with tools like Webpack and Gulp.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;scss&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-scss&quot;&gt;SCSS&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-scss&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// main.scss -- I&amp;#x27;m using Wepback sass-loader&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;@import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;~slick-carousel/slick/slick&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;@import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;~slick-carousel/slick/slick-theme&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// main.js&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;slick-carousel/slick/slick&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Alright, now that we&amp;#x27;ve successfully installed it, it&amp;#x27;s time to show how things are actually done in Slick. 😋&lt;/p&gt;&lt;h2 id=&quot;understanding-the-basics&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#understanding-the-basics&quot; aria-label=&quot;understanding the basics permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Understanding the basics&lt;/h2&gt;&lt;p&gt;With the Slick carousel 🎠 library, you have a ton of features at your disposal, yet a tad too many to actually go over them all. But honestly, that&amp;#x27;s not my goal! Instead, I want simply to help you understand how the library works and how to read &lt;a href=&quot;https://kenwheeler.github.io/slick/&quot; target=&quot;_blank&quot;&gt;its documentation&lt;/a&gt;.&lt;/p&gt;&lt;h3 id=&quot;the-code&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-code&quot; aria-label=&quot;the code permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The code&lt;/h3&gt;&lt;p&gt;Let&amp;#x27;s assume you&amp;#x27;re starting off with several images and you want to turn them into a beautiful carousel. How do you proceed?&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;img&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;/image-1.png&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;alt&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;Image 1&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;img&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;/image-2.png&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;alt&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;Image 2&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;img&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;/image-3.png&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;alt&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;Image 3&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Well, first we have to prepare the HTML layout of our images in such a way that it allows Slick to turn it into a carousel. We do this by adding 2 &lt;code&gt;div&lt;/code&gt; layers to our collection of images, with the most outer layer containing a class name your choice.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;my-carousel&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;img&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;/image-1.png&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;alt&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;Image 1&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;img&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;/image-2.png&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;alt&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;Image 2&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;img&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;/image-3.png&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;alt&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;Image 3&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The class of our outer &lt;code&gt;div&lt;/code&gt; is particularly important ❗, because we use it to tell Slick where our carousel is, which we do so as follows.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;.my-carousel&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;slick&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// setting-name: setting-value&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;After selecting our carousel with the jQuery selector, we apply the &lt;code&gt;slick()&lt;/code&gt; function to it and let the library do its magic... 🦄. By default, Slick will create a single item carousel with navigation arrows. But this isn&amp;#x27;t where the story ends yet, because we can still tell Slick &lt;em&gt;how&lt;/em&gt; to configure our carousel. And we do so by passing in an object with key-value pairs of settings.&lt;/p&gt;&lt;h2 id=&quot;the-features&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-features&quot; aria-label=&quot;the features permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The features&lt;/h2&gt;&lt;p&gt;Alright, so let&amp;#x27;s say I want to create a responsive multi-slide carousel. Which settings would I need to enable? 🧐&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:584px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:26%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAABJ0AAASdAHeZh94AAAAbElEQVQY02PwXXr/f/Tqh//DVj78H7riwX/zmbf/G0+/9d9kxm2yMIPLgnv/3RfeAxtkChWkyEDPxff/By578N9q1p3/RkCDYIaRayiD47y7YBdazb5DHRfazLkDNtAV6HXPRff+g/hGFBgIADl5CwDWtrXCAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;slidesToShow key setting of Slick carousel library&quot; title=&quot;slidesToShow key setting of Slick carousel library&quot; src=&quot;/static/490a3c28e5d7a1c33245e7a953aa2381/e05eb/image-1.png&quot; srcSet=&quot;/static/490a3c28e5d7a1c33245e7a953aa2381/772e8/image-1.png 200w,/static/490a3c28e5d7a1c33245e7a953aa2381/e17e5/image-1.png 400w,/static/490a3c28e5d7a1c33245e7a953aa2381/e05eb/image-1.png 584w&quot; sizes=&quot;(max-width: 584px) 100vw, 584px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Slick slidesToShow setting&lt;/figcaption&gt;&lt;p&gt;First off, let&amp;#x27;s enable multiple slides in our carousel. As shown above, the key &lt;code&gt;slidesToShow&lt;/code&gt; is responsible for this feature and takes a value of type &lt;code&gt;integer&lt;/code&gt;, which has 1 set as its default. How about we change that to 2?&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;.my-carousel&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;slick&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;slidesToShow&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:583px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:31.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAABJ0AAASdAHeZh94AAAAx0lEQVQY06VRVw7CUAzrlRhiL7EECD5YooxzsUrZYp+o85WLmKRAkRAfCD6suI5fFKdSZWNDPjloHQRiigH/SENwrP8MqbiyUCLkFqYrBP4dWF5bqG1tFJYWotPvB777+JshZWmzPCGlGghPdDcyN3xDzeP+D3h6nn1vwypt16T78Q17Zwfy0cHgckWfeGMvqF7RpRsz2keBDvUZ7Gcf6/wPeE6EEkoct74TaNJjrsmZ4SIzNxFX7jzBmvrinIZrWjU9LfSIfQO8z0UH7OhxdAAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;responsive key setting of Slick carousel library&quot; title=&quot;responsive key setting of Slick carousel library&quot; src=&quot;/static/858b10b4be4a7d9251ecd7fa15ff3f3f/9fc4b/image-2.png&quot; srcSet=&quot;/static/858b10b4be4a7d9251ecd7fa15ff3f3f/772e8/image-2.png 200w,/static/858b10b4be4a7d9251ecd7fa15ff3f3f/e17e5/image-2.png 400w,/static/858b10b4be4a7d9251ecd7fa15ff3f3f/9fc4b/image-2.png 583w&quot; sizes=&quot;(max-width: 583px) 100vw, 583px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Slick responsive setting&lt;/figcaption&gt;&lt;p&gt;Next up, making our carousel responsive! Again, as shown in the image above, the key &lt;code&gt;responsive&lt;/code&gt; takes an object containing breakpoints and a respective settings object. At first, the structure of this object might be a bit difficult to guess, but fortunately we can consult a demo with some code on &lt;a href=&quot;https://kenwheeler.github.io/slick/&quot; target=&quot;_blank&quot;&gt;their page&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;How about we only show 1 slide for mobile? 📱&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;.my-carousel&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;slick&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;slidesToShow&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;responsive&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;breakpoint&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;settings&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;slidesToShow&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This little demonstration could go on forever, but at this point I think you get the gist (I hope 😅). I recommend taking a look at the documentation and exploring &lt;a href=&quot;https://kenwheeler.github.io/slick/#settings&quot; target=&quot;_blank&quot;&gt;the various settings&lt;/a&gt; that are available.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:400px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:115.00000000000001%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAXABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAQDAgX/xAAWAQEBAQAAAAAAAAAAAAAAAAAAAQL/2gAMAwEAAhADEAAAAZrvMZmagd6FUjN//8QAGhAAAwADAQAAAAAAAAAAAAAAAQIDABITEP/aAAgBAQABBQKB1wup8QDnEnoZHJKHkdFyV1E//8QAFBEBAAAAAAAAAAAAAAAAAAAAIP/aAAgBAwEBPwEf/8QAFhEAAwAAAAAAAAAAAAAAAAAAABAR/9oACAECAQE/ASv/xAAeEAACAgICAwAAAAAAAAAAAAAAAQIhERIDIjJRYf/aAAgBAQAGPwK3RS1+lEmLfxM8S2iJSo6yx7yJKj//xAAcEAEAAgMBAQEAAAAAAAAAAAABABExQVEhYbH/2gAIAQEAAT8hRnhudguZXnUu3iXDfC/2MAp6FcRDYTcvBypNkwaul24OH2BP/9oADAMBAAIAAwAAABBv+H//xAAWEQEBAQAAAAAAAAAAAAAAAAAAARH/2gAIAQMBAT8QkYjX/8QAFxEBAQEBAAAAAAAAAAAAAAAAAREAEP/aAAgBAgEBPxBvCV03/8QAIRABAQACAQQCAwAAAAAAAAAAAREAITFBUWFxkcGhsdH/2gAIAQEAAT8QVDjSi9s9XHt6ZpEdJH3iEdAw3z5zZruagP8AFzkNEcE7n1m1GkAsbE/HxMMaBR3bZ4b1ycmCfoc+pxhKyctB65//2Q==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Meme of Yoda saying the reader still has much to learn about slick carousel&quot; title=&quot;Meme of Yoda saying the reader still has much to learn about slick carousel&quot; src=&quot;/static/3d4f8883102c51bd73f526764ea69646/066f9/meme-yoda.jpg&quot; srcSet=&quot;/static/3d4f8883102c51bd73f526764ea69646/e07e9/meme-yoda.jpg 200w,/static/3d4f8883102c51bd73f526764ea69646/066f9/meme-yoda.jpg 400w&quot; sizes=&quot;(max-width: 400px) 100vw, 400px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Never stop learning!&lt;/figcaption&gt;&lt;h2 id=&quot;bugs-galore&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#bugs-galore&quot; aria-label=&quot;bugs galore permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Bugs galore!&lt;/h2&gt;&lt;p&gt;No piece of software is without its flaws and while trying out Slick, I came across two bugs which provided me quite a few headaches...&lt;/p&gt;&lt;h3 id=&quot;not-rendering-within-a-hidden-div-element&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#not-rendering-within-a-hidden-div-element&quot; aria-label=&quot;not rendering within a hidden div element permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Not rendering within a hidden div element&lt;/h3&gt;&lt;p&gt;When I was first using Slick, I placed it in a hidden div element that could be toggled on and off. To my surprise, the carousel wasn&amp;#x27;t rendering until I clicked on one of the navigation buttons, as seen in &lt;a href=&quot;https://captainmccrank.github.io/&quot; target=&quot;_blank&quot;&gt;this example&lt;/a&gt;. 👈&lt;/p&gt;&lt;p&gt;Luckily, &lt;a href=&quot;https://github.com/kenwheeler/slick/issues/2806&quot; target=&quot;_blank&quot;&gt;someone else&lt;/a&gt; already experienced this issue. As it turns out, the width of the carousel slides are automatically computed based on the parent element, which turns out to be 0 if it is hidden. Unfortunately, this issue was never resolved! (╯°□°）╯︵ ┻━┻&lt;/p&gt;&lt;p&gt;However, I did come up with a quick and dirty workaround. Instead of initializing the carousel on page load, it will be initialized upon clicking the element responsible for making the carousel&amp;#x27;s parent element visible.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;#button-toggle&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;click&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setTimeout&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;.my-carousel&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;slick&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// settings here&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setTimeout&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;.my-carousel&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;removeClass&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;invisible&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In my little hack, I had to add a teeny tiny delay or the code wouldn&amp;#x27;t run. Furthermore, in order to hide the jittery rendering from the user, the carousel itself is invisible by default and only made visible after the rendering has been completed. Now, whenever you click the button, it seems like there&amp;#x27;s a minuscule delay in showing the carousel. Another dirty 😏 trick is to introduce a fade-in, et voilà! It&amp;#x27;s like there never was a bug in the first place.&lt;/p&gt;&lt;h3 id=&quot;webpack-production-build-error&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#webpack-production-build-error&quot; aria-label=&quot;webpack production build error permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Webpack production build error&lt;/h3&gt;&lt;p&gt;If you&amp;#x27;re importing Slick&amp;#x27;s SCSS with the &lt;code&gt;@import&lt;/code&gt; statement and are using Webpack as well, you are in for a surprise when building your project for production.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-text&quot;&gt;TEXT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-text&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;ERROR in ./node_modules/css-loader!./node_modules/postcss-loader/lib!./node_modules/sass-loader/lib/loader.js!./sass/app.scss Module not found: Error: Can&amp;#x27;t resolve &amp;#x27;./ajax-loader.gif&amp;#x27; in &amp;#x27;/Users/Vishal/Documents/Work/nf-core/sass&amp;#x27; @ ./node_modules/css-loader!./node_modules/postcss-loader/lib!./node_modules/sass-loader/lib/loader.js!./sass/app.scss 6:89679-89707&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Zoinks! 😥 However, there is &lt;a href=&quot;https://stackoverflow.com/questions/48416945/importing-slick-carousel-theme-css-throws-errors-on-webpack-build#48422942&quot; target=&quot;_blank&quot;&gt;a solution&lt;/a&gt;!&lt;/p&gt;&lt;p&gt;Simply add these two lines before your import statements:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;scss&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-scss&quot;&gt;SCSS&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-scss&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token property variable&quot;&gt;$slick-font-path&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;~slick-carousel/slick/fonts/&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// To add&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token property variable&quot;&gt;$slick-loader-path&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;~slick-carousel/slick/&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// To add&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;@import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;~slick-carousel/slick/slick&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;@import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;~slick-carousel/slick/slick-theme&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Do HTTP Requests In PHP With cURL]]></title><description><![CDATA[Making HTTP requests is one of the most fundamental tasks that you can perform as a web developer. As such, it's important to know how to do…]]></description><link>https://dilshankelsen.com/how-to-do-http-requests-in-php-with-curl/</link><guid isPermaLink="false">https://dilshankelsen.com/how-to-do-http-requests-in-php-with-curl/</guid><pubDate>Tue, 01 Oct 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Making HTTP requests is one of the most fundamental tasks that you can perform as a web developer. As such, it&amp;#x27;s important to know how to do them properly in the language of your choice. Having recently made the jump to PHP, it took me a while to understand how these were done. However, now that I&amp;#x27;ve familiarized myself with it, I thought I&amp;#x27;d share other newcomers 🤓 how to do HTTP requests in PHP with cURL, a built-in library.&lt;/p&gt;&lt;h2 id=&quot;what-is-curl&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#what-is-curl&quot; aria-label=&quot;what is curl permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;What is cURL?&lt;/h2&gt;&lt;p&gt;If you&amp;#x27;ve spent some time in the web development sphere, you might have already heard of it. &lt;a href=&quot;https://curl.haxx.se/&quot; target=&quot;_blank&quot;&gt;cURL&lt;/a&gt;, which stands for client URL, is an open-source command line tool that is used to transfer and receive data using internet protocols such as HTTP, HTTPS and FTP. Created by Daniel Stenberg in 1997, cURL is used in countless devices ranging from cars 🚗 and television sets to tablets and audio equipment. 🔉&lt;/p&gt;&lt;p&gt;So where does PHP come into play? Well, PHP has a module called &lt;em&gt;cURL PHP&lt;/em&gt; that allows us to access the functionality offered by this awesome tool. Let me show you next how to use it, &amp;#x27;kay?&lt;/p&gt;&lt;span class=&quot;text-center&quot;&gt;&lt;p&gt;&lt;img src=&quot;/static/evil-patrick-8d515b42e21fed71de505c58ed26c3a2.gif&quot; alt=&quot;GIF of Patrick from Spongebob Squarepants rubbing his hands eagerly&quot;/&gt;&lt;/p&gt;&lt;/span&gt;&lt;figcaption&gt;Time to get started! 😋&lt;/figcaption&gt;&lt;h2 id=&quot;the-code&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-code&quot; aria-label=&quot;the code permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The Code&lt;/h2&gt;&lt;p&gt;In this tutorial, I&amp;#x27;ll be showing you how to do the 2 most common HTTP request methods: &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;POST&lt;/code&gt;. However, regardless of which method you opt for (even &lt;code&gt;PUT&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt;) , they will all follow the same steps.&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Initialize a cURL session&lt;/li&gt;&lt;li&gt;Set the URL and other various options for the session&lt;/li&gt;&lt;li&gt;Execute the request and save the response&lt;/li&gt;&lt;li&gt;Close the session to free up resources&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Seems pretty straightforward, right? 😉 So how would this look like with code?&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-php&quot;&gt;PHP&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-php&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&amp;lt;?php&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    // 1. Open cURL session&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    $ch = curl_init();&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    // 2. Set options&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    curl_setopt($ch, CURLOPT_URL, &amp;quot;http://dilshankelsen.com&amp;quot;);&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    // 3. Execute and save response&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    $response = curl_exec($ch);&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    // 4. Close cURL session&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    curl_close($ch);&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Perhaps with the exception of the options setting (which we&amp;#x27;ll get into next 😁), everything appears to be quite straightforward. At the end of these 4 steps, you&amp;#x27;re left with a variable &lt;code&gt;$response&lt;/code&gt; that contains the response of our GET request. This may be JSON, a string or a complete HTML page.&lt;/p&gt;&lt;h3 id=&quot;understanding-options&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#understanding-options&quot; aria-label=&quot;understanding options permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Understanding options&lt;/h3&gt;&lt;p&gt;Next to error handling, options are where you will be paying attention most of the time. Options are set with the &lt;code&gt;curl_setopt(resource $ch, int $option, mixed $value)&lt;/code&gt; function, which takes three parameters: the cURL resource/session, the option and its corresponding value.&lt;/p&gt;&lt;p&gt;So you may be wondering: &amp;quot;&lt;em&gt;So what kind of options can I set?&lt;/em&gt;&amp;quot; 🤔 Ahem.... &lt;a href=&quot;https://www.php.net/manual/en/function.curl-setopt.php&quot; target=&quot;_blank&quot;&gt;maannnnyyyy&lt;/a&gt;! Too many to even go through one by one. Fortunately, you will only need a handful for most of your requests:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;CURLOPT_RETURNTRANSFER&lt;/code&gt; - Return the response as a string instead of outputting it to the screen&lt;/li&gt;&lt;li&gt;&lt;code&gt;CURLOPT_CONNECTTIMEOUT&lt;/code&gt; - The number of seconds to wait while trying to connect&lt;/li&gt;&lt;li&gt;&lt;code&gt;CURLOPT_TIMEOUT&lt;/code&gt; - The number of seconds to allow cURL functions to execute&lt;/li&gt;&lt;li&gt;&lt;code&gt;CURLOPT_URL&lt;/code&gt; - The URL to fetch&lt;/li&gt;&lt;li&gt;&lt;code&gt;CURLOPT_CUSTOMREQUEST&lt;/code&gt; - Set custom request method such as &lt;code&gt;PUT&lt;/code&gt; or &lt;code&gt;DELETE&lt;/code&gt;. cURL defaults to &lt;code&gt;GET&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;CURLOPT_POSTFIELDS&lt;/code&gt; - An array of the data to send in a request&lt;/li&gt;&lt;li&gt;&lt;code&gt;CURLOPT_HTTPHEADER&lt;/code&gt; - An array of HTTP header fields to set&lt;/li&gt;&lt;/ul&gt;&lt;h4 id=&quot;a-handy-trick&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#a-handy-trick&quot; aria-label=&quot;a handy trick permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;A handy trick&lt;/h4&gt;&lt;p&gt;Now, it doesn&amp;#x27;t take a genius 🤓 to realize that you&amp;#x27;ll be repeating &lt;code&gt;curl_setopt()&lt;/code&gt; quite a bit if you need to set several options. Of course, the creators were well aware of this and have provided us two ways to cut down on the repetitiveness.&lt;/p&gt;&lt;p&gt;First, instead of setting the URL with &lt;code&gt;curl_setopt($ch, CURLOPT_URL, &amp;quot;dilshankelsen.com&amp;quot;)&lt;/code&gt;, we can pass in the URL immediately when initializing the cURL session, like so: &lt;code&gt;$ch = curl_init(&amp;quot;dilshankelsen.com&amp;quot;)&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Second, we can use &lt;code&gt;curl_setopt_array(resource $ch, array $options)&lt;/code&gt; to set multiple options at once. An example would look as follows.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-php&quot;&gt;PHP&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-php&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;curl_setopt_array($ch, [&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    CURLOPT_RETURNTRANSFER =&amp;gt; 1,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    CURLOPT_URL =&amp;gt; &amp;quot;dilshankelsen.com&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;]);&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:500px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:100%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGAABAAMBAAAAAAAAAAAAAAAAAAQFBgP/xAAVAQEBAAAAAAAAAAAAAAAAAAADBP/aAAwDAQACEAMQAAAB46CjuJ2kigcxegUyg83/xAAeEAAABQUBAAAAAAAAAAAAAAAAAQIDEQQSISMyM//aAAgBAQABBQJKtqOQ7YRsQTInFN4D/8QAFhEAAwAAAAAAAAAAAAAAAAAAAxAx/9oACAEDAQE/ASRFi//EABcRAAMBAAAAAAAAAAAAAAAAAAEDEDH/2gAIAQIBAT8BSQYvZ//EABwQAAAGAwAAAAAAAAAAAAAAAAABAhARISIxMv/aAAgBAQAGPwKzqRjpubCYYzCW/8QAHRAAAwEAAQUAAAAAAAAAAAAAAAERQRAhUWHh8P/aAAgBAQABPyGCqGtwZNbTyzrompqDIEchS++9ifPnj//aAAwDAQACAAMAAAAQwAg8/8QAFxEBAQEBAAAAAAAAAAAAAAAAARCxEf/aAAgBAwEBPxAnCTYyf//EABoRAAICAwAAAAAAAAAAAAAAAAEQETFBUfD/2gAIAQIBAT8QoBhDB9pf/8QAHhABAQACAgIDAAAAAAAAAAAAAREAITFBUWEQgbH/2gAIAQEAAT8QFri5Ztu/WMLawaO/OGnC+sAwc3iPeBrWQiyrhRQyrGoFveCWnT+vj//Z&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Drake meme of loving clean code&quot; title=&quot;Drake meme of loving clean code&quot; src=&quot;/static/090e304b38a7a852e002162423046c8d/41099/meme-drake.jpg&quot; srcSet=&quot;/static/090e304b38a7a852e002162423046c8d/e07e9/meme-drake.jpg 200w,/static/090e304b38a7a852e002162423046c8d/066f9/meme-drake.jpg 400w,/static/090e304b38a7a852e002162423046c8d/41099/meme-drake.jpg 500w&quot; sizes=&quot;(max-width: 500px) 100vw, 500px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Gotta love clean code! 💻&lt;/figcaption&gt;&lt;h3 id=&quot;receiving-the-response&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#receiving-the-response&quot; aria-label=&quot;receiving the response permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Receiving the response&lt;/h3&gt;&lt;p&gt;As you surely noticed before, &lt;code&gt;curl_exec($ch)&lt;/code&gt; is responsible for executing your HTTP request. In addition to that, it will return three different responses depending on the following conditions:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;false&lt;/code&gt; if the request fails to execute&lt;/li&gt;&lt;li&gt;&lt;code&gt;true&lt;/code&gt; if the request executes without error and &lt;code&gt;CURLOPT_RETURNTRANSFER&lt;/code&gt; is set to &lt;code&gt;false&lt;/code&gt;&lt;/li&gt;&lt;li&gt;The result (e.g JSON) if the request executes without error and &lt;code&gt;CURLOPT_RETURNTRANSFER&lt;/code&gt; is set to &lt;code&gt;true&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Another useful function at your disposable is &lt;code&gt;curl_getinfo($ch)&lt;/code&gt;, which returns information about your request, such as the response code or total transaction time.&lt;/p&gt;&lt;h3 id=&quot;error-handling&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#error-handling&quot; aria-label=&quot;error handling permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Error handling&lt;/h3&gt;&lt;p&gt;Errors ❌ happen... there&amp;#x27;s nothing you can do about it. But at the very least, you should know how to properly handle them with cURL. For this task, we have two functions: &lt;code&gt;curl_error($ch)&lt;/code&gt; and &lt;code&gt;curl_errno($ch)&lt;/code&gt;. If applicable, the former returns the error message, while the latter returns the &lt;a href=&quot;https://curl.haxx.se/libcurl/c/libcurl-errors.html&quot; target=&quot;_blank&quot;&gt;cURL error code&lt;/a&gt;. The most common way to use these two functions is with an &lt;code&gt;if&lt;/code&gt; statement.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-php&quot;&gt;PHP&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-php&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;if (!curl_exec($ch)) {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    die(&amp;#x27;Error: &amp;quot;&amp;#x27; . curl_error($ch) . &amp;#x27;&amp;quot; - Code: &amp;#x27; . curl_errno($ch));&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;post-request&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#post-request&quot; aria-label=&quot;post request permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;POST request&lt;/h2&gt;&lt;p&gt;So far, all examples I&amp;#x27;ve been showing you were &lt;code&gt;GET&lt;/code&gt; requests. In fact, when no request method is specified, cURL simply defaults to &lt;code&gt;GET&lt;/code&gt;. To tell cURL you would like execute a &lt;code&gt;POST&lt;/code&gt; method, simply set the &lt;code&gt;CURLOPT_POST&lt;/code&gt; option to true. However, I prefer to use the option &lt;code&gt;CURLOPT_CUSTOMREQUEST&lt;/code&gt; as any method may be specified with it. One less option to remember! 😏&lt;/p&gt;&lt;p&gt;Without further ado, let&amp;#x27;s see how you would preform a proper &lt;code&gt;POST&lt;/code&gt; request, with error and JSON handling.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-php&quot;&gt;PHP&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-php&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&amp;lt;?php&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;// Initialize cURL resource&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ch = curl_init(&amp;quot;http://dilshankelsen.com/doesnotexist&amp;quot;);&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;// Data to send&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$payload = [&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &amp;quot;item1&amp;quot; =&amp;gt; &amp;quot;value&amp;quot;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &amp;quot;item2&amp;quot; =&amp;gt; &amp;quot;value2&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;];&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;// Header to send&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$header = [&amp;#x27;Content-Type:application/json&amp;#x27;];&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;// Set cURL options&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;curl_setopt_array($ch, [&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  CURLOPT_RETURNTRANSFER =&amp;gt; 1,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  CURLOPT_CUSTOMREQUEST =&amp;gt; &amp;quot;POST&amp;quot;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  CURLOPT_POSTFIELDS =&amp;gt; json_encode($payload),&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  CURLOPT_HTTPHEADER =&amp;gt; $header&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;]);&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;// Execute the request and save the response&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$response = curl_exec($ch);&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;// Check for errors&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;if (!$response) {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  die(&amp;#x27;Error: &amp;quot;&amp;#x27; . curl_error($ch) . &amp;#x27;&amp;quot; - Code: &amp;#x27; . curl_errno($ch));&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;// Decode the received JSON&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$received_data = json_decode($response);&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;// Close session to clear up resources&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;curl_close($ch);&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In this example, we attached a payload to our request. For this, we needed to encode our payload and set the appropriate headers in the options. If you&amp;#x27;re not familiar with JSON handling in PHP, why not &lt;a href=&quot;/how-to-handle-json-objects-with-php/&quot;&gt;check out my article&lt;/a&gt; on this topic (shameless plug 😅).&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Handle JSON Objects With PHP]]></title><description><![CDATA[Working with JavaScript for an extended period of time can make you take many things for granted that aren't as easy to do in other…]]></description><link>https://dilshankelsen.com/how-to-handle-json-objects-with-php/</link><guid isPermaLink="false">https://dilshankelsen.com/how-to-handle-json-objects-with-php/</guid><pubDate>Tue, 24 Sep 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Working with JavaScript for an extended period of time can make you take many things for granted that aren&amp;#x27;t as easy to do in other programming languages, one of them being JSON handling. Since JSON is derived from the JavaScript language, they share much of the same syntax. Naturally, this makes them particularly well suited for each other. However, handling this data format in another language such as PHP isn&amp;#x27;t as straightforward as you might hope and it can take some time getting used to. 😥 In this article, I hope to give you an overview on this topic and help you get started on how to handle JSON objects with PHP.&lt;/p&gt;&lt;p&gt;Please note that I will not cover what JSON actually is and how it works. For that, I recommend &lt;a href=&quot;https://www.copterlabs.com/json-what-it-is-how-it-works-how-to-use-it/&quot; target=&quot;_blank&quot;&gt;this fine article&lt;/a&gt; by Copter Labs.&lt;/p&gt;&lt;h2 id=&quot;encoding-and-decoding-json&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#encoding-and-decoding-json&quot; aria-label=&quot;encoding and decoding json permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Encoding and decoding JSON&lt;/h2&gt;&lt;p&gt;Since version 5.2, PHP features two built-in functions to transform your PHP data structures into JSON and vice versa:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;json_encode(mixed $value)&lt;/code&gt;; transforms a PHP data structure into a JSON object&lt;/li&gt;&lt;li&gt;&lt;code&gt;json_decode(string $json)&lt;/code&gt;; transforms a JSON object into a PHP data structure&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:432px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:96.50000000000001%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAATABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAQDAv/EABYBAQEBAAAAAAAAAAAAAAAAAAMBAv/aAAwDAQACEAMQAAAB6Q6E9CdN61hFONX/xAAbEAACAgMBAAAAAAAAAAAAAAAAAQIDERMhMf/aAAgBAQABBQLYbR2D8gsxK0jHZxTf/8QAGBEBAAMBAAAAAAAAAAAAAAAAAAECESL/2gAIAQMBAT8B2rhDH//EABkRAAIDAQAAAAAAAAAAAAAAAAABAhEhMf/aAAgBAgEBPwGjSfRNn//EABkQAAIDAQAAAAAAAAAAAAAAAAAhARAxIv/aAAgBAQAGPwJjMFLOoqVWH//EABsQAQADAAMBAAAAAAAAAAAAAAEAESExUYFh/9oACAEBAAE/ITGG/jHoyAstV4ItfgkurL4yIshTjIhGS+ukn//aAAwDAQACAAMAAAAQjO/8/8QAGREBAAMBAQAAAAAAAAAAAAAAAQARMSGh/9oACAEDAQE/EG1Ag132dkReT//EABkRAAMBAQEAAAAAAAAAAAAAAAABESExUf/aAAgBAgEBPxCsbZPA2yDp/8QAGhABAQEBAQEBAAAAAAAAAAAAAREAITFBYf/aAAgBAQABPxC/kDjAcIrFJ97iyBJCpnZCOl7PzDULWqqfHAUSutzSmkc91oHJhpHoRS7/2Q==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Batman slaps Robin meme: Keep reading the &amp;quot;How To Handle JSON Objects With PHP&amp;quot; article&quot; title=&quot;Batman slaps Robin meme: Keep reading the &amp;quot;How To Handle JSON Objects With PHP&amp;quot; article&quot; src=&quot;/static/691439d036466e21bf59dde73721e505/08871/meme-batman-slap.jpg&quot; srcSet=&quot;/static/691439d036466e21bf59dde73721e505/e07e9/meme-batman-slap.jpg 200w,/static/691439d036466e21bf59dde73721e505/066f9/meme-batman-slap.jpg 400w,/static/691439d036466e21bf59dde73721e505/08871/meme-batman-slap.jpg 432w&quot; sizes=&quot;(max-width: 432px) 100vw, 432px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Not so fast! ✋&lt;/figcaption&gt;&lt;p&gt;Now, slapping any PHP data structure into the former function won&amp;#x27;t always work the way you expect it to. Here&amp;#x27;s what you need to pay attention to.&lt;/p&gt;&lt;h3 id=&quot;watch-out-for-indexed-arrays&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#watch-out-for-indexed-arrays&quot; aria-label=&quot;watch out for indexed arrays permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Watch out for indexed arrays&lt;/h3&gt;&lt;p&gt;If you try to encode an indexed array in PHP, you&amp;#x27;ll fall flat on your face, my friend.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-php&quot;&gt;PHP&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-php&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&amp;lt;?php&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$myArray = array(&amp;#x27;Audi&amp;#x27;, &amp;#x27;Mercedes&amp;#x27;, &amp;#x27;VW&amp;#x27;);&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;echo json_encode($myArray); // [&amp;quot;Audi&amp;quot;,&amp;quot;Mercedes&amp;quot;,&amp;quot;VW&amp;quot;]&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As you can see, trying to encode an indexed array in PHP by itself will just spit out another array. 😐 What if we try to encode other data structures. What can kind of results can we expect?&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-php&quot;&gt;PHP&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-php&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&amp;lt;?php&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$associativeArray = array(&amp;quot;Chunk Bytes&amp;quot; =&amp;gt; true, &amp;quot;Awesome&amp;quot; =&amp;gt; &amp;quot;Yes&amp;quot;);&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;echo json_encode($associativeArray); // {&amp;quot;Chunk Bytes&amp;quot;:true,&amp;quot;Awesome&amp;quot;:&amp;quot;Yes&amp;quot;}&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;class Person {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    public $firstname = &amp;quot;Dilshan&amp;quot;;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    protected $lastname = &amp;quot;Kelsen&amp;quot;;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    private $blog = &amp;quot;Chunk Bytes&amp;quot;;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$person = new Person();&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;echo json_encode($person); // {&amp;quot;firstname&amp;quot;:&amp;quot;Dilshan&amp;quot;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As seen in the example above, encoding an associative array will return a JSON string as expected. Nothing to worry about here. 😀 Conversely, encoding an object works as you would expect it to... with one small caveat: &lt;code&gt;protected&lt;/code&gt; and &lt;code&gt;private&lt;/code&gt; properties are &lt;strong&gt;left out from the encoding&lt;/strong&gt;!&lt;/p&gt;&lt;h3 id=&quot;pay-attention-to-multidimensional-json-objects&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#pay-attention-to-multidimensional-json-objects&quot; aria-label=&quot;pay attention to multidimensional json objects permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Pay attention to multidimensional JSON objects&lt;/h3&gt;&lt;p&gt;On the other hand, when you decode a JSON string, PHP will return by default a standard class as seen in the snippet of code below.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-php&quot;&gt;PHP&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-php&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&amp;lt;?php&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$jsonString = &amp;#x27;{&amp;quot;Chunk Bytes&amp;quot;:true,&amp;quot;Awesome&amp;quot;:&amp;quot;Yes&amp;quot;}&amp;#x27;;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$result = json_decode($jsonString);&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;var_dump($result); // object(stdClass)#1 (2) { [&amp;quot;Chunk Bytes&amp;quot;]=&amp;gt; bool(true) [&amp;quot;Awesome&amp;quot;]=&amp;gt; string(3) &amp;quot;Yes&amp;quot; }&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;However, if you&amp;#x27;d like to &lt;strong&gt;convert it into an associative array&lt;/strong&gt; instead, simply add &lt;code&gt;true&lt;/code&gt; as a second parameter to the function as follows: &lt;code&gt;json_decode($jsonString, true);&lt;/code&gt;. Furthermore, for multidimensional JSON strings, you can limit 🛑 the depth of the decoding by indicating it as an additional parameter.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-php&quot;&gt;PHP&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-php&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&amp;lt;?php&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$jsonString = &amp;#x27;{&amp;quot;Chunk Bytes&amp;quot;:{&amp;quot;Awesome&amp;quot;:{&amp;quot;Yes&amp;quot;:true}}}&amp;#x27;;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$result = json_decode($jsonString, true, 2);&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;var_dump($result); // NULL&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Consequently, the function returns &lt;code&gt;NULL&lt;/code&gt; if the depth of the given JSON object is deeper than what has been indicated.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Transfer A Live WordPress Site To A Localhost]]></title><description><![CDATA[After your website has been deployed to a hosting server, it's not uncommon to keep making changes to your website. However, it's never a…]]></description><link>https://dilshankelsen.com/how-to-transfer-a-live-wordpress-site-to-a-localhost-manually/</link><guid isPermaLink="false">https://dilshankelsen.com/how-to-transfer-a-live-wordpress-site-to-a-localhost-manually/</guid><pubDate>Thu, 05 Sep 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;After your website has been deployed to a hosting server, it&amp;#x27;s not uncommon to keep making changes to your website. However, it&amp;#x27;s never a good idea to do so directly while visitors still have access to it, for obvious reasons... 😅 Instead, you&amp;#x27;re better off working on a staging or testing site from which you can eventually push your changes to your live site. In this tutorial, I&amp;#x27;ll be showing you how to transfer your live WordPress site to a localhost &lt;em&gt;manually&lt;/em&gt;. Not with a plugin, capiche?&lt;/p&gt;&lt;h2 id=&quot;setting-up-a-local-wordpress-site&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#setting-up-a-local-wordpress-site&quot; aria-label=&quot;setting up a local wordpress site permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Setting up a local WordPress site&lt;/h2&gt;&lt;p&gt;Before we get started with all the migrating goodness, we first need to prepare a local WordPress site, which we can copy everything to. I&amp;#x27;ve written &lt;a href=&quot;/how-to-install-wordpress-using-xampp/&quot;&gt;an article&lt;/a&gt; on how to do so using XAMPP, if you&amp;#x27;re interested. However, my preferred method of creating a local WordPress site is using &lt;a href=&quot;https://localbyflywheel.com/&quot; target=&quot;_blank&quot;&gt;Local By Flywheel&lt;/a&gt; (those guys really need come up with a better name 😐), a dev tool specifically for WordPress. It&amp;#x27;s easy and convenient to use, and is able to set up your site in a matter of seconds. But even better... it&amp;#x27;s free!&lt;/p&gt;&lt;p&gt;Once you&amp;#x27;ve set up your local WordPress site, it&amp;#x27;s time to start copying your live site! In this tutorial, I’ll be replicating all the steps with my website old WordPress site Chunk Bytes.&lt;/p&gt;&lt;h2 id=&quot;copying-your-live-website&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#copying-your-live-website&quot; aria-label=&quot;copying your live website permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Copying your live website&lt;/h2&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:425px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:175.99999999999997%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAjABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAQCAwH/xAAWAQEBAQAAAAAAAAAAAAAAAAACAwH/2gAMAwEAAhADEAAAAaNb1NxLClFuplIVzI+chU5hD//EAB0QAAIDAAIDAAAAAAAAAAAAAAABAgMREiEQEzL/2gAIAQEAAQUCVKiSgmvRWyHziNijjdE42Y6LGZ12OKXjWcmz/8QAGhEAAgIDAAAAAAAAAAAAAAAAAAECERAigf/aAAgBAwEBPwFRsscaWpweP//EABsRAAIBBQAAAAAAAAAAAAAAAAACMRAREiFB/9oACAECAQE/AbkmumTCxT//xAAfEAABAwMFAAAAAAAAAAAAAAAAAQIhEBExICIzYpH/2gAIAQEABj8C7FlOTwREzSTa+SHLcur5pnT/AP/EABwQAAICAwEBAAAAAAAAAAAAAAERACExQVFxkf/aAAgBAQABPyEtZbahQJg8MYNvElsEFqK1CeiAMAo9AEqP5m0oZFgLhJkhynNHbqmV3cF5gdr5CVZn/9oADAMBAAIAAwAAABDvKrAP/wD/xAAaEQEBAAIDAAAAAAAAAAAAAAABEQAQIUGh/9oACAEDAQE/EJLiBl91xG8jOuv/xAAbEQEAAwADAQAAAAAAAAAAAAABABEhQVFhof/aAAgBAgEBPxAfMLFh8Za8TqMmtQMn/8QAHxABAAICAgMBAQAAAAAAAAAAAQARITFBYVGBkXGh/9oACAEBAAE/ELCRIHTBrUMSm3R6auWylGkEHWbi48jY/fv2DFx851OeAJUs5tlmq4yZ9ReydikF1h5iQqCBunvuUBq5WjtjQDhEcWfyLFKSLee46O1Wf2WhaDyGXp2sHFE//9k=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Patrick meme from Spongebob Squarepants about simply moving a live WordPress site a localhost.&quot; title=&quot;Patrick meme from Spongebob Squarepants about simply moving a live WordPress site a localhost.&quot; src=&quot;/static/fb28639c41066dbb6b7a9f9be0782490/b9721/meme-patrick-push.jpg&quot; srcSet=&quot;/static/fb28639c41066dbb6b7a9f9be0782490/e07e9/meme-patrick-push.jpg 200w,/static/fb28639c41066dbb6b7a9f9be0782490/066f9/meme-patrick-push.jpg 400w,/static/fb28639c41066dbb6b7a9f9be0782490/b9721/meme-patrick-push.jpg 425w&quot; sizes=&quot;(max-width: 425px) 100vw, 425px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Patrick knows what&amp;#x27;s up! 😉&lt;/figcaption&gt;&lt;p&gt;In order to successfully replicate a WordPress website, whether it be locally or online, you will need three things:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;The website&amp;#x27;s database,&lt;/li&gt;&lt;li&gt;It&amp;#x27;s frontend and backend files (such as HTML, CSS and PHP), and&lt;/li&gt;&lt;li&gt;It&amp;#x27;s configuration file(s)&lt;/li&gt;&lt;/ul&gt;&lt;h3 id=&quot;exporting-the-database&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#exporting-the-database&quot; aria-label=&quot;exporting the database permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Exporting the database&lt;/h3&gt;&lt;p&gt;In your website&amp;#x27;s cPanel, head over to the &lt;em&gt;Databases&lt;/em&gt; section and click on &lt;em&gt;phpMyAdmin&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:51%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACCElEQVQozz2SyXriMBCEef9HmsBcMsksYTEGIxsjy7LlldVAWJJDTUlk5lBfyy259He3elXdYiXGqJSHosgxDUJM/Dk8f4ax53M9w+j1FT8HA/x6G+HPaIzfNg6ptyHebByOMZ36EEKgl2UaRVljuz+irCoYY7Db7XC/3/H5+cl4w+12w/l8xvv72eVP53dcLleXt7pcH+uu69AbT6fo9/sYjYbQOsVKZYhkijzPsZQJVG6gshyakmmOVKU8x7XSWIRLJPw2RYGEOc2zvclshicaBkEAY01oKGSG9bp1xgupmaO4ni8VIhGhKUtkPCtEiDiOUbCyut1gs9k8DAeD7/A8D5nWSLKCVCX2uy1WJFpaGkdnL9JYxRIViayhlBJpmqKuGzTtmoZrluz7+Pb0hOfnZ1dyyJ/8KHH9XKxS+LEipYJYSkyERDATKGmmaDSfBwjDkDMoUTVfhgETP15eMJlMOGWD1JQIYvalrEhrEGvjou1RlGSQJGy4l3N4llAphbpp0W62D8PD4eCmejh02G63EKtHr+q6RkiDOb8F+yfiBJ5IsJhbQuMIbd+jKEKqMxK29Nijdzqd8E8bGiapdsPI+JNMFElrll+j4gUFY0m6tmkc3WIhXMmahsYUD8OuO2K/3ztlX71piN8djzgwd+ZFl8sFV/vWqOvt+v/d3e4P2T2rj48P/AVfUtgTJwd9tAAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;A screenshot of a phpMyAdmin interface with a database selected.&quot; title=&quot;A screenshot of a phpMyAdmin interface with a database selected.&quot; src=&quot;/static/d56cd1a033f0951846708d680af4e18d/5a190/image-1.png&quot; srcSet=&quot;/static/d56cd1a033f0951846708d680af4e18d/772e8/image-1.png 200w,/static/d56cd1a033f0951846708d680af4e18d/e17e5/image-1.png 400w,/static/d56cd1a033f0951846708d680af4e18d/5a190/image-1.png 800w,/static/d56cd1a033f0951846708d680af4e18d/2bef9/image-1.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;phpMyAdmin interface&lt;/figcaption&gt;&lt;p&gt;Once you&amp;#x27;ve accessed the interface, you want to first search for the database related to your website in the sidebar on the left. In my case it&amp;#x27;s &lt;code&gt;chunkbyt_wp649&lt;/code&gt;. After you&amp;#x27;ve found it, select it and then click the &lt;em&gt;Export&lt;/em&gt; tab at the top. Next, make sure you&amp;#x27;ve got the &lt;em&gt;Quick&lt;/em&gt; 🏃‍♂️💨 export method selected and export your database by clicking on &lt;em&gt;Go&lt;/em&gt;.&lt;/p&gt;&lt;h3 id=&quot;copying-the-sites-content&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#copying-the-sites-content&quot; aria-label=&quot;copying the sites content permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Copying the site&amp;#x27;s content&lt;/h3&gt;&lt;p&gt;Next, we need to copy all the content that lives in the WordPress directory &lt;code&gt;wp-content&lt;/code&gt;. To do this, access the &lt;em&gt;File Manager&lt;/em&gt; under &lt;em&gt;Files&lt;/em&gt; in your cPanel. From the home directory, enter the &lt;code&gt;public_html&lt;/code&gt; directory and look for the &lt;code&gt;wp-content&lt;/code&gt; folder. In order to download the entire folder, you&amp;#x27;ll have to select it and click on &lt;em&gt;Compress&lt;/em&gt; at the top, as well as choose &lt;em&gt;Zip archive&lt;/em&gt; as the compression type afterwards.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:51%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAByElEQVQoz5WSX2vUQBTF820taPsg+KTvVaqrVrQqVCh9Unyy4IOCD34JRWp1l2STbDLJ5H8yyebnzexWqIjghcM5TCbnnjszzp0XJ9w+esmtJ8fcOHjEzr0ZO3cP2X1wxJ5g9/4zrh88ZW/23PK1/cfcPDxm//Qtx2cfefjmjFfvP3H64TOz1+9wdN3SDYZ+DabvyaqKWJeovCIpa1RWEmUFSVETi07ymqqHtO4JdIWqOhLRUdGSVAYnjFfMgxV+omnMQNcPzJcRbhCJcY7ShXBBlOZESWa1v1IsvICLucvc9fFk7/TdX8U4WRqwDJaUTcu4lpiMhNI5KTqk8VUMbNKVHYHKWcYZ0ZQyb8ibNalM4eQ6JPRdxhFbk2nwc05w8YMqimlUQh1v0FhWJK6Hf/6dxZeveN/OUQuXQqZMXBenLFK01mI4Yj2FPV+hYk1VNNRyRldQtqQqIwxiVlssvZA8q9ByJA5/1GQ8jREmuT2vsh0245pxA9HT5fhRahFIYyVmhezTcpGOTbbFVGsZuW2NZbs+8RbrLQ/yGnpjMKazfLluRP814aX5/9YwDJuEgzyVqVMvnbuuk4St1f+E6X//MyWbzCb9C/aE7pSjQs98AAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;A screenshot of the cPanel File Manager, showing the home directory.&quot; title=&quot;A screenshot of the cPanel File Manager, showing the home directory.&quot; src=&quot;/static/60f5ab37b70a647c3c26b741ecd8225d/5a190/image-2.png&quot; srcSet=&quot;/static/60f5ab37b70a647c3c26b741ecd8225d/772e8/image-2.png 200w,/static/60f5ab37b70a647c3c26b741ecd8225d/e17e5/image-2.png 400w,/static/60f5ab37b70a647c3c26b741ecd8225d/5a190/image-2.png 800w,/static/60f5ab37b70a647c3c26b741ecd8225d/2bef9/image-2.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;File manager interface&lt;/figcaption&gt;&lt;p&gt;Depending on the size of your folder, it may take a few minutes. ⏳ Of course, you&amp;#x27;ll have to download the zip file once it&amp;#x27;s done. After the download has finished, you may delete the &lt;code&gt;wp-content.zip&lt;/code&gt; file from the &lt;em&gt;File Manager&lt;/em&gt;.&lt;/p&gt;&lt;h3 id=&quot;copying-the-sites-configuration&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#copying-the-sites-configuration&quot; aria-label=&quot;copying the sites configuration permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Copying the site&amp;#x27;s configuration&lt;/h3&gt;&lt;p&gt;Last but not least, we also need to copy the site&amp;#x27;s configuration, which in this case lives in &lt;code&gt;wp-config.php&lt;/code&gt;. Same as before, head over to the &lt;code&gt;public_html&lt;/code&gt; directory and download the file to your computer.&lt;/p&gt;&lt;h2 id=&quot;importing-all-the-data-and-files&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#importing-all-the-data-and-files&quot; aria-label=&quot;importing all the data and files permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Importing all the data and files&lt;/h2&gt;&lt;p&gt;Awesome! We&amp;#x27;re almost halfway there! In this part of the tutorial, I&amp;#x27;ll show you now how to import all the data and files we&amp;#x27;ve just collected, starting off with the database. 🤓&lt;/p&gt;&lt;h3 id=&quot;importing-the-database&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#importing-the-database&quot; aria-label=&quot;importing the database permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Importing the database&lt;/h3&gt;&lt;p&gt;Head over to your local website&amp;#x27;s database administration panel. In most cases, that will be &lt;em&gt;phpMyAdmin&lt;/em&gt; again. However, in my case, Local By Flywheel uses a database manager called &lt;a href=&quot;https://www.adminer.org/&quot; target=&quot;_blank&quot;&gt;Adminer&lt;/a&gt;. Nevertheless, these steps won&amp;#x27;t differ for you, regardless of what you use.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:51%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABGklEQVQoz62QW0/DMAyF8///F0jAHpAQu5R1m0a7NWtKm95WkiYHp2WodNsLw9Iny459chLGOUcURUiTFDIvsI8avLwmeFsK6isIAcL+EMeXiSJD5wpMiBgyy6hIUVcVRJhg8eTBe15hvS4RhppQCALVZc41DodzXD/PDRjnBkUByMLCWKCWEqvpAv5sCc8LaDAlBxLbLacFDUszgL0KM6TisKZvHBuNh8d33N2HmEw45nNJzkrs9wl2uxoNnXeSpOzExzB8h+2vpoWGhGL4vsBmk2E2PXaCZVmRwxxKqYHgOWxYuHALifhAVX1Ca4u2Nehf0efT3CmPgw1v+z14eWE4M3bXCV6z/lcY/jk6h23b0n/pm3B/7bS+AAJeCwsJuOZhAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;A screenshot of adminer, a database manager, of a local WordPress site. It shows an overview of all the databases.&quot; title=&quot;A screenshot of adminer, a database manager, of a local WordPress site. It shows an overview of all the databases.&quot; src=&quot;/static/c5c2be08d006c40b1b8d7fba42a04225/5a190/image-3.png&quot; srcSet=&quot;/static/c5c2be08d006c40b1b8d7fba42a04225/772e8/image-3.png 200w,/static/c5c2be08d006c40b1b8d7fba42a04225/e17e5/image-3.png 400w,/static/c5c2be08d006c40b1b8d7fba42a04225/5a190/image-3.png 800w,/static/c5c2be08d006c40b1b8d7fba42a04225/2bef9/image-3.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Adminer interface&lt;/figcaption&gt;&lt;p&gt;First, you need to create a new database. You may name it the same as the original one or give it a completely new one. Frankly, it won&amp;#x27;t affect the process. After that, with your new database selected, import your &lt;code&gt;sql&lt;/code&gt; file that we&amp;#x27;ve exported from the previous section.&lt;/p&gt;&lt;p&gt;Before moving on, do make sure that a database user 👩‍💼 exists who has access and all the privileges to modify your newly created database. In particular, make sure to keep their name and password in mind. If you don&amp;#x27;t have one, be sure to create one!&lt;/p&gt;&lt;h3 id=&quot;moving-wp-content-to-your-localhost&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#moving-wp-content-to-your-localhost&quot; aria-label=&quot;moving wp content to your localhost permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Moving wp-content to your localhost&lt;/h3&gt;&lt;p&gt;Next up, unzip your &lt;code&gt;wp-content.zip&lt;/code&gt; file and move it to your local WordPress site&amp;#x27;s directory that contains its own &lt;code&gt;wp-content&lt;/code&gt; folder. In this case, you may rename the original folder (e.g. &lt;code&gt;old-wp-content&lt;/code&gt;) if you got content you want to keep, so it won&amp;#x27;t get deleted. However, you can also have it replaced 🗑 by your new folder if you&amp;#x27;d like to. I&amp;#x27;ve opted for the latter.&lt;/p&gt;&lt;h3 id=&quot;moving-wp-configphp-to-your-localhost&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#moving-wp-configphp-to-your-localhost&quot; aria-label=&quot;moving wp configphp to your localhost permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Moving wp-config.php to your localhost&lt;/h3&gt;&lt;p&gt;Just as before, move the live site&amp;#x27;s &lt;code&gt;wp-config.php&lt;/code&gt; file to your local site&amp;#x27;s directory, replacing the old one. Now, you may be tempted to leave it at that, but we still need to configure the file. If you actually try to access your site now, you will be greeted with &lt;code&gt;Error establishing a database connection&lt;/code&gt;. 🚨 And why is that the case?&lt;/p&gt;&lt;p&gt;Simply, it has the wrong credentials to access your database, namely still those from your live site. That&amp;#x27;s why it fails to connect to your database. On top of that, if you decided to give your database a different name for your local site, you will have to adjust that in your configurations too. ☝️&lt;/p&gt;&lt;p&gt;Alright, for this step you&amp;#x27;ll want to open your local &lt;code&gt;wp-config.php&lt;/code&gt; file in a text editor of your choice and change the following lines:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-php&quot;&gt;PHP&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-php&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;/** The name of the database for WordPress */&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;define(&amp;#x27;DB_NAME&amp;#x27;, &amp;#x27;chunkbytes&amp;#x27;);&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;/** MySQL database username */&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;define(&amp;#x27;DB_USER&amp;#x27;, &amp;#x27;admin&amp;#x27;);&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;/** MySQL database password */&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;define(&amp;#x27;DB_PASSWORD&amp;#x27;, &amp;#x27;.........&amp;#x27;);&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Make sure the right name of the database is written down, as well as the username and password of the user who has access to it (remember what I asked you to keep in mind? 😜). Once you&amp;#x27;ve done that... we&amp;#x27;re still not completely finished!&lt;/p&gt;&lt;h3 id=&quot;changing-the-urls&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#changing-the-urls&quot; aria-label=&quot;changing the urls permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Changing the URLs&lt;/h3&gt;&lt;p&gt;While we may have successfully migrated the website to a localhost, there&amp;#x27;s still one tinsy winsy problem: our database still contains the URLs to our live website. This may cause some unexpected problems, so it&amp;#x27;s best to change them right away! 🧐&lt;/p&gt;&lt;p&gt;Luckily, I&amp;#x27;ve come across a tool from &lt;a href=&quot;https://interconnectit.com/products/search-and-replace-for-wordpress-databases/&quot; target=&quot;_blank&quot;&gt;Interconnect IT&lt;/a&gt; that will make this easy for us. Go ahead and download it, and then unzip the file. The creators recommend to obfuscate the name of the folder by giving it a different name. In my case, I&amp;#x27;ll rename it to &lt;code&gt;chunk_sr&lt;/code&gt;. After you&amp;#x27;ve done that, head over again to your local website&amp;#x27;s directory (where &lt;code&gt;wp-content&lt;/code&gt; and &lt;code&gt;wp-config.php&lt;/code&gt; are located), and move the folder there.&lt;/p&gt;&lt;p&gt;In order to run the search and replace tool, you&amp;#x27;ll need to navigate to it from your website. In my case, this would be the address &lt;code&gt;http://chunkbytes.local/chunk_sr/&lt;/code&gt;. 😁 If you&amp;#x27;re not using Local By Flywheel, your address may be something like &lt;code&gt;http://localhost:8080/{website_name}/{tool_name}/&lt;/code&gt;. Upon accessing it, you&amp;#x27;ll be greeted by a lovely screen.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:51%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAABYlAAAWJQFJUiTwAAABp0lEQVQoz1VQu47UMBT1r9FQUtDQ8wtsCSV0UFEhbbNbrPgAikFi0IqsNNIomkde5DmZOE7i2Ik9G0sD3Z4kWtAeWSf3+p5z743Jer2+s6zb5XKxWNi2vVqtfN/P8/x4PFZVhWCz2aRpmmUZpZQx5nmeZVnb7RYy4s5wnP1+jwKkYRjGcQyGB4zinEZRlCQJNLvdDpdBEBDPdTENnZIJQeBDhyG+71V1fTzETZkqpZM4hq0sS3jAdV2jI8E+81aHCdkBm+ZIwWiB5WlRICieAlWIR7PjOMjLEcwPwt++57tOEkVZHJeUYsjY9HEGGB1xCQvBGyACj15Ky1awXnGtudK10rLrBOdYAKeYVsAOnPO2beEic+OmaaQQouubXz+bxbfmdjkGP763lIrByGGQ4F5BIyfArJQi+GAkuJOyM4a+u8hfPCvevslfv8pfPmdfPtfXl+zje/bpA9/YUp/++bXW5DThfoYxfeD1W1s3tTpkKkv6NOnDoHd3OLiE7vSIYRgIpuPpwAbZn7/tzRX/es1P97UQrBV5ySohx//vlR7+wxhzPp8fABnzB7Aub1b6AAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;A screenshot of the search and replace tool by interconnect/it&quot; title=&quot;A screenshot of the search and replace tool by interconnect/it&quot; src=&quot;/static/a18d34f179972a7f730cc98d826976a1/5a190/image-4.png&quot; srcSet=&quot;/static/a18d34f179972a7f730cc98d826976a1/772e8/image-4.png 200w,/static/a18d34f179972a7f730cc98d826976a1/e17e5/image-4.png 400w,/static/a18d34f179972a7f730cc98d826976a1/5a190/image-4.png 800w,/static/a18d34f179972a7f730cc98d826976a1/8ea22/image-4.png 1023w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Interface of search and replace tool by Interconnect IT&lt;/figcaption&gt;&lt;p&gt;Fortunately, the tool automatically retrieves the database credentials from the &lt;code&gt;wp-config.php&lt;/code&gt; file, so there&amp;#x27;s only a few things left for us to do. First of all, in the &lt;code&gt;search for...&lt;/code&gt; field, you want to &lt;strong&gt;enter the full url&lt;/strong&gt; of your live website. In my case, that&amp;#x27;s &lt;code&gt;http://chunkbytes.com&lt;/code&gt;. In the &lt;code&gt;replace with...&lt;/code&gt; field, you want to &lt;strong&gt;enter the full url&lt;/strong&gt; of your local website. For me that is, &lt;code&gt;http://chunkbytes.local&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;In the actions, section you may then click on &lt;code&gt;dry run&lt;/code&gt; to see it emulate the changes. This doesn&amp;#x27;t actually replace any of the URLs yet. Instead, to have it actually make changes to your database, you&amp;#x27;ll need to click on &lt;code&gt;live run&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Et voilà! You&amp;#x27;ve done it! Now you know how to successfully transfer a live WordPress site to your localhost! 😉&lt;/p&gt;&lt;h2 id=&quot;houston-we-have-a-problem&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#houston-we-have-a-problem&quot; aria-label=&quot;houston we have a problem permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Houston, we have a problem&lt;/h2&gt;&lt;p&gt;Don&amp;#x27;t worry! If you&amp;#x27;ve followed along and encountered no problems, you&amp;#x27;re good to go. However, I encountered a peculiar problem after I migrated Chunk Bytes to my computer. Whenever I tried to access my local site, I would be redirected to my live site... 😫&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:576px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:56.49999999999999%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAMFAQT/xAAVAQEBAAAAAAAAAAAAAAAAAAABAv/aAAwDAQACEAMQAAABj9i2lTzRn//EABsQAAIBBQAAAAAAAAAAAAAAAAEDAAIREiMx/9oACAEBAAEFAsDdYAXO0sGmf//EABURAQEAAAAAAAAAAAAAAAAAAAEQ/9oACAEDAQE/ASf/xAAVEQEBAAAAAAAAAAAAAAAAAAABEP/aAAgBAgEBPwFn/8QAHBAAAQMFAAAAAAAAAAAAAAAAAQAQEQIDIWFx/9oACAEBAAY/AoGeKomGMq2dN//EABwQAAMAAQUAAAAAAAAAAAAAAAABITEQEUFRof/aAAgBAQABPyHCPYSTuS5HkVNViuxGn//aAAwDAQACAAMAAAAQ1x//xAAWEQADAAAAAAAAAAAAAAAAAAAQESH/2gAIAQMBAT8Qlj//xAAVEQEBAAAAAAAAAAAAAAAAAAAQQf/aAAgBAgEBPxCD/8QAGxABAAMAAwEAAAAAAAAAAAAAAQARITFBUXH/2gAIAQEAAT8Qsdsd5Pxl6oACWPWs2mqmYCdE8So+l7B5LY8z/9k=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Meme with Kevin Hart staring with disbelieve.&quot; title=&quot;Meme with Kevin Hart staring with disbelieve.&quot; src=&quot;/static/29a209c116e751bf44b21601070d6400/ac097/meme-stare.jpg&quot; srcSet=&quot;/static/29a209c116e751bf44b21601070d6400/e07e9/meme-stare.jpg 200w,/static/29a209c116e751bf44b21601070d6400/066f9/meme-stare.jpg 400w,/static/29a209c116e751bf44b21601070d6400/ac097/meme-stare.jpg 576w&quot; sizes=&quot;(max-width: 576px) 100vw, 576px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;WTF is going on?! What&amp;#x27;s this black magic?!&lt;/figcaption&gt;&lt;p&gt;Keep in mind that I already had changed the URLs, so faulty links couldn&amp;#x27;t be the problem. As it turned out, my default browser (i.e. Chrome) had cached my live site due to a plugin of mine, causing it to redirect me to it whenever I tried to access &lt;code&gt;chunkbytes.local&lt;/code&gt;. The solution? Delete the cache or use a different browser.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Create A Navbar With Bootstrap 4 In WordPress]]></title><description><![CDATA[The navigation bar at the top of a website is arguably the most important element of a website. And with Bootstrap, you can create a…]]></description><link>https://dilshankelsen.com/how-to-create-a-navbar-with-bootstrap-4-in-wordpress/</link><guid isPermaLink="false">https://dilshankelsen.com/how-to-create-a-navbar-with-bootstrap-4-in-wordpress/</guid><pubDate>Sat, 27 Jul 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The navigation bar at the top of a website is arguably the most important element of a website. And with Bootstrap, you can create a responsive and good looking navbar within minutes without breaking a sweat. However, if you&amp;#x27;re using WordPress in your backend, it can be a bit tricky to integrate the Bootstrap navbar into it. In this article, I&amp;#x27;ll show you how to create a navbar with Bootstrap 4 in WordPress in a few easy steps. 🤓&lt;/p&gt;&lt;p&gt;Need to install WordPress first? Check out &lt;a href=&quot;/how-to-install-wordpress-using-xampp/&quot;&gt;my tutorial&lt;/a&gt; using XAMPP!&lt;/p&gt;&lt;h2 id=&quot;integration-with-bootstrap-3&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#integration-with-bootstrap-3&quot; aria-label=&quot;integration with bootstrap 3 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Integration with Bootstrap 3&lt;/h2&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:340px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:81%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAQABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIDBf/EABUBAQEAAAAAAAAAAAAAAAAAAAEC/9oADAMBAAIQAxAAAAFh6QzNgo//xAAbEAEAAgIDAAAAAAAAAAAAAAACAAEDERIhMf/aAAgBAQABBQLcPcVI2jUxlc68/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFREBAQAAAAAAAAAAAAAAAAAAEDH/2gAIAQIBAT8Bp//EABwQAAEDBQAAAAAAAAAAAAAAABEAAhABITFBkf/aAAgBAQAGPwK6PJa80GzmP//EAB0QAQACAgIDAAAAAAAAAAAAAAEAESExQWFRceH/2gAIAQEAAT8hdlGInINPDz67hwR1xFU5rmpSFBw2fZmFK6Z//9oADAMBAAIAAwAAABAAL//EABYRAQEBAAAAAAAAAAAAAAAAACEBEP/aAAgBAwEBPxAjn//EABcRAAMBAAAAAAAAAAAAAAAAAAABESH/2gAIAQIBAT8QdwiH/8QAHBABAAICAwEAAAAAAAAAAAAAAREhADFBUWGB/9oACAEBAAE/EGExyZoMmUQQIkpE7LodYqaobKHmKWCyFeqT0b+GShXCZAImjZyCiknkM//Z&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Bootstrap 4 To 3 Meme&quot; title=&quot;Bootstrap 4 To 3 Meme&quot; src=&quot;/static/f257ff0880d311d0d284dce1ee1a97ea/d30a3/meme-bootstrap.jpg&quot; srcSet=&quot;/static/f257ff0880d311d0d284dce1ee1a97ea/e07e9/meme-bootstrap.jpg 200w,/static/f257ff0880d311d0d284dce1ee1a97ea/d30a3/meme-bootstrap.jpg 340w&quot; sizes=&quot;(max-width: 340px) 100vw, 340px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;I can explain! 😅&lt;/figcaption&gt;&lt;p&gt;To understand what we&amp;#x27;re doing in detail, we first need to go back in time to the days of Bootstrap 3... 👀 As you may or may not know, a WordPress menu is generated on your web page using the &lt;a href=&quot;https://developer.wordpress.org/reference/functions/wp_nav_menu/&quot; target=&quot;_blank&quot;&gt;built in &lt;code&gt;wp_nav_menu()&lt;/code&gt; function&lt;/a&gt;. On its own, you simply have an unordered list. However, the function offers a wide array of parameters that the user can employ to customise the menu, like &lt;code&gt;theme_location&lt;/code&gt; and &lt;code&gt;item_spacing&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Furthermore, the real power of this function lies in its ability to let developers add custom classes and ids to the generated WordPress menu. 💪 Naturally, you could easily integrate Bootstrap 3 formatting into your navbar by adding the necessary classes and id attributes. Unfortunately, things aren&amp;#x27;t as straightforward with Bootstrap 4.&lt;/p&gt;&lt;h2 id=&quot;changes-in-bootstrap-4&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#changes-in-bootstrap-4&quot; aria-label=&quot;changes in bootstrap 4 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Changes in Bootstrap 4&lt;/h2&gt;&lt;p&gt;When the 4th iteration of the UI framework was released, the formatting of the navbars was modified. The main difference? Now the menu list itself also had Bootstrap specific classes and ids, which was previously not the case. In the 3rd version, only the &lt;code&gt;div&lt;/code&gt;s surrounding the unordered menu list required classes... So what&amp;#x27;s the big deal, &lt;em&gt;you may ask&lt;/em&gt;? 🤔&lt;/p&gt;&lt;p&gt;Well, the &lt;code&gt;wp_nav_menu()&lt;/code&gt; function only allows for custom attributes in the tags surrounding the menu list. Not for the menu itself. 😱 Fortunately, &lt;code&gt;wp_nav_menu()&lt;/code&gt; allows for much deeper customisation by inserting a custom &lt;a href=&quot;https://codex.wordpress.org/Class_Reference/Walker&quot; target=&quot;_blank&quot;&gt;walker class&lt;/a&gt; (i.e. an object) as a parameter. And even better, someone already created a solution for our problem using this class, specifically for Bootstrap!&lt;/p&gt;&lt;h2 id=&quot;installing-wp-bootstrap-navwalker&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#installing-wp-bootstrap-navwalker&quot; aria-label=&quot;installing wp bootstrap navwalker permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Installing WP Bootstrap Navwalker&lt;/h2&gt;&lt;p&gt;The ready made solution I&amp;#x27;m talking about is &lt;a href=&quot;https://github.com/wp-bootstrap/wp-bootstrap-navwalker&quot; target=&quot;_blank&quot;&gt;WP Bootstrap Navwalker&lt;/a&gt;. It&amp;#x27;s fully compliant with all Theme Review guidelines for wordpress.org submission. 😎 So how do we go about installing this baby?&lt;/p&gt;&lt;span class=&quot;text-center&quot;&gt;&lt;p&gt;&lt;img src=&quot;/static/installation-6df96996a831b1e2bf84182b80ae43c2.gif&quot; alt=&quot;Bruce Almighty Bootstrap Nav Walker Installation GIF&quot;/&gt;&lt;/p&gt;&lt;/span&gt;&lt;figcaption&gt;Time to get to work!&lt;/figcaption&gt;&lt;p&gt;First of all, download the zip file from its Github page. After you&amp;#x27;ve extracted the folder from it, open your &lt;code&gt;functions.php&lt;/code&gt; file in your website folder and insert the following line in it:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-php&quot;&gt;PHP&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-php&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;require_once get_template_directory() . &amp;#x27;/class-wp-bootstrap-navwalker.php&amp;#x27;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;With this line you&amp;#x27;re registering WP Bootstrap Navwalker in your project. Next, go to your PHP file containing the &lt;code&gt;wp_nav_menu()&lt;/code&gt;, usually in &lt;code&gt;header.php&lt;/code&gt;, and make the following changes to it:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;php&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-php&quot;&gt;PHP&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-php&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;wp_nav_menu( array(&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &amp;#x27;theme_location&amp;#x27;  =&amp;gt; &amp;#x27;primary&amp;#x27;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &amp;#x27;depth&amp;#x27;           =&amp;gt; 2, // 1 = no dropdowns, 2 = with dropdowns.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &amp;#x27;container&amp;#x27;       =&amp;gt; &amp;#x27;div&amp;#x27;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &amp;#x27;container_class&amp;#x27; =&amp;gt; &amp;#x27;collapse navbar-collapse&amp;#x27;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &amp;#x27;container_id&amp;#x27;    =&amp;gt; &amp;#x27;bs-example-navbar-collapse-1&amp;#x27;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &amp;#x27;menu_class&amp;#x27;      =&amp;gt; &amp;#x27;navbar-nav mr-auto&amp;#x27;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &amp;#x27;fallback_cb&amp;#x27;     =&amp;gt; &amp;#x27;WP_Bootstrap_Navwalker::fallback&amp;#x27;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &amp;#x27;walker&amp;#x27;          =&amp;gt; new WP_Bootstrap_Navwalker(),&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;) );&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Et voilà! The deed has been done. 😁 As you can see above, we inserted the WP Bootstrap Navwalker class at the very end. We even have a navbar to fall back on in case it fails to load. Lastly, make sure that the &lt;code&gt;container_id&lt;/code&gt; value matches the value for &lt;code&gt;data_target&lt;/code&gt; in the surrounding &lt;code&gt;div&lt;/code&gt; tag. Otherwise, you mobile navbar may not expand at all.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Query Elasticsearch Using JavaScript]]></title><description><![CDATA[Elasticsearch is an amazing open-source tool to store, search and analyse data. With an emphasis on  search . Fortunately, this query…]]></description><link>https://dilshankelsen.com/how-to-query-elasticsearch-using-javascript/</link><guid isPermaLink="false">https://dilshankelsen.com/how-to-query-elasticsearch-using-javascript/</guid><pubDate>Tue, 02 Jul 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Elasticsearch is an amazing open-source tool to store, search and analyse data. With an emphasis on &lt;em&gt;search&lt;/em&gt;. Fortunately, this query goodness isn&amp;#x27;t restricted to Kibana alone and its dev tools section. Elastic, the company behind this search engine, has also provided us with a feature packed NodeJS API client, allowing you to query directly from any backend application. There are also several &lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/client/index.html&quot; target=&quot;_blank&quot;&gt;official Elasticsearch clients&lt;/a&gt; as well as &lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/client/community/current/index.html&quot; target=&quot;_blank&quot;&gt;community contributed clients&lt;/a&gt; for other programming languages. However, in this article, I&amp;#x27;ll be showing you specifically how to query Elasticsearch using JavaScript! So let&amp;#x27;s get started, shall we?! 😆&lt;/p&gt;&lt;h2 id=&quot;lets-talk-about-kibana&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#lets-talk-about-kibana&quot; aria-label=&quot;lets talk about kibana permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Let&amp;#x27;s talk about Kibana&lt;/h2&gt;&lt;p&gt;When it comes to using Elasticsearch, Kibana is a natural choice as an analytics and visualisation tool. It&amp;#x27;s like the Robin to Batman. 🦇 Moreover, should you ever find it lacking certain features you need, you can always fill that gap by creating your own plugin. I also have a &lt;a href=&quot;/how-to-create-a-plugin-for-kibana/&quot;&gt;step-by-step tutorial&lt;/a&gt; on how to create a Kibana plugin, in case you&amp;#x27;re interested! Each plugin automatically comes equipped with NodeJS on the backend, with &lt;a href=&quot;https://hapijs.com/&quot; target=&quot;_blank&quot;&gt;HapiJS&lt;/a&gt; as the framework.&lt;/p&gt;&lt;p&gt;Kibana plugins are probably the most common place for needing the API client to query Elasticsearch. However, what you&amp;#x27;re about to read can be done anywhere, even in a simple script file! 😋&lt;/p&gt;&lt;h2 id=&quot;installation&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#installation&quot; aria-label=&quot;installation permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Installation&lt;/h2&gt;&lt;p&gt;To install the JavaScript API client, simply run one of the commands below. Keep note that the library requires you to have at least &lt;code&gt;v8&lt;/code&gt; of NodeJS running. Moreover, it&amp;#x27;s best that you install the same major client version as Elasticsearch. For instance, if you&amp;#x27;re working with Elasticsearch 7, you should install version 7 of the NodeJS API client.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; @elastic/elasticsearch@7&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;yarn&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; @elastic/elasticsearch@7&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Next, you want to import the library into a file of your choice and instantiate a new client with which you can start querying Elasticsearch. Make sure it is observing the &lt;em&gt;right&lt;/em&gt; port number upon instantiating! 👏&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;Client&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;@elastic/elasticsearch&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; client &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Client&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;http://localhost:9200&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;configuration&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#configuration&quot; aria-label=&quot;configuration permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Configuration&lt;/h3&gt;&lt;p&gt;Before moving on, the default configuration should suffice for most people. However, if you do need to configure your client, you can get further details on that right &lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/client-configuration.html&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;!&lt;/p&gt;&lt;h2 id=&quot;querying-elasticsearch&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#querying-elasticsearch&quot; aria-label=&quot;querying elasticsearch permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Querying Elasticsearch&lt;/h2&gt;&lt;p&gt;To search indices in Elasticsearch, you will be using the &lt;code&gt;search&lt;/code&gt; function, which allows you to use the Elasticsearch Query DSL.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;search&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;kibana_sample_data_ecommerce&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token string-property property&quot;&gt;&amp;quot;query&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token string-property property&quot;&gt;&amp;quot;match_all&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token string-property property&quot;&gt;&amp;quot;size&amp;quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;catch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;search&lt;/code&gt; function receives at minimum an object with two properties: &lt;code&gt;index&lt;/code&gt; and &lt;code&gt;body&lt;/code&gt;. The former shows which index you would like to query, while the latter contains the instructions of your query. Afterwards, the function returns a promise for the results. Finally, the &lt;code&gt;body&lt;/code&gt; property of the returned object contains the results of your query.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; object &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;statusCode&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;headers&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; object&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;warnings&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;meta&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; object &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;endless-possibilites&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#endless-possibilites&quot; aria-label=&quot;endless possibilites permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Endless possibilites&lt;/h2&gt;&lt;p&gt;Querying Elasticsearch might only represent a fraction of what you might need to do. The client has an &lt;em&gt;overwhelming&lt;/em&gt; 😱 amount of &lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html&quot; target=&quot;_blank&quot;&gt;other APIs&lt;/a&gt; you might want to consider, such as for creating new indices, adding and removing documents, etc.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Use URL Parameters In React Router]]></title><description><![CDATA[When it comes to routing in any type of application, you will always come across two types: static and dynamic routing. Static routes are…]]></description><link>https://dilshankelsen.com/how-to-use-url-parameters-in-react-router/</link><guid isPermaLink="false">https://dilshankelsen.com/how-to-use-url-parameters-in-react-router/</guid><pubDate>Tue, 18 Jun 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;When it comes to routing in any type of application, you will always come across two types: static and dynamic routing. Static routes are what most people are familiar with when they start out. These are predetermined routes such as &lt;code&gt;/home&lt;/code&gt; or &lt;code&gt;/about/history&lt;/code&gt;. On the other hand, your dynamic routes are determined as your app is rendering. This provides a number of advantages over static routing, which I will get into later on. Moreover, this tutorial will be specifically focusing on &lt;strong&gt;responsive routing&lt;/strong&gt;, that is, how to use URL parameters in React Router.&lt;/p&gt;&lt;p&gt;New to React Router and need an introduction instead? Check out my article &lt;a href=&quot;/an-introduction-to-react-router/&quot;&gt;here&lt;/a&gt; to get started!&lt;/p&gt;&lt;h2 id=&quot;url-parameters-explained&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#url-parameters-explained&quot; aria-label=&quot;url parameters explained permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;URL parameters explained&lt;/h2&gt;&lt;p&gt;So what are URL or route parameters exactly? Well, let&amp;#x27;s imagine you are selling products on your website and each product has its own product page. How would go about programming this? Hard-code a product page for each product? Well, maybe if you have 1 or 2, but imagine you have 100 or more... that might not be the best idea. 😰&lt;/p&gt;&lt;p&gt;Rather, you should keep things DRY (Don&amp;#x27;t Repeat Yourself) and create a single product page that changes its displayed information depending on which product a user is currently looking at. And how would we do this? Well, with the use dynamic routing, i.e. responsive routing!&lt;/p&gt;&lt;span class=&quot;text-center&quot;&gt;&lt;p&gt;&lt;img src=&quot;/static/see-coming-2ddda095f67f9f3dad9114f214fbbc0a.gif&quot; alt=&quot;Dynamic routing tutorial is about dynamic routing. Who would have thought? &quot;/&gt;&lt;/p&gt;&lt;/span&gt;&lt;p&gt;Responsive routing let&amp;#x27;s us use variables in our routes that can take on any kind &lt;code&gt;string&lt;/code&gt; value. In our responsive route, we distinguish a route parameter or variable by placing &lt;code&gt;:&lt;/code&gt; in front of it. For instance, the dynamic route &lt;code&gt;/product/:id&lt;/code&gt; can now take the values &lt;code&gt;/product/1&lt;/code&gt; or &lt;code&gt;/product/laptop_4&lt;/code&gt;. From these routes, we could then extract the product ID, retrieve any data related to that id from a database and consequently display it on our page.&lt;/p&gt;&lt;p&gt;On a side note: the end of a route parameter can be marked by a &lt;code&gt;/&lt;/code&gt;. Thus, &lt;code&gt;id&lt;/code&gt; would be valid parameter in &lt;code&gt;/product/:id/price&lt;/code&gt;&lt;/p&gt;&lt;h2 id=&quot;setting-up-responsive-routes&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#setting-up-responsive-routes&quot; aria-label=&quot;setting up responsive routes permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Setting up responsive routes&lt;/h2&gt;&lt;p&gt;Alright, it&amp;#x27;s time to code ladies and gents! 👩‍💻Below we have a small example of 2 routes with corresponding components. Nothing fancy here...&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;react&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;Route&lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;NavLink&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;react-router-dom&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function maybe-class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;This is our lovely product page!&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function maybe-class-name&quot;&gt;HomePage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Welcome to Chunk Bytes!&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function maybe-class-name&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;nav&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;ul&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;NavLink&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Home&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;NavLink&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;NavLink&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;/product&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;NavLink&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;ul&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;nav&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Route&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;exact&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;component&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript maybe-class-name&quot;&gt;HomePage&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Route&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;/product&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;component&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript maybe-class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Let&amp;#x27;s make our product page a bit more dynamic with the following changes. First, let&amp;#x27;s add a parameter to our product route with &lt;code&gt;path=&amp;quot;/product/:id&amp;quot;&lt;/code&gt;. Next, let&amp;#x27;s create two &lt;code&gt;NavLink&lt;/code&gt;s, each pointing to two different products.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Removing the regular product page&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;NavLink&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;/product&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;NavLink&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Adding two new NavLinks&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;NavLink&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;/product/laptop1&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Laptop&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;NavLink&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;NavLink&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;/product/car1&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;NavLink&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;li&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;With these simple steps, we already created a responsive route. Hooray! 😋 But of course, we&amp;#x27;re not done yet! 😤 Next, we need to extract our product IDs from the route, so our &lt;code&gt;Product&lt;/code&gt; component knows which product information to retrieve and render. So how do we retrieve this information?&lt;/p&gt;&lt;h2 id=&quot;retrieving-url-parameter-information&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#retrieving-url-parameter-information&quot; aria-label=&quot;retrieving url parameter information permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Retrieving URL parameter information&lt;/h2&gt;&lt;p&gt;We all know that React components automatically receive a &lt;code&gt;props&lt;/code&gt; parameter (I hope you do... 😳). This is also where we can find our route parameter value. Let&amp;#x27;s &lt;code&gt;console.log&lt;/code&gt; our component&amp;#x27;s &lt;code&gt;props&lt;/code&gt; and see what it has to offer.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:48%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAABYlAAAWJQFJUiTwAAABfUlEQVQoz5WSi26DMAxF+//ft61badVCKI8kQJ4MCHDn0G2qplXaLFlOgnO4dryb5xnjOCLGdV0pBqzL7Sx6COHXuCwLrLU4nk5gjEEIgWi7eJizHMfkiDxnOF9SFMKibRtKamCMhTIGmpzXNeznOt6TBOGcoygKym9vwECKdK0hEokmbSlB4HBKCcYh+QjdGTRlCSUlAgH96x6j0ZC0r6oKSik4gnvvb8CZymtZB/4sYa70wXmClijLghRxjFrBpyl6KmskYH85b+ueIBHYNC16reFpvwGXdYHvenRnBcMtwjDDuBVTwL8tvsEuLkKYUO85+JNEK1qwvIaj+E5qPMswfJaz0kPESw89KoyJy7yAvwioSsNdc7j9C6YshT8cYNML5hC+Fdz7vbKv/Q1If3bCwym39UkdE9SHNzhSN1Evpy+Fd5BHtgG9oh5mCjozUJlDR2NzpYZX9OK27zHQ3P1U8hAYB9Urv8FMYWGlRfKagGUZxmHYPA79XxV+AMkWBHntcen1AAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Console Output Of Component Props&quot; title=&quot;Console Output Of Component Props&quot; src=&quot;/static/0b35c5670ffb814e0d399348091bbc5b/5a190/image-1.png&quot; srcSet=&quot;/static/0b35c5670ffb814e0d399348091bbc5b/772e8/image-1.png 200w,/static/0b35c5670ffb814e0d399348091bbc5b/e17e5/image-1.png 400w,/static/0b35c5670ffb814e0d399348091bbc5b/5a190/image-1.png 800w,/static/0b35c5670ffb814e0d399348091bbc5b/33e10/image-1.png 844w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Console Output Of Component Props&lt;/figcaption&gt;&lt;p&gt;As we can see, &lt;code&gt;props&lt;/code&gt; has several properties. However, there&amp;#x27;s one in particular that is of interest to us: &lt;code&gt;match&lt;/code&gt;. This in turn has another property called &lt;code&gt;params&lt;/code&gt; that actually contains the value we are looking for. Alright! So let&amp;#x27;s make some final changes to our code before we wrap things up.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function maybe-class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; match &lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      This is our lovely product page!&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      You are currently viewing &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;match&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Instead accessing our route parameter value with &lt;code&gt;props.match.params.id&lt;/code&gt;, we can do some &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment&quot; target=&quot;_blank&quot;&gt;destructuring&lt;/a&gt; with &lt;code&gt;{ match }&lt;/code&gt;. You could even destructure a layer deeper to keep things simpler, e.g. &lt;code&gt;{ match: { params }}&lt;/code&gt;. Destructur-...ception? 🤨&lt;/p&gt;&lt;h2 id=&quot;a-teeny-tiny-gotcha&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#a-teeny-tiny-gotcha&quot; aria-label=&quot;a teeny tiny gotcha permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;A teeny-tiny gotcha&lt;/h2&gt;&lt;p&gt;If you&amp;#x27;re rendering your component in a route using the &lt;code&gt;render&lt;/code&gt; props, e.g. &lt;code&gt;&amp;lt;Route path=&amp;quot;/product/:id&amp;quot; render={() =&amp;gt; &amp;lt;Product /&amp;gt;} /&amp;gt;&lt;/code&gt;, make sure to actually pass the &lt;code&gt;props&lt;/code&gt; into your component as follows: &lt;code&gt;render={(props) =&amp;gt; &amp;lt;Product {...props} /&amp;gt;} /&amp;gt;&lt;/code&gt;. I&amp;#x27;ve done this mistake myself 😬 and I&amp;#x27;ve seen many others do it. If you don&amp;#x27;t, you will see errors such as &lt;code&gt;TypeError: match.params.id is undefined&lt;/code&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[An Introduction To React Router - Routing Basics]]></title><description><![CDATA[A nice perk of React being solely a library, as opposed to a complete JavaScript framework, is its learning curve. Unlike Angular, you aren…]]></description><link>https://dilshankelsen.com/an-introduction-to-react-router/</link><guid isPermaLink="false">https://dilshankelsen.com/an-introduction-to-react-router/</guid><pubDate>Tue, 04 Jun 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A nice perk of React being solely a library, as opposed to a complete JavaScript framework, is its learning curve. Unlike Angular, you aren&amp;#x27;t required to learn about a bunch of new concepts in order to use it. However, once your app starts growing in size, you&amp;#x27;ll soon realise that you need to import other functionalities from beyond the library itself. Naturally, this brings us to routing, particularly an introduction to React Router 4.&lt;/p&gt;&lt;h2 id=&quot;routing-in-single-page-applications&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#routing-in-single-page-applications&quot; aria-label=&quot;routing in single page applications permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Routing in Single Page Applications&lt;/h2&gt;&lt;p&gt;Before we start getting technical, let&amp;#x27;s first answer the question &lt;em&gt;&amp;quot;What is routing?&amp;quot;&lt;/em&gt;. However, if you&amp;#x27;re already familiar with this concept, feel free to skip ahead. 😉&lt;/p&gt;&lt;span class=&quot;text-center&quot;&gt;&lt;p&gt;&lt;img src=&quot;/static/know-more-a5f690431aba603818714d546ce98e42.gif&quot; alt=&quot;The More You Know GIF About React Routing&quot;/&gt;&lt;/p&gt;&lt;/span&gt;&lt;p&gt;In a traditional Multi Page Application (MPA) like Wikipedia, whenever you want to access another part of a website, you simply request a different HTML file from the backend server. You carry out this request through the address bar in your web browser. Imagine you want to access my Wikipedia page at &lt;code&gt;https://en.wikipedia.org/wiki/dilshan-kelsen&lt;/code&gt; (doesn&amp;#x27;t exist... &lt;strong&gt;yet!&lt;/strong&gt;). Accordingly, you would receive a rendered HTML file of that page which is then displayed in your browser.&lt;/p&gt;&lt;p&gt;But this isn&amp;#x27;t how Single Page Applications (SPA) work. Ohhhh no! I mean, how could they? You only have a single HTML file available from which the entire website is rendered. Instead, they way you navigate through a SPA is by swapping out components that are displayed on your screen. For example, imagine a &lt;code&gt;HomePage&lt;/code&gt; component being swapped with an &lt;code&gt;AboutUs&lt;/code&gt; component when clicking on &lt;em&gt;About Us&lt;/em&gt; in the navigation bar. In a similar fashion as before, we tell our router what components we want to display by submitting information through the address bar. However, we actually never ⚠️ make a GET request to the server! Rather, we send this information to the router in our application.&lt;/p&gt;&lt;p&gt;Confused about Multi Page and Single Page Applications? Check out &lt;a href=&quot;/single-page-applications-vs-multi-page-applications/&quot;&gt;my article&lt;/a&gt; them to learn the difference!&lt;/p&gt;&lt;h2 id=&quot;react-router&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#react-router&quot; aria-label=&quot;react router permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;React Router&lt;/h2&gt;&lt;p&gt;As you might have guessed, React Router (RR) will act as the routing system of your React application. Doesn&amp;#x27;t take a genius to figure that out... 😬&lt;/p&gt;&lt;h3 id=&quot;preparation-and-installation&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#preparation-and-installation&quot; aria-label=&quot;preparation and installation permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Preparation and installation&lt;/h3&gt;&lt;p&gt;I believe software development is best learnt through hands-on experience. Naturally, you can follow along using your own React project or you can simply create a new one with &lt;code&gt;npx create-react-app &amp;lt;app-name&amp;gt;&lt;/code&gt;. And don&amp;#x27;t worry! &lt;code&gt;npx&lt;/code&gt; is automatically equipped with npm 5.2 and higher, so you won&amp;#x27;t need to install anything else. Furthermore, you also want to have two or more components ready to fiddle with so we can practice some routing.&lt;/p&gt;&lt;p&gt;Following along with a newly created project? Here&amp;#x27;s some sample code that you can copy into your &lt;em&gt;App.js&lt;/em&gt; file. Make sure to actually include these components into your &lt;code&gt;App&lt;/code&gt; component with &lt;code&gt;&amp;lt;HomePage /&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;AboutUs /&amp;gt;&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// To include in App.js&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function maybe-class-name&quot;&gt;HomePage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Welcome to this fairly empty homepage!&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function maybe-class-name&quot;&gt;AboutUs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Howdy! This is the about page of the fairly empty website.&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Furthermore, depending on the initial design of your &lt;em&gt;Create React App&lt;/em&gt;, you may also want to activate your artistic abilities and change some CSS, such as the background colour and minimum height, in order to see your components. 👨‍🎨&lt;/p&gt;&lt;p&gt;Moving on! To actually install RR, you want to run the command line &lt;code&gt;npm i react-router-dom&lt;/code&gt;. Next, we will focus on three main components: &lt;code&gt;&amp;lt;BrowserRouter&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;Link&amp;gt;&lt;/code&gt;. I will get into these next. 👇&lt;/p&gt;&lt;h3 id=&quot;browserrouter&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#browserrouter&quot; aria-label=&quot;browserrouter permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;BrowserRouter&lt;/h3&gt;&lt;p&gt;RR has two main routers: &lt;code&gt;BrowserRouter&lt;/code&gt; and &lt;code&gt;HashRouter&lt;/code&gt;. You can imagine these as two separate systems for how your routing is managed. And which one should you use? Well, generally the former should be used for SPAs and the latter for MPAs in which you added a React application with routing functionality. This distinction is important since the routing of your backend server and that of RR can sometimes interfere with each other and cause some unexpected surprises.&lt;/p&gt;&lt;p&gt;For a better understanding of their differences, I recommend watching &lt;a href=&quot;https://www.youtube.com/watch?v=cdUyEou0LHg&quot; target=&quot;_blank&quot;&gt;a YouTube video by RailCoding&lt;/a&gt;. Keep in mind that it contains an older version of React Router, so the names and code might look a bit different. However, the principle behind it remains very much the same. In this tutorial, I will be focusing on the former since most of you reading this will be developing SPAs (Right? 😅).&lt;/p&gt;&lt;p&gt;Now, in order for our application to make use of any routing functionality from RR, we need to wrap the entire application in a &lt;code&gt;&amp;lt;BrowserRouter&amp;gt;&lt;/code&gt; tag. We can do this either in the &lt;em&gt;index.js&lt;/em&gt; file...&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Don&amp;#x27;t forget to import BrowserRouter&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;BrowserRouter&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;react-router-dom&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;ReactDOM&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;BrowserRouter&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;BrowserRouter&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;root&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;...or the other alternative would be to use it inside our &lt;code&gt;App&lt;/code&gt; component in &lt;em&gt;App.js&lt;/em&gt;. The choice is yours!&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Don&amp;#x27;t forget to import BrowserRouter&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;BrowserRouter&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;react-router-dom&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function maybe-class-name&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;BrowserRouter&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;        ...&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;BrowserRouter&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:500px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:150.99999999999997%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAeABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAMBAgX/xAAWAQEBAQAAAAAAAAAAAAAAAAAAAQP/2gAMAwEAAhADEAAAAfST2ySwg4tNcWM//8QAHRAAAgICAwEAAAAAAAAAAAAAAQIAAxESExQhIv/aAAgBAQABBQL5CsBEyJv4HXbAJr0EtXK1Lheu5nFbpXW4X//EABkRAAIDAQAAAAAAAAAAAAAAAAARARASQf/aAAgBAwEBPwGsxwSP/8QAFBEBAAAAAAAAAAAAAAAAAAAAIP/aAAgBAgEBPwEf/8QAHxAAAwABAwUAAAAAAAAAAAAAAAERIQIScRAiMUFR/9oACAEBAAY/At2pknkavsmDuY3peDdc2cF+dKmjbdPJln//xAAcEAEBAQEBAQADAAAAAAAAAAABEQAhQVExYbH/2gAIAQEAAT8hYwBZcJLfkU80NHPrSf1RyKH990olbtRbRndvfr3N39bhzDXISHx6rpg1u//aAAwDAQACAAMAAAAQbBT/AP/EABoRAAMBAAMAAAAAAAAAAAAAAAABETEhUfD/2gAIAQMBAT8QTUwWEm64fuh0lP/EABgRAQEBAQEAAAAAAAAAAAAAAAEAESFR/9oACAECAQE/EEfZey7jLrf/xAAeEAEBAAICAwEBAAAAAAAAAAABEQAhMUFRcYFhof/aAAgBAQABPxCgborb6yitoaEnK/vWWpuBrZDBZKLs0E+5ZEPcb/uusVSDyaYa5xK4AIgt1bzrFHcqwCJNT44qEXQOqGCJqdrefWCECNaDxZrHfDxqwhrjP//Z&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;BrowserRouter Component Of React Router - Hard Decision Meme&quot; title=&quot;BrowserRouter Component Of React Router - Hard Decision Meme&quot; src=&quot;/static/413d733b7ba3cae44d64200ca90f9734/41099/meme-choice.jpg&quot; srcSet=&quot;/static/413d733b7ba3cae44d64200ca90f9734/e07e9/meme-choice.jpg 200w,/static/413d733b7ba3cae44d64200ca90f9734/066f9/meme-choice.jpg 400w,/static/413d733b7ba3cae44d64200ca90f9734/41099/meme-choice.jpg 500w&quot; sizes=&quot;(max-width: 500px) 100vw, 500px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;It&amp;#x27;s time to decide, my friend...&lt;/figcaption&gt;&lt;h3 id=&quot;route&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#route&quot; aria-label=&quot;route permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Route&lt;/h3&gt;&lt;p&gt;The next component we&amp;#x27;re looking at is &lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt;. This is essentially where your desired component will be rendered according to what is specified in the address bar. Take the example below for instance.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Don&amp;#x27;t forget to import Route&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;Route&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;react-router-dom&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Route&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;exact&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;component&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript maybe-class-name&quot;&gt;HomePage&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// HomePage is rendered here&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Route&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;/about&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;component&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript maybe-class-name&quot;&gt;AboutUs&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// AboutUs is rendered here&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;HomePage&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// No longer needed&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;AboutUs&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// No longer needed&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here, we specify two routes: &lt;code&gt;/&lt;/code&gt; and &lt;code&gt;/about&lt;/code&gt;. Whenever the first path is entered into the address bar, our &lt;code&gt;&amp;lt;HomePage&amp;gt;&lt;/code&gt; component is rendered wherever the &lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt; component is located. Similarly, when &lt;code&gt;/about&lt;/code&gt; is entered, &lt;code&gt;&amp;lt;AboutUs&amp;gt;&lt;/code&gt; is rendered. You may have also noticed that the first route has the attribute &lt;code&gt;exact&lt;/code&gt; in it. Why&amp;#x27;s that? As the name implies, this is to make sure that RR renders the desired component only when the exact path is found in the address bar, rather than in a subset of the URL. For instance, if we were not to use it, even if we accessed &lt;code&gt;&amp;lt;AboutUs&amp;gt;&lt;/code&gt; with &lt;code&gt;/about&lt;/code&gt;, it would render &lt;code&gt;&amp;lt;HomePage&amp;gt;&lt;/code&gt; as well since it found &lt;code&gt;/&lt;/code&gt; in &lt;code&gt;/about&lt;/code&gt;. Got it? 🧐&lt;/p&gt;&lt;p&gt;Now, technically you can place &lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt; wherever you want in your application. However, it makes most sense to group them together as seen above.&lt;/p&gt;&lt;h3 id=&quot;link&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#link&quot; aria-label=&quot;link permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Link&lt;/h3&gt;&lt;p&gt;Alright, so we can access different components by typing different paths into the address bar. But as you probably learned in Web Development 101, no one navigates a website like that. Instead, we use internal links. And &lt;code&gt;&amp;lt;Link&amp;gt;&lt;/code&gt; serves this purpose. Now, instead of passing an &lt;code&gt;href&lt;/code&gt;, as we would to with an anchor tag, we pass in the props &lt;code&gt;to&lt;/code&gt; with a path of our choice.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Don&amp;#x27;t forget to import Link&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;Route&lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;Link&lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;react-router-dom&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Link&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Home&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Link&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Link&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;/about&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;About&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Link&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In addition, we can also make use of &lt;code&gt;&amp;lt;NavLink&amp;gt;&lt;/code&gt;. It acts pretty much the same as its other counterpart, except that it styles itself as “active” when its &lt;code&gt;to&lt;/code&gt; prop matches the current location.&lt;/p&gt;&lt;h2 id=&quot;handy-tricks&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#handy-tricks&quot; aria-label=&quot;handy tricks permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Handy tricks&lt;/h2&gt;&lt;p&gt;Now, using what you&amp;#x27;ve learned so far in this tutorial, you could last quite a while until you need to open up the documentation for further details. However, before I let you go, there are a two more handy tricks 🤓 that you might make use of when routing!&lt;/p&gt;&lt;h3 id=&quot;redirecting-users&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#redirecting-users&quot; aria-label=&quot;redirecting users permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Redirecting users&lt;/h3&gt;&lt;p&gt;You can redirect users on your site using the &lt;code&gt;&amp;lt;Redirect&amp;gt;&lt;/code&gt; with a &lt;code&gt;to&lt;/code&gt; props, as seen in &lt;code&gt;&amp;lt;Link&amp;gt;&lt;/code&gt;. For instance, imagine a situation where a user clicks a button that triggers a function that in turn returns this component, thus redirecting the user.&lt;/p&gt;&lt;h3 id=&quot;passing-down-props-to-children&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#passing-down-props-to-children&quot; aria-label=&quot;passing down props to children permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Passing down props to children&lt;/h3&gt;&lt;p&gt;Developers like to emphasise, oh so often, that your code should be DRY (Don&amp;#x27;t repeat yourself). Hence, the purpose of reusable components. Moreover, imagine reusing a component with slightly different props in various situations, i.e. depending which path you&amp;#x27;re on.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-jsx&quot;&gt;JSX&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-jsx&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag class-name&quot;&gt;Route&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript tag class-name&quot;&gt;HomePage&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript tag attr-name&quot;&gt;someProps&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript tag script language-javascript script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript tag script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript tag script language-javascript string&quot;&gt;&amp;#x27;Chunk Bytes&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag script language-javascript tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token tag script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Create A Plugin For Kibana]]></title><description><![CDATA[Since version 4.3 of Kibana, Elastic let's you create your own custom features through plugins. However, the company very much likes to…]]></description><link>https://dilshankelsen.com/how-to-create-a-plugin-for-kibana/</link><guid isPermaLink="false">https://dilshankelsen.com/how-to-create-a-plugin-for-kibana/</guid><pubDate>Tue, 28 May 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Since version 4.3 of Kibana, Elastic let&amp;#x27;s you create your own custom features through plugins. However, the company very much likes to point out that it&amp;#x27;s &lt;a href=&quot;https://www.elastic.co/elk-stack&quot; target=&quot;_blank&quot;&gt;ELK stack&lt;/a&gt; is still rapidly evolving. As a result, you will need to verify and accordingly update your plugin&amp;#x27;s compatibility with each new version of Kibana. Alternatively, you could also just ignore any newer versions altogether and avoid that nuisance. 😂 Anyways... where was I? Ah yes!&lt;/p&gt;&lt;p&gt;Unfortunately, the rapid development cycles of these tools has led to a lack of proper documentation and outdated tutorials. Take Tim Roes&amp;#x27; blog series &lt;a href=&quot;https://www.timroes.de/writing-kibana-4-plugins-basics&quot; target=&quot;_blank&quot;&gt;Writing Kibana Plugins&lt;/a&gt; as an example, which is often cited as an excellent resource to Kibana plugin development. Since the series was posted, Elastic decided to slowly switch from AngularJS to ReactJS for development, essentially rendering them obsolete. 😢 Yet, a wise decision considering that AngularJS will only be supported until July 2021.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; AngularJS is different from Angular 2+. Check out &lt;a href=&quot;/beginner-introduction-to-angular-overview/&quot;&gt;my article&lt;/a&gt; to learn the difference.&lt;/p&gt;&lt;p&gt;In this tutorial, I&amp;#x27;ll be showing you how to create your very first Kibana plugin with ReactJS using version 7 or higher. However, while I haven&amp;#x27;t tried it myself, this tutorial should theoretically work for any version down to 6.3.&lt;/p&gt;&lt;h2 id=&quot;installing-elasticsearch&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#installing-elasticsearch&quot; aria-label=&quot;installing elasticsearch permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Installing Elasticsearch&lt;/h2&gt;&lt;p&gt;So here I am talking about Kibana and then suddenly Elasticsearch pops up. What&amp;#x27;s the deal with that? 🤨&lt;/p&gt;&lt;span class=&quot;text-center&quot;&gt;&lt;p&gt;&lt;img src=&quot;/static/surprise-c68b91aa9aeed50994feb752b96621ee.gif&quot; alt=&quot;Elasticsearch pops out of nowhere. Surprise Motherfucker Meme&quot;/&gt;&lt;/p&gt;&lt;/span&gt;&lt;p&gt;Well, if you aren&amp;#x27;t already aware of it, Kibana only runs on Elasticsearch. Unfortunately, there&amp;#x27;s no way around that. Now, how you install it can heavily depend on many things such as your OS, its version and whether you want to use any virtualisation tools like Docker or Kubernetes. So if in doubt, always consult the &lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-install.html&quot; target=&quot;_blank&quot;&gt;documentation&lt;/a&gt; for your preferred method.&lt;/p&gt;&lt;p&gt;If you&amp;#x27;re looking to install Elasticsearch 7.0 or above, you can safely follow the steps I&amp;#x27;m about to show you, regardless of your OS. 👇&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Download the latest version of Elasticsearch &lt;a href=&quot;https://www.elastic.co/downloads/elasticsearch&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt; as a &lt;code&gt;.zip&lt;/code&gt; or &lt;code&gt;.tar.gz&lt;/code&gt;&lt;/li&gt;&lt;li&gt;Extract the content from these files into a separate folder&lt;/li&gt;&lt;li&gt;Access this folder through your Terminal or PowerShell with &lt;code&gt;cd &amp;lt;/folder-location&amp;gt;&lt;/code&gt;&lt;/li&gt;&lt;li&gt;Once in the folder directory, launch Elasticsearch by running the command &lt;code&gt;bin/elasticsearch&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Et voilà! That&amp;#x27;s all there is to it. 🙃 To be 100% sure that it&amp;#x27;s actually working, you can simply access &lt;code&gt;localhost:9200&lt;/code&gt;in your browser, after which you should get something like this:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-text&quot;&gt;TEXT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-text&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &amp;quot;name&amp;quot; : &amp;quot;Chunk Bytes&amp;quot;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &amp;quot;cluster_name&amp;quot; : &amp;quot;elasticsearch&amp;quot;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &amp;quot;cluster_uuid&amp;quot; : &amp;quot;zJVzmZpkTdCCQurGqUol9Q&amp;quot;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &amp;quot;version&amp;quot; : {&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &amp;quot;number&amp;quot; : &amp;quot;7.0.1&amp;quot;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &amp;quot;build_flavor&amp;quot; : &amp;quot;default&amp;quot;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &amp;quot;build_type&amp;quot; : &amp;quot;tar&amp;quot;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &amp;quot;build_hash&amp;quot; : &amp;quot;e4efcb5&amp;quot;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &amp;quot;build_date&amp;quot; : &amp;quot;2019-04-29T12:56:03.145736Z&amp;quot;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &amp;quot;build_snapshot&amp;quot; : false,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &amp;quot;lucene_version&amp;quot; : &amp;quot;8.0.0&amp;quot;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &amp;quot;minimum_wire_compatibility_version&amp;quot; : &amp;quot;6.7.0&amp;quot;,&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &amp;quot;minimum_index_compatibility_version&amp;quot; : &amp;quot;6.0.0-beta1&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  },&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &amp;quot;tagline&amp;quot; : &amp;quot;You Know, for Search&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;setting-up-the-kibana-development-environment&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#setting-up-the-kibana-development-environment&quot; aria-label=&quot;setting up the kibana development environment permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Setting up the Kibana development environment&lt;/h2&gt;&lt;h3 id=&quot;downloading-from-github&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#downloading-from-github&quot; aria-label=&quot;downloading from github permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Downloading from Github&lt;/h3&gt;&lt;p&gt;Alright, back to what you came for! This time we will be heading over to &lt;a href=&quot;https://github.com/elastic/kibana&quot; target=&quot;_blank&quot;&gt;Kibana&amp;#x27;s Github page&lt;/a&gt;. Here you want to choose and download the &lt;em&gt;same&lt;/em&gt; version as your copy of Elasticsearch which you can pick through the right git branch or tag. Elastic was so kind enough to create an illustration of the version compatibilities between ES and Kibana, as seen below. Ultimately, it&amp;#x27;s best to have the &lt;em&gt;exact&lt;/em&gt; versions.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:777px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:37.99999999999999%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAAsTAAALEwEAmpwYAAABd0lEQVQoz03Si5KkIAwFUP//G3e22yeI+LYRAb17w9RMrVUpLcXkJFDc9w2te7SdgjzLNViLqm7QKQ2lNbpOY98PrulyyHqtDeIdsR0bmqbFsqz530Lx42BH6N5gGCc8DzBOM46PY4EHIUakdOM8PSa+DyEipoQnAfpUKMcS4UpIxEgUZUUJq7WqR9V0TLbCDCPmdYcPCacP8JdIHHpj8WHi8wqMB26vYM0X/r5FromyKITfmyGHtLisG+Z5wXF8cJMrGql8es9CNgsT30VOJ3xqzOYP3pVish7WTihEJdlFWLeKwoUjmLLwotCJkEnW7WBBQ+H1LQw3nDUw5QvvRmHiDKWTom27X2HDhDI7mdUuQs4wxm+hY6sy6+tHyBleIhy+UP4vlCSd7DKFZd1i5W6KcKEw8C/RiHTnDGWNo1Aknj2fA4WvF0oK83puXlGz5Xw8GPK8bnuWLMuGS1rj7DzvGwvJUXHuhPdsm9Lc8vuFiig5NrL+H+DWYdRfqHjlAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Version compatibility between ES and Kibana&quot; title=&quot;Version compatibility between ES and Kibana&quot; src=&quot;/static/51b74662b282b6e68fb5414e0b2f194b/108f8/image-1.png&quot; srcSet=&quot;/static/51b74662b282b6e68fb5414e0b2f194b/772e8/image-1.png 200w,/static/51b74662b282b6e68fb5414e0b2f194b/e17e5/image-1.png 400w,/static/51b74662b282b6e68fb5414e0b2f194b/108f8/image-1.png 777w&quot; sizes=&quot;(max-width: 777px) 100vw, 777px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Version compatibility between ES and Kibana&lt;/figcaption&gt;&lt;p&gt;&lt;strong&gt;Beware:&lt;/strong&gt; The master branch does not represent the most recently released version, but rather an unreleased version under development.&lt;/p&gt;&lt;h3 id=&quot;checking-your-nodejs-version&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#checking-your-nodejs-version&quot; aria-label=&quot;checking your nodejs version permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Checking your NodeJS version&lt;/h3&gt;&lt;p&gt;Moving on! 😃 Once you&amp;#x27;ve cloned the git repository or downloaded it as a &lt;code&gt;.zip&lt;/code&gt;, verify that you have the same NodeJS version installed as required by your Kibana project. You&amp;#x27;ll find the necessary version indicated in the &lt;code&gt;.node-version&lt;/code&gt; or &lt;code&gt;.nvmrc&lt;/code&gt; file. Likewise, run the command &lt;code&gt;node -v&lt;/code&gt; to view your own installed version.&lt;/p&gt;&lt;p&gt;The most simple way to update your NodeJS version is to simply download and install the desired version from &lt;a href=&quot;https://nodejs.org/en/download/releases/&quot; target=&quot;_blank&quot;&gt;their website&lt;/a&gt;. However, if you find yourself needing to keep multiple NodeJS versions installed due to other projects, it&amp;#x27;s recommended that you use Node Version Manager to switch between them. There&amp;#x27;s a &lt;a href=&quot;https://github.com/nvm-sh/nvm&quot; target=&quot;_blank&quot;&gt;Linux/Mac version&lt;/a&gt; 🍏 and a &lt;a href=&quot;https://github.com/coreybutler/nvm-windows&quot; target=&quot;_blank&quot;&gt;Windows&lt;/a&gt; version that you can use.&lt;/p&gt;&lt;h3 id=&quot;bootstrapping-with-yarn&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#bootstrapping-with-yarn&quot; aria-label=&quot;bootstrapping with yarn permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Bootstrapping with Yarn&lt;/h3&gt;&lt;p&gt;Before we move on, it&amp;#x27;s very important that your Kibana folder is named &lt;code&gt;kibana&lt;/code&gt; and that&amp;#x27;s it! Not &lt;code&gt;kibana-7.0.0&lt;/code&gt; or something else. Simply &lt;code&gt;kibana&lt;/code&gt;. If you fail to do this before you bootstrap, you won&amp;#x27;t be able to generate a plugin, as the plugin generator will be looking for a folder called &lt;code&gt;kibana&lt;/code&gt;. Alright, let&amp;#x27;s continue...&lt;/p&gt;&lt;p&gt;Our next step is to bootstrap Kibana. For this we will need to use Yarn, so make sure you have it installed. You can easily verify this in your terminal or PowerShell with &lt;code&gt;yarn -v&lt;/code&gt;. If this command fails, you don&amp;#x27;t have it installed and should get it right &lt;a href=&quot;https://yarnpkg.com/en/&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Afterwards, you have to:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Initialise a git repository with &lt;code&gt;git init&lt;/code&gt; in your project directory, and&lt;/li&gt;&lt;li&gt;Bootstrap Kibana with the command &lt;code&gt;yarn kbn bootstrap&lt;/code&gt; &lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:500px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:135%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAbABQDASIAAhEBAxEB/8QAGQAAAgMBAAAAAAAAAAAAAAAAAAMBAgQF/8QAFwEAAwEAAAAAAAAAAAAAAAAAAAECA//aAAwDAQACEAMQAAAB6icTcypzCyz4ZSykFL//xAAbEAACAwEBAQAAAAAAAAAAAAABAgADESETIv/aAAgBAQABBQI9iHZZe4f1eC3IyljpYeZJ0CV8Zz8kc//EABYRAQEBAAAAAAAAAAAAAAAAAAAREP/aAAgBAwEBPwHKr//EABcRAQEBAQAAAAAAAAAAAAAAAAARAQL/2gAIAQIBAT8BznURH//EAB4QAAIBAwUAAAAAAAAAAAAAAAABEQIiMRAyUXHw/9oACAEBAAY/AjiBpM3ETcyZyWqSfIzpULo//8QAHRABAQABBQEBAAAAAAAAAAAAAQARITFBUWFxkf/aAAgBAQABPyEPKZr8ICYF5vuLWdySH2W4CzwQOlE5SBcOq3JLi0/IHL0v/9oADAMBAAIAAwAAABA8PvH/xAAVEQEBAAAAAAAAAAAAAAAAAAARIP/aAAgBAwEBPxAg/8QAGBEBAQADAAAAAAAAAAAAAAAAAQAQETH/2gAIAQIBAT8QCJuEF1j/AP/EAB4QAQEAAgICAwAAAAAAAAAAAAERACFBUXGRMYHh/9oACAEBAAE/ECgCTrGVooht8nGOseBTHQfSN+cbXiXnM/MW1KpXBdnBGXvjxhRIFWOmxON5rWpW09YrieEE9OQ6KRQD7xENe7P/2Q==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Long Kibana bootstrapping process meme&quot; title=&quot;Long Kibana bootstrapping process meme&quot; src=&quot;/static/25835d12413272b2a5a9553d20e60a29/41099/meme-skeleton.jpg&quot; srcSet=&quot;/static/25835d12413272b2a5a9553d20e60a29/e07e9/meme-skeleton.jpg 200w,/static/25835d12413272b2a5a9553d20e60a29/066f9/meme-skeleton.jpg 400w,/static/25835d12413272b2a5a9553d20e60a29/41099/meme-skeleton.jpg 500w&quot; sizes=&quot;(max-width: 500px) 100vw, 500px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Didn&amp;#x27;t wait too long I hope?&lt;/figcaption&gt;&lt;p&gt;And that&amp;#x27;s that! With all these steps, your Kibana development environment is ready to be used. Moreover, if you&amp;#x27;d like to, you can already launch Kibana with &lt;code&gt;yarn start&lt;/code&gt; or &lt;code&gt;yarn start --oss&lt;/code&gt; for the open-source version. Make sure that Elasticsearch is running as well! However, this is not the one you will be working with.&lt;/p&gt;&lt;h2 id=&quot;generating-a-plugin&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#generating-a-plugin&quot; aria-label=&quot;generating a plugin permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Generating a plugin&lt;/h2&gt;&lt;p&gt;Alright, the thing you&amp;#x27;ve been looking for! It&amp;#x27;s time to create a Kibana plugin! 😋 Fortunately, we have the option to generate a base plugin rather than create one completely from scratch. Once again located in your Kibana project directory, run the command &lt;code&gt;node scripts/generate_plugin &amp;lt;plugin-name&amp;gt;&lt;/code&gt; in your terminal. This should prompt you a few questions to which you can answer as you&amp;#x27;d like to. However, make sure that you enter the right version of your Kibana project, otherwise you won&amp;#x27;t be able to launch it! 😱&lt;/p&gt;&lt;p&gt;Afterwards, the plugin generator will create another folder called &lt;code&gt;kibana-extra&lt;/code&gt; next to your original Kibana folder, which contains all of your plugins. To launch it, go to &lt;code&gt;kibana-extra/your-plugin&lt;/code&gt; and run &lt;code&gt;yarn start&lt;/code&gt; or &lt;code&gt;yarn start --oss&lt;/code&gt; to launch it in the open-source version.&lt;/p&gt;&lt;p&gt;Check your terminal for the exact address of your plugin, i.e. usually &lt;code&gt;http://localhost:5603/&lt;/code&gt; followed by 3 random letters. Lastly, you will be able to access your plugin on the sidebar and see something similar to this.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:48.50000000000001%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAeUlEQVQoz+2OQQ6DMAwE8/+PRhBSWkSCndhZYo6cgKq3Whr5YO9onZ9emOcPYnxjowLi+pi8MVwIEcMYsCwrVHEg0lBFbyGdUtWEE7z3sG2MXU5EsGmtXaZ/o5rQaqZMWBMdO2Xu9QVc7mM5x0Vxxg5Pcd+E/8LfCHfsVg/cb9mK1QAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Generated Kibana plugin screenshot&quot; title=&quot;Generated Kibana plugin screenshot&quot; src=&quot;/static/a549b21e9f291a0f3e1a9ade440399ab/5a190/image-2.png&quot; srcSet=&quot;/static/a549b21e9f291a0f3e1a9ade440399ab/772e8/image-2.png 200w,/static/a549b21e9f291a0f3e1a9ade440399ab/e17e5/image-2.png 400w,/static/a549b21e9f291a0f3e1a9ade440399ab/5a190/image-2.png 800w,/static/a549b21e9f291a0f3e1a9ade440399ab/2bef9/image-2.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Your very first Kibana plugin!&lt;/figcaption&gt;&lt;p&gt;Now, I won&amp;#x27;t show you how to actually program inside of your plugin since this is merely a ReactJS app, and that would beyond the scope of this tutorial. However, learning how to use the &lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html&quot; target=&quot;_blank&quot;&gt;Elasticsearch API&lt;/a&gt;, the &lt;a href=&quot;https://elastic.github.io/eui/#/&quot; target=&quot;_blank&quot;&gt;Elastic UI framework&lt;/a&gt; and &lt;a href=&quot;https://elastic.github.io/elastic-charts/&quot; target=&quot;_blank&quot;&gt;Elastic Charts&lt;/a&gt; will prove to be indispensable tools in your plugin development.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Install Bootstrap 4 In Laravel 5.8 And Above]]></title><description><![CDATA[When you learn a new tool and find yourself in a new environment, even the simplest of tasks can seem very confusing to do. I found myself…]]></description><link>https://dilshankelsen.com/how-to-install-bootstrap-4-in-laravel/</link><guid isPermaLink="false">https://dilshankelsen.com/how-to-install-bootstrap-4-in-laravel/</guid><pubDate>Tue, 21 May 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;When you learn a new tool and find yourself in a new environment, even the simplest of tasks can seem very confusing to do. I found myself in that position not too long ago when I was tinkering with Laravel 5.8 and I wanted to make use of Bootstrap 4. A lack of proper documentation on how to access it, beside the usual &amp;quot;iT&amp;#x27;s aLReADy pRe-cOnFiguREd&amp;quot;, made this whole process much harder than it should have been. But don&amp;#x27;t worry, I got you covered! I will show you how to install Bootstrap 4 in Laravel. I suffered for your convenience...🤣&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:666px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:56.49999999999999%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAEEA//EABUBAQEAAAAAAAAAAAAAAAAAAAMA/9oADAMBAAIQAxAAAAHZz0HIQpf/xAAbEAACAQUAAAAAAAAAAAAAAAAAARECAxASIf/aAAgBAQABBQJVclksspa4/8QAFhEBAQEAAAAAAAAAAAAAAAAAAQAR/9oACAEDAQE/AV0tb//EABoRAAICAwAAAAAAAAAAAAAAAAABAhESIVH/2gAIAQIBAT8BUKejCPD/xAAXEAADAQAAAAAAAAAAAAAAAAAQESAx/9oACAEBAAY/AkNj/8QAGhABAAIDAQAAAAAAAAAAAAAAAQARITFhcf/aAAgBAQABPyG87JiN07IRNZgFnsds/9oADAMBAAIAAwAAABCcH//EABgRAAIDAAAAAAAAAAAAAAAAAAABITFh/9oACAEDAQE/EE0SbH//xAAYEQADAQEAAAAAAAAAAAAAAAAAAVFhkf/aAAgBAgEBPxBc0MXEf//EABsQAQACAwEBAAAAAAAAAAAAAAEAESExoUFh/9oACAEBAAE/EHUwrN7+wGCZK1FW+kEhEtsRYG3k6J//2Q==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Trying To Install Bootstrap 4 In Laravel Meme&quot; title=&quot;Trying To Install Bootstrap 4 In Laravel Meme&quot; src=&quot;/static/c4e5764e8c09ef46d8a8357d2dac7a20/b9a87/meme-fist.jpg&quot; srcSet=&quot;/static/c4e5764e8c09ef46d8a8357d2dac7a20/e07e9/meme-fist.jpg 200w,/static/c4e5764e8c09ef46d8a8357d2dac7a20/066f9/meme-fist.jpg 400w,/static/c4e5764e8c09ef46d8a8357d2dac7a20/b9a87/meme-fist.jpg 666w&quot; sizes=&quot;(max-width: 666px) 100vw, 666px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;In order to follow this tutorial, make sure you have a laravel project up and running. If not, you can easily create a new one with &lt;code&gt;laravel new &amp;lt;project-name&amp;gt;&lt;/code&gt;.&lt;/p&gt;&lt;h2 id=&quot;understanding-laravel&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#understanding-laravel&quot; aria-label=&quot;understanding laravel permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Understanding Laravel&lt;/h2&gt;&lt;p&gt;Hearing that Laravel comes already pre-configured with front-end tools like Vue.js and Bootstrap doesn&amp;#x27;t make you any wiser in how to access them. So naturally this begs the question as to how exactly Bootstrap is implemented in Laravel. The answer? Well, look no further than the &lt;code&gt;resources&lt;/code&gt; folder. 👈 This folder contains all your uncompiled resources, such as your JavaScript, SCSS, images and views.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:344px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:129.99999999999997%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAaCAYAAAC3g3x9AAAACXBIWXMAABYlAAAWJQFJUiTwAAADxElEQVRIx4VVy3IaVxDVfygImAfMMMMAA8PTSJinBRgpyErZIAGykERJYMdRVSqusvMD2aYq26yyyD6/4Kp81Ul3K0N4SM6i6TuXO327+5zTs6NpOkzTQiQSRS7twHPjMIwYbDuOWMyitSlry7LF+By/o+uRR22Hf2r1JsqFNP767QazQR2twy7ubm9xeTnFcDjEaDQWu7ubw/OyCIeVrwdU6UZFUZBzHahhfjYo65hklEgkxXOWvI5Gja9nyH9yGeVyBcmkjc8/tdHvedjfryGVSiEUCksAVdXoUnUr2ObzMqDreigWXPz5ewfjgY125xWelUrL8vjcqvEFfrDVoDv+BpesqhEEAkE4cRNGVEeYMuISGRg+xxez8ZpbsgqQ7yUgl+K6LgqlMnIpG19+jWP2KooXlOX08q0Acn19g/F4IkDdEmDT6RRv3gyWGW9l6G8YRhTdog7P0WDG4nAcR0DJ5wvU4xRdnEY2m6P9BDIZbzvgaspSuqrjsKXSCxq9WCCwykin0ygUiojHHQGJ++qDtAnOWobcQ8vScTVSkPc0DM8muL+/l9LOz89RrVYlGPf1SZT/+4O9Lj6wR32l4Pzy3l5QPGez6h8S0NaqWyvZD2qaBj5+H0KtYuL16xEBMcZkciHAXFxckL3FYDDEyckJOp2utGArwwfaPJBWU3aRcYMEjkq9I24WS9K/UukZAZPHwcEB6vUGKpXnIsPVPi4zZE4lk0naMJGrjJFwGwIEI8po8hkfDPbBYGj5vFUy94F1WiwWoEdSGP/8BZObH/HtUVe49+HDDyTDAwngk9r3T4KiahpCfKsSRiBESrEdAWN39xtB9DFF/O9wqNSqKCWy+OP9Lxh1TlFrNnA2PBMwcrm8lLea2aatocwH44kE0lYSn4ZzPM+XSYYltNsdsjaNrYSU7FOG2+Sbomhr9FmirPJhVcXuXgBFOwOLAAoEg9CJSjwHWXbpdEaQZSn685LVw+tHeahQL7N2Cj2viggF2ieKLBYLzGYzAWixeCfK4cHAPLy6upa9ly97Sz7ubPYholP5hiWeW7GqEDamzMMQMf81Y22Kr2XIaCdMG0eUYdJ0cEj9Oz39Do1GE7VaHcfHx+j3T5bfldWBu9VDUQsFtKI0qmyPtKyi1zsilEcisWazJb7f78v42lTIFsrcWJ3WectAyqRDEeIfHfDRZe+rxA/2GCdFKQ4hVSrvU7kG/r45xOdWBpUXXbybzzGfL2RCsww5kM/Fpwi+PmDJt1IW8jEChD72cZIk04Kn9ubn86mA/wB3KS4P6ffVMgAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Resources Folder In Laravel&quot; title=&quot;Resources Folder In Laravel&quot; src=&quot;/static/8df61ac709fc732655fb51d928e4da74/75d93/image-1.png&quot; srcSet=&quot;/static/8df61ac709fc732655fb51d928e4da74/772e8/image-1.png 200w,/static/8df61ac709fc732655fb51d928e4da74/75d93/image-1.png 344w&quot; sizes=&quot;(max-width: 344px) 100vw, 344px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Resources folder in Laravel&lt;/figcaption&gt;&lt;p&gt;Since Bootstrap is made up of both JS and CSS components, we will want to look into their respective subfolders in &lt;code&gt;resources&lt;/code&gt;.&lt;/p&gt;&lt;h3 id=&quot;examining-the-javascript&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#examining-the-javascript&quot; aria-label=&quot;examining the javascript permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Examining the JavaScript&lt;/h3&gt;&lt;p&gt;Oh look! There&amp;#x27;s a file called &lt;code&gt;bootstrap.js&lt;/code&gt;. That must be what we&amp;#x27;re looking for, right? Well... yes and no! The name here is actually referring to the activity of bootstrapping rather than the UI library Bootstrap. That being said, this file bootstraps and configures any JS dependencies, whether it be Vue.js, jQuery or Bootstrap itself.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// bootstrap.js&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access maybe-class-name&quot;&gt;Popper&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;popper.js&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token dom variable&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;jQuery&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;jquery&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;bootstrap&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;catch&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Without getting into any details, we can see that &lt;code&gt;popper.js&lt;/code&gt; and &lt;code&gt;jquery&lt;/code&gt;, two Bootstrap dependencies, are being imported and assigned to variables. Afterwards, &lt;code&gt;bootstrap&lt;/code&gt; itself is imported. Lastly, the file itself is imported into and accessed through the &lt;code&gt;app.js&lt;/code&gt; file. 😯&lt;/p&gt;&lt;p&gt;In the end, when your application is launched from your server, all your JS is compiled into one single file in &lt;code&gt;public/js/app.js&lt;/code&gt;. This is the script file you actually want to reference in your main HTML file with &lt;code&gt;&amp;lt;script src=&amp;quot;js/app.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt;.&lt;/p&gt;&lt;h3 id=&quot;examining-the-sass&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#examining-the-sass&quot; aria-label=&quot;examining the sass permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Examining the Sass&lt;/h3&gt;&lt;p&gt;In case you weren&amp;#x27;t aware of it, Bootstrap can be used with Sass, a preprocessor scripting language. Or in layman&amp;#x27;s terms, an extension of CSS. It&amp;#x27;s actually the recommended way over using regular CSS for a number of reasons. For example, it allow&amp;#x27;s you to easily and safely &lt;a href=&quot;https://getbootstrap.com/docs/4.0/getting-started/theming/&quot; target=&quot;_blank&quot;&gt;customise your Bootstrap components&lt;/a&gt; and import only the bare necessities rather than the entire library. But I digress... 😅&lt;/p&gt;&lt;p&gt;When we look at &lt;code&gt;app.scss&lt;/code&gt;, we can see the entire Bootstrap Sass library being imported with &lt;code&gt;@import &amp;#x27;~bootstrap/scss/bootstrap&amp;#x27;&lt;/code&gt;. Same as before, our Sass files are ultimately compiled down to regular CSS and can be found under &lt;code&gt;public/css/app.css&lt;/code&gt;. This is the file you also want to reference in your HTML file with &lt;code&gt;&amp;lt;link rel=&amp;quot;stylesheet&amp;quot; href=&amp;quot;css/app.css&amp;quot;&amp;gt;&lt;/code&gt;.&lt;/p&gt;&lt;h2 id=&quot;installing-and-running-bootstrap&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#installing-and-running-bootstrap&quot; aria-label=&quot;installing and running bootstrap permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Installing and running Bootstrap&lt;/h2&gt;&lt;p&gt;Okay, wait! We haven&amp;#x27;t actually installed Bootstrap on our machine yet. We can do that simply by running &lt;code&gt;npm install&lt;/code&gt;, as it&amp;#x27;s already listed in our &lt;code&gt;package.json&lt;/code&gt; file. In Laravel, this file serves as the package manager for any front-end libraries and frameworks. On the other hand, back end tools are managed by Composer 🎼. You may also want to adjust the Bootstrap version that you will install, since it appears to default on version &lt;code&gt;4.0.0&lt;/code&gt;, at the time of this writing.&lt;/p&gt;&lt;p&gt;Alright, almost finished! The only thing left to do is to compile our front-end. Once again, &lt;code&gt;php artisan&lt;/code&gt; ain&amp;#x27;t gonna help us here buddy. Instead, you want to run the command &lt;code&gt;npm run watch&lt;/code&gt;. This will compile our front-end as well as recompile it on any changes.&lt;/p&gt;&lt;p&gt;It&amp;#x27;s time to get cracking with Bootstrap! 👩‍💻&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Angular vs React vs Vue - A Framework Comparison]]></title><description><![CDATA[As a frontend developer, you've undoubtedly heard of the big three: Angular, React and Vue. These days, it's hard to come across a project…]]></description><link>https://dilshankelsen.com/angular-react-vue-framework-comparison/</link><guid isPermaLink="false">https://dilshankelsen.com/angular-react-vue-framework-comparison/</guid><pubDate>Tue, 30 Apr 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;As a frontend developer, you&amp;#x27;ve undoubtedly heard of the big three: Angular, React and Vue. These days, it&amp;#x27;s hard to come across a project that doesn&amp;#x27;t use one of them. As per usual, the developer is plagued with the decision of choosing one over the other. Particularly as a beginner, this can be a daunting task. But don&amp;#x27;t fret my friend! You&amp;#x27;ll no longer be pulling your hair out over this topic. 🤯 In this article, I&amp;#x27;ll be giving you a comprehensive comparison between all three frameworks so that you can finally make that decision of yours.&lt;/p&gt;&lt;p&gt;To be clear, this article provides a birds-eye analysis of their current state in 2019 rather than a detailed technical comparison. Furthermore, I’ll be covering the topics listed below. Feel free to click on any of them to skip ahead!&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;#introduction&quot;&gt;Introduction&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;#popularity&quot;&gt;Popularity&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;#job-demand&quot;&gt;Job Demand&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;#technical-features&quot;&gt;Technical Features&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;#future-prospects&quot;&gt;Future Prospects&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h2 id=&quot;introduction&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#introduction&quot; aria-label=&quot;introduction permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Introduction&lt;/h2&gt;&lt;p&gt;I&amp;#x27;m assuming you have little to no knowledge on the frameworks I&amp;#x27;m discussing here. So how about I introduce them to you first? Keep in mind that they are all open-source. 😋&lt;/p&gt;&lt;h3 id=&quot;angular--omit-in-toc&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#angular--omit-in-toc&quot; aria-label=&quot;angular  omit in toc permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Angular &lt;/h3&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:350px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:100%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAD8UlEQVQ4y22UXWhbZRjHn3NOiuJ2IRQ2diGb1dgmbsnJx4p2Wp1CbwaiYxNRuxnZleBEBdGJIDIEBS+8UcQLV2/Uge7CO+di2zSNSzcho9o26UnWrDWdlXUTt36c5H38P+95W0vZCw/ve/LxO//n4/0TbVgL1E6XKUEziClyqYYdYZfJdXIUd0Yobp+nOOFMecQookab1hilNGQ6+LM+Y7c8Sjh/UNyS57KGxDTkHMUsgeNsybO8YBhRQOhV3QDC2YEyW86eVhmjOrm7y3byg1E7cQLQ8M+0RysUdYMUs6HaKZhnecFmoE4V5104v+qRm6taydYU7eEcPcg5y10G9CwgxwDZASCNGHXrwDUY4m5A+qHsR8S/CPbsJFeoi2d7j/oX3IP+IHUKFPC4xCLiNICHAdxSgBCBonaugxB1xwWCs4mE79mpZpnuV0vZAv85cIaztFPlnGRzmGI+giUEDuhBUSrp67pVg3QPG+AydgV1qkJRnok+rRhrdfGGKmzrVUNB6gqgFoCrBviYAdoCsg3wUQNUnoSTUmW6j69/9g23mr4wufr2J/wL3ctQqQBTRuFN1C6SD2po6zkzdewC8Oa01A6NmKbdXNv2uAZdPfk5/3XqB165tsi5kItUpUlaoeyN8+S2/4qyoZaWgKw6gHVKtgPYkPp5TrpVpg7++91PNbC89SG+GHlKnydeeAsqO6Ay0TTASRm/K5T+v8tMD9N31CmzNxGkHW96d6TZn5vnlfEpnul5nktPZnj56gL/UyzxEEWkhk3T7ewQxmcwGHhamz0L10uA51A7Rmeb80fe0c243Srtz6hBCvs5OyHAAQO01xUCZleC0TklCisU8ZcujqvVy7Nqdt+LXO/NcGlfP1/qO8bNW7fUwvc/qSzt0kB09qQAEY7UEUaQJMAcDWzb+2GFHuDGodeXRcnCmx/zFO1QkxRBmp3qLG1Xc198K1+p37qfW5JBz7elXskFhhEqikI0w8wi3KWtu38af248+wZf//K0X9u+v+Wh8OVQWuVDScxglMfCB5rzX5/xS09k5CVcuLO7L2+GegwsmoPbiF1x/Dg10K1aKH2oQuHfy7QTzYkxSrCKkqAJsdawFfcFmqV7AAsXR0OpvtFHXqYsUg6uXixQKMArVoquifuE9pL/3kCodlfPa56VmMeQo0nB/ZUxGbHcamFrT+YAfptvS9Nw1zM0CVECZFxkmsC41Iyp1o2FNahb2xc+b0d8BOAKUroB6IkxcrfINaOXSKd5yXLhia6ewa82Gu2afa25jxhs3djZJLnRIrkdeW1VrjRg3QNHzH7bNU5Rqm4AQ7UFoAMgFYO7uu7UI8b/3t/E+A9Ef3H0UiKIXgAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Angular Logo&quot; title=&quot;Angular Logo&quot; src=&quot;/static/605cc60067fae5002c12cdefafe19eda/13ae7/image-1.png&quot; srcSet=&quot;/static/605cc60067fae5002c12cdefafe19eda/772e8/image-1.png 200w,/static/605cc60067fae5002c12cdefafe19eda/13ae7/image-1.png 350w&quot; sizes=&quot;(max-width: 350px) 100vw, 350px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;Arguably the grand daddy of modern JS frameworks. Well, not exactly... Long story short: In 2009, Google released the first full-fledged framework for creating Single Page Applications called AngularJS or Angular 1. However, due to some imperfections, Google decided to completely rewrite the framework into what&amp;#x27;s now called Angular 2, or simply Angular. You can find a more in-depth introduction to Angular 👉 &lt;a href=&quot;/beginner-introduction-to-angular-overview&quot;&gt;right here&lt;/a&gt;.&lt;/p&gt;&lt;h3 id=&quot;react--omit-in-toc&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#react--omit-in-toc&quot; aria-label=&quot;react  omit in toc permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;React &lt;/h3&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:350px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:100%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAADHUlEQVQ4y12VW0iUQRTHZ71E4WN3iuopogv0EBVERNeXHoIgCgqhh6AgCqEbhIhBGfUQgmWKYKJYSXSxsqDIotAys9bbzrfralSCSFC9GEF7vv7zn/Otaws/zvHMN2fOZc5ojPsNiedbxph4aIwVT+BkmM+1JEiEed6mJMIYMKYXDIuZ/KXhKA1DOxYGQudoJzgMlsChPyRFB25tNSgBG7lm4dTq+hSHLhLLDYfAS3AOvAYVPCQt06DXgeegFBF2Qm5lxIHkTXXo0unCpj46bAUrmFJKZkBWgWZwF5SZnjAqxz5Qw6gTCCYdOUxrfVwNRqQAH7WZhMw3ybCAp3bQwXfw0QxCb3BlYQ034dt61q8HaQ+Jpp1Uh4M4xdfvJljMVHrp7BH0o0zdSi2d+jS3g0qNsJCB0WEgMfMz49N4QwcutTv82EoHowvkFnD2X+AZ9MuQTyCr6TDqeNI79JEEcpLpWukCT6EfgUzh1NnmsxSZsUwhbKtg+wq5F/ITaIf+GOynY0b45W9Mo7qBhTk4ZSZkC/52Ue0C06G3MN1+HnwMNIEGM55x93I51togr/CWQDkOrvKP8I/RFOpAN+tl5T5sIaTjgtrGsecEU01m72cr9B1OOYiFJhq5yA42gH7dXK/OQnx7SifpB/Sy7DR10+Zqu8HophrwEIalcOo63Ayug2J23ko5KNEOl4JLTDslsyA3s3mBnPEpT45UMcO2EgcveFWsjOKAldAXQs6D3AbbmGY1AN6z3oFs0U6bqAYxhu6L3sIaBnKexbYywUkJ5B7kb3Ab+lnIRsgqDcZlkYfboBGm9GJ/UIeBLOBHCa2hb1w5uKjz7lgPalmSQUzVCGzhhD5dSX2+hnX0rMzl6Dl7nzbBSsAajmWiWV4LGtmkd6HfbyXncYhnn64HYA2jSOJS+45Xq7yGbIo0wgNsXPQ4DOW+NiNaA3/yHtCpXX4FTnNTnIdVqK0avAXrJp+v8D+HlaFeIW50Ee7m1FjWN2ZCG60t4ugFsiz7wAYu5VyHLtzRjE/ZascDra2V/OzT5B7haM3/e/CvdTQU+P0DJSsns6/Fi2YAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;React Logo&quot; title=&quot;React Logo&quot; src=&quot;/static/d6b91ae57f5ec070855c07c3dfa0281d/13ae7/image-2.png&quot; srcSet=&quot;/static/d6b91ae57f5ec070855c07c3dfa0281d/772e8/image-2.png 200w,/static/d6b91ae57f5ec070855c07c3dfa0281d/13ae7/image-2.png 350w&quot; sizes=&quot;(max-width: 350px) 100vw, 350px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;In the past, Facebook struggled to maintain their social media platform. Their solution? React! Released in 2013, it&amp;#x27;s technically a library and several other external libraries are needed to turn it into an &lt;a href=&quot;https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&quot; target=&quot;_blank&quot;&gt;MVC&lt;/a&gt; framework. Moreover, unlike Angular, React is a relatively unopinionated framework as it provides you with a lot of flexibility in how you do things. Well... that&amp;#x27;s quite frankly a defining feature of a library. 😆&lt;/p&gt;&lt;h3 id=&quot;vue--omit-in-toc&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#vue--omit-in-toc&quot; aria-label=&quot;vue  omit in toc permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Vue &lt;/h3&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:300px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:100%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAADd0lEQVQ4y5WUy08TcRDHpwUSb8YjiYknDiaeZEsr1u5uWx+A8ZEIQdtSUWg1qDHa+ASBdldQCYkXEo2JhkQENGpFgS7lES/6B3jxaKLxYjx4Udj6W2dmf5BohOgvmfTX6c53PvPYgjbZC2wve0GfM8DfnoJq9TAo9XHw1SUgdPYyBBpTULP/GASa0qCgT2mIQ3WoGQKnT4A+b3Dssg7oljmoF4y8bhlj+DnuT7SPK9HEmK8++RxtsPnzY6jyBiHQnIJtR9IV6BtCe6ZEEmOB4+lxvWiOY9wo2ovwjGkAXk7p0zlHm8w6+qzhBG9lHCUSd3z1LY6vDm1vMoYCSNsCvoZkB/vQlGjcCd6+wDEcixqoFQf1VQ/gF0ubyjpoP/SiYQfa0jaKLkrRD7UtJyv8B49twPsX8inh2GKg46Stz5k2xnynWNR47JZcQErLqJGUAjOK0P0rQtmdIDGbiRqSV9D6mWxPwiZR9WGnwORC0i2ixmY0AFQvQyPKOzpRTmYps7Mtc0ooekxIyiU2okNf7dUzQp833We51NwN/CSNMiL0hC0TsKGV6PjK+Fbup/qkW/gOHHWQVKz0bVdC1BxqdbTnPSTyU5b6MVy8vj48c50q9aCgq8wZCrmMLN0mgu3mOYH9ogERKd+DA5k/6Iy2ZTpqH5UMRBhduAk7X98sxx/eycwl6o8/1iaQjKfqb00JTErDs+Uzb2ofXYDqux1UIYkCi/FgOANRGvskZQl7KXYMXRQkRqWH7l3mofHwXDqVYwtGGQvSUMJFFoHIXD+oL3u82qteEs/LNbJpz3CNBK7JSqmSbhjv9HZ4m5wii6kzWeBD6uHZPhL2yjXaggFLHDhjCHWkS6ijXQLvy4P4FraMTbpbnTf6+hZWasDKiU7mOINO6zMlBzSdG+DSp7JLOHWavEvsrkmnfKbcBTChYaIPfjuYHbSnnTx6Ji6aGzDgkzbNpRNZie7oe4/tWReZ6+NnEQA07P1fD1O6Ayp318hol5TUt5IcRKM7PKzEMtxBrHZCI5e4bCKsOlMHgeFzJP5WDojoCupEN6gT1zy8yJh05/B5WPNwuUzqLiqaxmTTOYE0W6XPy+u2Ft1vpWPmyEI/Lb1Xvud5tAfkp3dfzXdza/75qFYO5KJ6ZJ82olXKu4d2Fv9p4L/OyoAKJGqC3DmP3NNV434BTQ9r0g4g+0UAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Vue Logo&quot; title=&quot;Vue Logo&quot; src=&quot;/static/d417b5d92ee6897728326c76e7f1fd12/5a46d/image-3.png&quot; srcSet=&quot;/static/d417b5d92ee6897728326c76e7f1fd12/772e8/image-3.png 200w,/static/d417b5d92ee6897728326c76e7f1fd12/5a46d/image-3.png 300w&quot; sizes=&quot;(max-width: 300px) 100vw, 300px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;Once upon a time, a Chinese Google employee worked with AngularJS and decided he could do better. 😤 Well, he actually thought the framework was a bit too heavy-handed and opinionated. In &lt;a href=&quot;https://medium.freecodecamp.org/between-the-wires-an-interview-with-vue-js-creator-evan-you-e383cbf57cc4&quot; target=&quot;_blank&quot;&gt;his own words:&lt;/a&gt; &amp;quot;What if I could just extract the part that I really liked about Angular and build something really lightweight without all the extra concepts involved?&amp;quot; And thus Vue.js was born! In many ways, Vue is very similar to React and is therefore often seen as a direct competitor.&lt;/p&gt;&lt;h2 id=&quot;popularity&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#popularity&quot; aria-label=&quot;popularity permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Popularity&lt;/h2&gt;&lt;p&gt;Now, popularity isn&amp;#x27;t necessarily the best indicator for quality. However, it does give you a fair indication of which tools developers enjoy working with. One metric that is often taken into consideration are GitHub stars.&lt;/p&gt;&lt;h3 id=&quot;github-stars--omit-in-toc&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#github-stars--omit-in-toc&quot; aria-label=&quot;github stars  omit in toc permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;GitHub stars &lt;/h3&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:49%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABqklEQVQoz31SW47TQBAcEYmz8I0UaQ/CH+IqXAoJThCOkBVEQBQlm/Vz7bE9PZ5HF91OVmhhl5ZKnrHdNVXVY6y18N5DK+f8BMwZKTPafkDf9/C2xTiNaG3L0peD65CZ1wyYGONK3hlzPp+Xn7VSSv+QKkKMCCEgRkVETInJddmOlXzHWlqNYLXZbIyp6xrjOP6XkJlFyeOTQd6ynwddar0ty0IJzXa7NUYtO+eetfw3acwJjnqEeVQqniaHHz9/fa2q6pM4+CJOP5vT6fSiwpT/7CnOC1manewv0rqu4W/fb3E8HtE0DXa7HUxRFJim6VlClsaUIlwkzPMIDnSxLYwyLHae0LTlx/tz8a5u6g+Hw+G9GYbhiWV+JBJ1XlRR9MhKJIgpw4cImiML8kBB297odMuyNPv9/jKUabwqVJsS/pwCKDhppsWiJyf7JJBpJ41C5PEyJTkk3uhAJLbXonClzLi7vxNLA2xfSi4V2ocatnvA0HewwyjZeZBglvvqifQKieWc1Z3c4TURGcEroyULabJykqhy09JEEoEjzW2+QImuoCthfoHwN/PY5oyoCYg7AAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;GitHub stars for Angular, React and Vue&quot; title=&quot;GitHub stars for Angular, React and Vue&quot; src=&quot;/static/2776746123d292f2a1f29ffb5297f8cd/5a190/image-4.png&quot; srcSet=&quot;/static/2776746123d292f2a1f29ffb5297f8cd/772e8/image-4.png 200w,/static/2776746123d292f2a1f29ffb5297f8cd/e17e5/image-4.png 400w,/static/2776746123d292f2a1f29ffb5297f8cd/5a190/image-4.png 800w,/static/2776746123d292f2a1f29ffb5297f8cd/2bef9/image-4.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;As found on &lt;strong&gt;star-history.t9t.io&lt;/strong&gt;&lt;/figcaption&gt;&lt;p&gt;As you can see in the figure above, Vue and React narrowly rank 1st and 2nd, respectively. At a distant 3rd we have Angular. Based on this figure, you might have the impression that Angular is falling behind and that Vue is the next hot framework we should be looking at, right? Well, we need some more data to confirm our suspicion as simple GitHub stars own their own aren&amp;#x27;t as reliable.&lt;/p&gt;&lt;h3 id=&quot;npm-downloads--omit-in-toc&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#npm-downloads--omit-in-toc&quot; aria-label=&quot;npm downloads  omit in toc permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Npm downloads &lt;/h3&gt;&lt;p&gt;Let&amp;#x27;s turn towards npm downloads instead. This should give us a far better indication of each framework&amp;#x27;s popularity.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:48%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABnElEQVQoz32S627bMAyF/f4PNyz7M2BAu2JI7diWL9H9fnYUJ2kLDDPwgZJMHlIiu33fIaWEUgpaawghcOUeqHh8Hyuua/0vnbUWIYQnxhgE7zGrCGk8UvSoeoH1CmvcUcu/hI6kjS6l9OXnoyYTMlyISCmieCalsE0OJReUkmnvMCQ3Hzmi5oSuVVVK+YBOmUEshQnuZy1ZOXjsm9ANVl6mXyhqQg0GXYzxLpafgjeeSepNINPmVk1LGCyyUwdacO/4/0jUhSbYHMunoNu+HFdJHogWCJqwWXoE5EDeSQ8YcX+l3DrGN6Q6vDyCUmDADCgG8QrN1u0dTvSQYsQ6jRC7xUrXgfrbskLMAv21ot8SjC/s8jZBLT2u8x/I8QVXfcVmJBayOoO3XeN1dzgrh8l5TJyKi9LYvMHPacLrtmH0K86WOsmie1sFvo0DTuT7cMZpauseP8jp0uP3PmC2F4ymMRzWjpjMyPOFdoZyEpYNSa3LjiPhnUby7jZz7QkKO19j4pnng3N0QkL0Ed56BBeeRB+Oc1Ye6dem4C9ZzAnrxSi6+QAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Npm downloads for React, Angular and Vue&quot; title=&quot;Npm downloads for React, Angular and Vue&quot; src=&quot;/static/48801225d6ba48c72bf214b4ee9a92d9/5a190/image-5.png&quot; srcSet=&quot;/static/48801225d6ba48c72bf214b4ee9a92d9/772e8/image-5.png 200w,/static/48801225d6ba48c72bf214b4ee9a92d9/e17e5/image-5.png 400w,/static/48801225d6ba48c72bf214b4ee9a92d9/5a190/image-5.png 800w,/static/48801225d6ba48c72bf214b4ee9a92d9/2bef9/image-5.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;As found on &lt;strong&gt;npmtrends.com&lt;/strong&gt;&lt;/figcaption&gt;&lt;p&gt;Surprisingly, here we get a different result. Over the past 2 years, React appears to have clearly cemented its existing lead, with Angular and Vue trailing far behind as 2nd and 3rd, respectively. Alright, this isn&amp;#x27;t really making things clearer for us. There&amp;#x27;s only one thing that can help us: more data! 😉&lt;/p&gt;&lt;h3 id=&quot;website-statistics--omit-in-toc&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#website-statistics--omit-in-toc&quot; aria-label=&quot;website statistics  omit in toc permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Website statistics &lt;/h3&gt;&lt;p&gt;This time, we&amp;#x27;re taking a look at site visits using &lt;a href=&quot;https://www.alexa.com/&quot; target=&quot;_blank&quot;&gt;alexa.com&lt;/a&gt;. It should go without saying that these numbers below are only estimates. Nonetheless, they should be fairly reliable.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:38.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAIAAAB2/0i6AAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAxUlEQVQY041QXQ6DIAzm/ldZslMsO4i+bNGBSGkLBV3BR33wC2laUvh+zK/DWjtNkzaIKCL5Hgx2pA6da63bbRjnnNIehAel3u73YJSQ9TBDRIjE2uZMzCrhvK3fEmEASCFsACZGlCqBsoNsIXnM88qT8+rh/Fgt+bCM3499venxNCHiyuJVdS1SCkuB1Mac5UrppioDpiKyN88BZ4/EhESRWkVu4jWAK9k1ohuGcVmW5rmJKUV5elytppbZdUJbV677R6h/vM/UPUlPwoAAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Angular Website Statistics&quot; title=&quot;Angular Website Statistics&quot; src=&quot;/static/32bd0631dac6cf0558d784bc5e8b1dfc/5a190/image-6.png&quot; srcSet=&quot;/static/32bd0631dac6cf0558d784bc5e8b1dfc/772e8/image-6.png 200w,/static/32bd0631dac6cf0558d784bc5e8b1dfc/e17e5/image-6.png 400w,/static/32bd0631dac6cf0558d784bc5e8b1dfc/5a190/image-6.png 800w,/static/32bd0631dac6cf0558d784bc5e8b1dfc/159fb/image-6.png 1019w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:36.50000000000001%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAA5klEQVQoz51R204FIQzc//8dX/0I9cUfUM/RXUBY7rSMBTfHxEuMlkzaQJlO28Vai23boLWeWNcVxhjUWv+FpZSCGCNSSui9/xnDhicmcGcs+75PhUopjDiEAO89RqEuCb8RttYuxMOWITPL5yoPuZYZF7mLueKsLEJMFxWflTVRpY3G6fEJ189XuHd3R8s5ozQSCOkEwaWK1zCK1cH2lbC/EzovHWqHW3WDc3rAMmanrIfaE7RAHbAhIZU8FX/brhxqDGcsTi+bsB8tMzOIaC4li9IPH+fWiH+eIxOjSK4XlcRtLuUNGCwjdNNhuF8AAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;React Website Statistics&quot; title=&quot;React Website Statistics&quot; src=&quot;/static/1b9f85c030e207eeef89deaf3986ce93/5a190/image-7.png&quot; srcSet=&quot;/static/1b9f85c030e207eeef89deaf3986ce93/772e8/image-7.png 200w,/static/1b9f85c030e207eeef89deaf3986ce93/e17e5/image-7.png 400w,/static/1b9f85c030e207eeef89deaf3986ce93/5a190/image-7.png 800w,/static/1b9f85c030e207eeef89deaf3986ce93/2bef9/image-7.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:36.50000000000001%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAA6klEQVQoz5VRu1IEIRDk/3/MQBMDA8vAzCtZFvZ4DMNr22HPKxNP64BmCIae7hkVQsCyLDDGwDkHrTW2bUNrDbXWu6HmlVICUcYYQ7Bj3+8D5IzRMfYONcmmOmtXTLVE6SjAzDgy/yEbfaD3jutSRT5SzojckLkgEINLAQk+jIfz8Uj8lVC23SystnhaHvC8PYrCGGHcGSZUGM9YQzlgJ2JBpIvSWwp9TvBnwqt9wXt6gyKxp61Hrg0s/SytImYhErVV3lz4du+k5+F0wqdZfyxfp0lEF+tSIEtMNCMf0/6LMDsL7/33UAa+ALlEIvkYgKG8AAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Vue Website Statistics&quot; title=&quot;Vue Website Statistics&quot; src=&quot;/static/0111a1c57ad05782234d54eed79edf78/5a190/image-8.png&quot; srcSet=&quot;/static/0111a1c57ad05782234d54eed79edf78/772e8/image-8.png 200w,/static/0111a1c57ad05782234d54eed79edf78/e17e5/image-8.png 400w,/static/0111a1c57ad05782234d54eed79edf78/5a190/image-8.png 800w,/static/0111a1c57ad05782234d54eed79edf78/2bef9/image-8.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;Oh, what the fuck now! 🙄 This part is showing the exact inverse of what we stated previously. Among themselves, Vue is ranked 1st, Angular 2nd and React 3rd. However, looking a little deeper into the stats, it appears that Vue&amp;#x27;s largest audience originates from China, making up a whopping 47% of their traffic. Conversely, India and the US represent Angular and React&amp;#x27;s largest audience, respectively. They are both hovering around 27%.&lt;/p&gt;&lt;h3 id=&quot;subreddit-community--omit-in-toc&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#subreddit-community--omit-in-toc&quot; aria-label=&quot;subreddit community  omit in toc permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Subreddit community &lt;/h3&gt;&lt;p&gt;What can we conclude so far about this section? Well, that&amp;#x27;s difficult to say... Let&amp;#x27;s take a quick look at more data! 😅 This time I&amp;#x27;m looking at their subreddits in order to gauge the size of their international communities. This is what I&amp;#x27;ve found:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;https://www.reddit.com/r/reactjs/&quot; target=&quot;_blank&quot;&gt;React&lt;/a&gt; - 106.000 subscribers&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.reddit.com/r/Angular2/&quot; target=&quot;_blank&quot;&gt;Angular&lt;/a&gt; - 31.500 subscribers&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.reddit.com/r/vuejs/&quot; target=&quot;_blank&quot;&gt;Vue&lt;/a&gt; - 24.000 subscribers&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Hmmm... so here we are seeing a similar pattern to that of npm downloads. React clearly has the lead, with Angular and Vue ranking as 2nd and 3rd, respectively. Honestly, I could keep going on showing more data, but we got to stop somewhere, right?&lt;/p&gt;&lt;p&gt;Taking everything into consideration, it would appear that React is the most popular framework by far with Angular at a distant 2nd. Unfortunately, Vue&amp;#x27;s popularity is particularly hard to guess and appears to be deflated since the Chinese community doesn&amp;#x27;t have access to the same resources as we do, due to its infamous &lt;a href=&quot;https://en.wikipedia.org/wiki/Great_Firewall&quot; target=&quot;_blank&quot;&gt;Great Firewall&lt;/a&gt;. Alas, what you take away from these numbers is up to you. I&amp;#x27;m just the messenger. 😜&lt;/p&gt;&lt;h2 id=&quot;job-demand&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#job-demand&quot; aria-label=&quot;job demand permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Job Demand&lt;/h2&gt;&lt;h3 id=&quot;regular-jobs--omit-in-toc&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#regular-jobs--omit-in-toc&quot; aria-label=&quot;regular jobs  omit in toc permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Regular Jobs &lt;/h3&gt;&lt;p&gt;Now, this topic is a tad harder to scrutinise since it heavily depends on where you actually live. However, I don&amp;#x27;t want to leave it at that. Instead, I&amp;#x27;ve decided to analyse and contrast the two largest English speaking labor markets in the world: India and the United States. For this task, I&amp;#x27;ll be using &lt;a href=&quot;https://www.indeed.com/&quot; target=&quot;_blank&quot;&gt;indeed.com&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;The results below were obtained by including &amp;#x27;developer&amp;#x27; as a keyword in the search term. This way, we are sure to exclude other unrelated job postings. Might seem obvious, but this is something &lt;a href=&quot;https://medium.com/@TechMagic/reactjs-vs-angular5-vs-vue-js-what-to-choose-in-2018-b91e028fa91d&quot; target=&quot;_blank&quot;&gt;a popular Medium article&lt;/a&gt; neglected to do in their research, leading to highly skewed results in favour of React. Keep in mind, it&amp;#x27;s also impossible to prevent any AngularJS job postings from creeping in. Thus, it&amp;#x27;s best to adjust the numbers downwards accordingly.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:613px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:66.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAGQAAAgMBAAAAAAAAAAAAAAAAAAMBAgQF/8QAFQEBAQAAAAAAAAAAAAAAAAAAAgH/2gAMAwEAAhADEAAAAcjpaH1ipT//xAAaEAACAwEBAAAAAAAAAAAAAAACEgABAxET/9oACAEBAAEFAhMlYg05UHOvNKLVp//EABYRAAMAAAAAAAAAAAAAAAAAABARIf/aAAgBAwEBPwGIf//EABURAQEAAAAAAAAAAAAAAAAAABAh/9oACAECAQE/Aaf/xAAeEAABBAEFAAAAAAAAAAAAAAABAAIRIRAiMUFRcf/aAAgBAQAGPwLegYTNc31j2EHHisf/xAAbEAADAAMBAQAAAAAAAAAAAAAAAREhMUFRgf/aAAgBAQABPyFd1qInBoFW1WGjwEny9l+FMPkinxH/2gAMAwEAAgADAAAAEHQP/8QAFhEBAQEAAAAAAAAAAAAAAAAAAAER/9oACAEDAQE/ELRr/8QAFhEBAQEAAAAAAAAAAAAAAAAAEQAB/9oACAECAQE/EBRt/8QAGxAAAwEBAAMAAAAAAAAAAAAAAREhADFBYXH/2gAIAQEAAT8QKImJEpqeljgEcAgp4dwMGMNwdDzJDCZDBmAA5IO/d3dN/9k=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Angular 2 and AngularJS Meme&quot; title=&quot;Angular 2 and AngularJS Meme&quot; src=&quot;/static/92355dfb5ac145d33b899920332d0788/0c9cf/meme-jobs.jpg&quot; srcSet=&quot;/static/92355dfb5ac145d33b899920332d0788/e07e9/meme-jobs.jpg 200w,/static/92355dfb5ac145d33b899920332d0788/066f9/meme-jobs.jpg 400w,/static/92355dfb5ac145d33b899920332d0788/0c9cf/meme-jobs.jpg 613w&quot; sizes=&quot;(max-width: 613px) 100vw, 613px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Okay, it&amp;#x27;s not actually &lt;em&gt;that&lt;/em&gt; high...&lt;/figcaption&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:61%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAA7EAAAOxAGVKw4bAAACTElEQVQoz2WTz2sTQRTHF73oyYMH/w0vehU8KOJNaKCWepEiiKIigkfFi6iH+AM1QTdJ08RKG5VUZGtabGPVYCsKqUvECqHBYqjZJLvb3WR3dr7OzCabpA489u3Mdz77fTNvJUIIPM8TQSmFaZqwLEvk3fntwde4xjCMgXnOknjChxCyqNfr0HUjAHbXgpz6eaPRgKbVWE4GdAFQNy00mw2QtgWn3UZN05gD8z+g1TIZrIYtm4A4LejNTfbeHARyN93RYrCWQ4L3/tIpc8eVPDzXhN3W+3S+RuJ1iy/bNj5+eI/prIKrmV9IFf6AdFwRASQ+zGsBPy8Cy/txPzeKxOc72HJ8h8EZ8lCUWchPopBTUzj1VMWx8DdE8xuB0COuD1RHgFkJyO/C9VeHMZo8iHDuUudsKSS+oVwuIxKJYjI9gdizFzidKGE4UsTIoy+oaDZHgtdBja+gC2zLOwl0aS+uZY/g/NRRnEkeQnHjk/i4AKqqClmWkU4lkX6ewVhcxcloEUMPVrD62xBAlwNrCrw5Cd7CDuZwJ9JKCGczIZybPoD8WrYHrFarGE8kMJ6eRD4ZxpVYASceqxhjTjXT8R3yeu0y6soeuAyKRQZe3Ie7EzeZ7gIq9e8+sHsphUIBsXgcpVtDuHxvBscflvC2uCnWXHGGjjhDrN8G5S5zDDovofFmN17PL8FxKbsw0gNy8fraD1QzN/BybgXLFXtb2xCRi4v5O8MuZxjeaoiVJ7MZkwFJr236G7d/ENL7pXo69uR92Aki2srvQ675B6iNX2teK79bAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;United States and India job market results for React, Angular and Vue&quot; title=&quot;United States and India job market results for React, Angular and Vue&quot; src=&quot;/static/8e63d8d104dbc63eeefa58634c72f2b7/5a190/image-9.png&quot; srcSet=&quot;/static/8e63d8d104dbc63eeefa58634c72f2b7/772e8/image-9.png 200w,/static/8e63d8d104dbc63eeefa58634c72f2b7/e17e5/image-9.png 400w,/static/8e63d8d104dbc63eeefa58634c72f2b7/5a190/image-9.png 800w,/static/8e63d8d104dbc63eeefa58634c72f2b7/2bef9/image-9.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;On the left we have &amp;#x27;Murica 🇺🇸 and on the right India 🇮🇳. In the US, both React and Angular are in high demand, representing 49% and 45% of all job postings, respectively. Unfortunately, Vue developers get the short end of the stick and are limited to only 6% of the jobs, i.e. 1,750 open positions (Not nothing, if you ask me! 😉). In India, we see a similar story: Vue developers only have a small pool of jobs available to themselves, while Angular and React enjoy plenty. However, it appears that Indian companies are more in favour of Angular than React.&lt;/p&gt;&lt;h4 id=&quot;types-of-companies--omit-in-toc&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#types-of-companies--omit-in-toc&quot; aria-label=&quot;types of companies  omit in toc permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Types of companies &lt;/h4&gt;&lt;p&gt;While we&amp;#x27;re at it, I&amp;#x27;d like to briefly clarify in which types of companies these frameworks are used. Technically, all frameworks can be used in large enterprise-scale applications, and they are indeed used for them. For example, Google with Angular, Facebook with React and Alibaba with Vue. However, when you look at the job market, you&amp;#x27;ll notice a pattern. Angular is almost entirely used in large companies (which it is primarily intended for), while Vue and React are mainly used in small to medium sized companies, particularly in startups.&lt;/p&gt;&lt;p&gt;As I said though, it all depends on your location. Whatever you choose depends, more often than not, on what is used in the area you want to work in.&lt;/p&gt;&lt;h3 id=&quot;freelancing--omit-in-toc&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#freelancing--omit-in-toc&quot; aria-label=&quot;freelancing  omit in toc permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Freelancing &lt;/h3&gt;&lt;p&gt;However, regular jobs are only one part of the equation &lt;em&gt;mon ami&lt;/em&gt; 🧐. Mind you, we also got freelancing! Since freelancing gigs are mainly online, this should perhaps be more representative regardless of your location (for English speakers that is!). For this task, I&amp;#x27;ll be checking out &lt;a href=&quot;https://www.upwork.com/&quot; target=&quot;_blank&quot;&gt;upwork.com&lt;/a&gt; and &lt;a href=&quot;https://www.freelancer.com/&quot; target=&quot;_blank&quot;&gt;freelancer.com&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:62.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACjklEQVQ4y11T30sUURSe/8HXiIh6SYKgh16K6CkhsAItH2LxIZ8Ck0BKlnwxegoy25Z0yzUzH0yMfrhQG1RbC2ZEu2sbVvbTFGedmXVnG2dn5s79uvfOD3MPnDnMved+93z3fEdyXRfcCXFhWRZM04Rt2wjW/9/n0XEckVOrca+xdbIpT+KfwORSCSsrKzAMQ/xTSn0w7xA3mwHKckm4oijhegjIjqFmO1C1MixDF0nVvwZ0vSqq4aAckAVQYqGsqawyncUSqnqF5VYEswBUCqqoN0rZjWxvA5DnEX5/aK7v1CUhm5Cypqr48P4dxjMLeFzQ4JANKh5lIv7LhoypYhK59CU46TiIseYV4JKAMrAoq0iM3MWd4SGcS87gcP8cLj/5AVuAUvFu3JbXfiL6oBXt9w8gdeMgqp1bofUfBa3IonCXXSzBXsfCaBQPhwcwOTmB6Ngs2hLzaB7IIVVQBJDFus5tMBNFx8g+nHnUjNTQEaxf3IvFrm3Qpvo8NsSBZMvfsNq9E0bvbsyN9KL7Xg4nBz+hJV7A1fRvP5HCsHT0TEfQMdGE86njmEk2odqzB0sXGrF6vY01zBFvLhFW7utYD2I3x9CVyOJUoojIrSKOxfJIZJYEIH9Di/lkJoJsuhH6mwa46S1Q+/ZDYcXow6c9ysEbxrJlHBr4jBND8wKwJZ5H67UZfJU9PTq25TX3zxXgGTvynHlWgjG6C6WzO1DLT3tMHNvToc10mHi5iPbbRbQNfkTn+Be8/V7xqvO7zGXBQa2FXpSfNoC+kOC+2g5zdmJDZvWTsqpb+KWYMC3iS2bzpAhxiy4tg+o5UFveNFEhIJ9T4ktEOLvNqZtRz9kaYXNOfVHzyCQV7HPgfyIbpkpjI/mVAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Upwork and Freelancer results for React, Angular and Vue&quot; title=&quot;Upwork and Freelancer results for React, Angular and Vue&quot; src=&quot;/static/47b1c8a1613236a1ac0a19ca854fa840/5a190/image-10.png&quot; srcSet=&quot;/static/47b1c8a1613236a1ac0a19ca854fa840/772e8/image-10.png 200w,/static/47b1c8a1613236a1ac0a19ca854fa840/e17e5/image-10.png 400w,/static/47b1c8a1613236a1ac0a19ca854fa840/5a190/image-10.png 800w,/static/47b1c8a1613236a1ac0a19ca854fa840/2bef9/image-10.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;On Upwork, React is King. On the other hand, while it does indeed occupy the largest percentage on Freelancer, it&amp;#x27;s nearly not as dominant as on Upwork. Keep in mind that Upwork is much larger than Freelancer with over 1,400 listed React jobs compared to only 159. Furthermore, Vue appears to be faring much better than before. Unfortunately, this means that Angular is the biggest loser here. 😞 Yet, considering that you mainly freelance for SME, it would make sense that Angular isn&amp;#x27;t particularly popular here.&lt;/p&gt;&lt;h2 id=&quot;technical-features&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#technical-features&quot; aria-label=&quot;technical features permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Technical Features&lt;/h2&gt;&lt;p&gt;So we talked about which framework developers enjoy, which one company&amp;#x27;s want, but what about you? What do you think about them? Well, in this section I&amp;#x27;ll be giving a quick technical overview of each framework so you can answer that question for yourself.&lt;/p&gt;&lt;h3 id=&quot;angular--omit-in-toc-1&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#angular--omit-in-toc-1&quot; aria-label=&quot;angular  omit in toc 1 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Angular &lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;TypeScript&lt;/strong&gt; - Angular uses TypeScript, a superset of JavaScript that gives you optional static typing. It allows you to spot common errors as you type them and is a boon for maintainability in very large projects. Programmers coming from OOP languages will feel quite familiar with it.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Component-based Architecture&lt;/strong&gt; - All three frameworks, including Angular, use components to build their application. A component is a visual part of your screen, such as a shopping cart 🛒. It can receive input, which generates some type of change, typically in the UI. In Angular, the logic behind a component and its visuals are kept separate, that is, the JavaScript and HTML.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Size&lt;/strong&gt; - Angular&amp;#x27;s file size is the largest out of all the other frameworks with 566KB. Yet, unlike the others, it&amp;#x27;s a complete solution from the get-go.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Steep Learning Curve&lt;/strong&gt; - Angular is particularly hard to learn for many reasons. 😫 First off, you have to learn TypeScript and there&amp;#x27;s no way around it. You are confronted with many new concepts and tools, such as modules, services, templates, RxJS, etc. Moreover, it&amp;#x27;s quite verbose and solely aimed at creating enterprise-scale web apps, thus confronting you with the MVC pattern right from the start.&lt;/li&gt;&lt;/ul&gt;&lt;h3 id=&quot;react--omit-in-toc-1&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#react--omit-in-toc-1&quot; aria-label=&quot;react  omit in toc 1 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;React &lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;JSX&lt;/strong&gt; – I told you before that Angular likes to keep the visual and logic of a component separate. However, React likes to mingle it together. The entire app is practically written in JavaScript. To do this you use JSX, which is a syntax extension to JavaScript that allows you to write things like this: &lt;code&gt;const element = &amp;lt;h1&amp;gt;Hello, world!&amp;lt;/h1&amp;gt;;&lt;/code&gt;. And no, that isn&amp;#x27;t HTML. It&amp;#x27;s JSX. Oh, and you can also use TypeScript if you feel the need to!&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Virtual DOM&lt;/strong&gt; - React uses something called a virtual DOM. In layman terms, React keeps a lightweight copy of the DOM that it modifies as needed. Upon each modification, it checks for any differences between the virtual and real DOM and updates only these differences in the latter. This allows for an increase in performance.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Size&lt;/strong&gt; - As opposed to Angular, React is only 97.5KB large. However, this is due to Facebook keeping the library bare-bones. Accordingly, in order to create a proper web application following a MVC pattern, you will need to import third-party libraries such as React router and Redux. On the other hand, unlike Angular, the framework isn&amp;#x27;t restricted to SPAs and can simply be added to an existing website to increase interactivity.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Moderate Learning Curve&lt;/strong&gt; - The library itself does not have such a steep learning curve. JSX, while not fancied by all developers, isn&amp;#x27;t particularly hard to learn either. However, once you want to start building actual web applications, it can get a bit harder to learn. In particular, state management with Redux is known to be quite difficult to grasp.&lt;/li&gt;&lt;/ul&gt;&lt;h3 id=&quot;vue--omit-in-toc-1&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#vue--omit-in-toc-1&quot; aria-label=&quot;vue  omit in toc 1 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Vue &lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Virtual DOM&lt;/strong&gt; - Same as React, Vue uses a virtual DOM to manage and update visual changes in its application.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;TypeScript&lt;/strong&gt; - Vue offers developers the option to use TypeScript. However, unlike Angular, it doesn&amp;#x27;t have complete support yet. Fortunately, Vue is set to fully support TS in the future with its third iteration.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Size&lt;/strong&gt; - Similarly to React, Vue is quite small compared to Angular with 58.8KB, as its core library only provides the bare 🐻 necessities. However, same as React, you can easily drop it into any existing website and you will have to import additional libraries to create a full-fledged Single Page Application.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Separation of Concerns&lt;/strong&gt; - Unlike React, Vue does not mix the UI and logic of a component. Rather it keeps them separate, like Angular, yet does so in a more compact way, resulting in far fewer files for each component.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Easy to Moderate Learning Curve&lt;/strong&gt; - Regarding the learning curve, it&amp;#x27;s very similar to React. However, it&amp;#x27;s arguably easier to learn due to its separation of concerns and its less convoluted state management library, Vuex.&lt;/li&gt;&lt;/ul&gt;&lt;h2 id=&quot;future-prospects&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#future-prospects&quot; aria-label=&quot;future prospects permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Future Prospects&lt;/h2&gt;&lt;p&gt;Before I let you go, it&amp;#x27;s important we talk about each framework&amp;#x27;s future potential. I mean, would you start using any rinky dink framework you come across for an application, especially for an enterprise? Of course not! There needs to be ongoing support for anyone to actually even consider using it.&lt;/p&gt;&lt;p&gt;Google has announced that it will keep supporting Angular for the foreseeable future. Particularly, after the fiasco of rewriting AngularJS without consideration of its user base, Google has pledged to do twice-a-year upgrades and that each version be backward-compatible with prior releases. So if you decide to use Angular, rest assured that in several years, it will still be actively supported. Likewise, Facebook has pledged to support React for many years to come. So there shouldn&amp;#x27;t be much of a problem here, right? What isn&amp;#x27;t safer than a multibillion dollar company supporting your favourite framework?&lt;/p&gt;&lt;p&gt;The problem of a framework&amp;#x27;s future becomes more apparent when we look at Vue. It&amp;#x27;s the brainchild of one individual: Evan You. What happens if he dies or suddenly abandons the project? Those are real concerns. However, if that were to happen the framework would still persist as there&amp;#x27;s an entire team behind the framework now. Yet, with its creator no longer in charge of it&amp;#x27;s long term vision, it&amp;#x27;s hard to say how much and how long it will stick to that vision.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:62.5%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAEDBf/EABQBAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhADEAAAAdQsxFQ//8QAGBABAQEBAQAAAAAAAAAAAAAAAQIAEhH/2gAIAQEAAQUC59zGkQIkOSjf/8QAFhEAAwAAAAAAAAAAAAAAAAAAARBh/9oACAEDAQE/ATF//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAGxAAAgEFAAAAAAAAAAAAAAAAAAEhEBExkeH/2gAIAQEABj8CcrZ0yQWdP//EABwQAQACAQUAAAAAAAAAAAAAAAEAESEQMVFhgf/aAAgBAQABPyHfC81FA5A7UOlPI6BhbiiNl6f/2gAMAwEAAgADAAAAELDP/8QAFxEAAwEAAAAAAAAAAAAAAAAAARAxQf/aAAgBAwEBPxCjhf/EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8QP//EABwQAQADAQADAQAAAAAAAAAAAAEAESExYXGB8P/aAAgBAQABPxADt1RK785BY3lZtlBAOdFe2PtLTf2Qtr8PhlT/2Q==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;React and Angular vs Vue Meme&quot; title=&quot;React and Angular vs Vue Meme&quot; src=&quot;/static/cbf18317406da615a5b9b55326e096b0/4b190/meme-battle.jpg&quot; srcSet=&quot;/static/cbf18317406da615a5b9b55326e096b0/e07e9/meme-battle.jpg 200w,/static/cbf18317406da615a5b9b55326e096b0/066f9/meme-battle.jpg 400w,/static/cbf18317406da615a5b9b55326e096b0/4b190/meme-battle.jpg 800w,/static/cbf18317406da615a5b9b55326e096b0/4a8d4/meme-battle.jpg 802w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Want to make a bet? 😏&lt;/figcaption&gt;</content:encoded></item><item><title><![CDATA[A Beginner Introduction To Angular (2+) - An Overview]]></title><description><![CDATA[I've recently decided to pick up Angular 7, a JavaScript framework, as the company I'm working at is using it in its stack. It packs quite…]]></description><link>https://dilshankelsen.com/beginner-introduction-to-angular-overview/</link><guid isPermaLink="false">https://dilshankelsen.com/beginner-introduction-to-angular-overview/</guid><pubDate>Tue, 16 Apr 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;I&amp;#x27;ve recently decided to pick up Angular 7, a JavaScript framework, as the company I&amp;#x27;m working at is using it in its stack. It packs quite the punch in terms of its capabilities and can be quite overwhelming for many newcomers. I thought I&amp;#x27;d share my view on how Angular operates and hopefully ease your way into the framework. 😉&lt;/p&gt;&lt;h2 id=&quot;angularjs-vs-angular-2&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#angularjs-vs-angular-2&quot; aria-label=&quot;angularjs vs angular 2 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;AngularJS vs Angular (2+)&lt;/h2&gt;&lt;p&gt;No discussion on Angular is complete without the conundrum that is AngularJS and Angular. In case you aren&amp;#x27;t aware of it or just confused about what&amp;#x27;s going, here&amp;#x27;s the gist of it.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:56.49999999999999%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIDAf/EABYBAQEBAAAAAAAAAAAAAAAAAAIBA//aAAwDAQACEAMQAAABSWjVBTK//8QAGRAAAgMBAAAAAAAAAAAAAAAAABABERMx/9oACAEBAAEFAtNCjilf/8QAFxEAAwEAAAAAAAAAAAAAAAAAAAERE//aAAgBAwEBPwFOGh//xAAXEQEAAwAAAAAAAAAAAAAAAAAAARET/9oACAECAQE/AaZy/8QAFhABAQEAAAAAAAAAAAAAAAAAAREg/9oACAEBAAY/AowNf//EABoQAQADAQEBAAAAAAAAAAAAAAEAESExEEH/2gAIAQEAAT8hsDUaW7PoKq3uTKD2oBvg0n//2gAMAwEAAgADAAAAEHD/AP/EABcRAAMBAAAAAAAAAAAAAAAAAAABEUH/2gAIAQMBAT8Qe0RGn//EABcRAQEBAQAAAAAAAAAAAAAAAAEAEUH/2gAIAQIBAT8QT2Wd2//EABsQAQEAAwEBAQAAAAAAAAAAAAERACExQVFx/9oACAEBAAE/EGubsJNm5zKWVQK+tdxQDwWbwCwCxZ7jofuaq7Z7n//Z&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;AngularJS and Angular Spiderman Meme&quot; title=&quot;AngularJS and Angular Spiderman Meme&quot; src=&quot;/static/7f6638704c0f56b31934a590b3f69139/4b190/meme-spiderman.jpg&quot; srcSet=&quot;/static/7f6638704c0f56b31934a590b3f69139/e07e9/meme-spiderman.jpg 200w,/static/7f6638704c0f56b31934a590b3f69139/066f9/meme-spiderman.jpg 400w,/static/7f6638704c0f56b31934a590b3f69139/4b190/meme-spiderman.jpg 800w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;Back in 2009, Google released AngularJS, arguably the world&amp;#x27;s first framework for creating Single Page Applications. Needless to say, it became quite the hit, even to this day. Yet, due to its imperfections, Google decided to rewrite the framework from scratch, and released it in 2016 under the name Angular (version 2). As you might have guessed, many invested companies and individuals didn&amp;#x27;t take this decision too lightly. Accordingly, Google will still support AngularJS till July 1st, 2021.&lt;/p&gt;&lt;p&gt;Whenever anyone talks about AngularJS or Angular 1, they are referring to the initial framework released in 2009. On the other hand, Angular 2 (or 7 at the time of this writing) is used to refer to rewritten one. However, don&amp;#x27;t be fooled! Despite the name, these are two completely different frameworks. Knowing one doesn&amp;#x27;t make you proficient in the other. Unfortunately, this confusion has also carried over into question and answer site where it can become difficult at times to differentiate between both frameworks. 😫&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;Tip: Search on google with `-angularjs` option to omit the search results with the 1.x version&lt;/p&gt;&lt;a href=&quot;https://blog.risingstack.com/angularjs-to-angular-history-and-tips-to-get-started/&quot; target=&quot;_blank&quot;&gt;@RisingStack&lt;/a&gt;&lt;/blockquote&gt;&lt;p&gt;Oh! And in contrast to AngularJS, Angular 2+ implements Typescript into its projects, which is a superset of Javascript. Another way of saying Javascript with extra features. 🤓&lt;/p&gt;&lt;h2 id=&quot;angular-cli&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#angular-cli&quot; aria-label=&quot;angular cli permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Angular CLI&lt;/h2&gt;&lt;p&gt;An indispensable tool for any Angular developer is the Angular CLI. It&amp;#x27;s a command-line interface tool that will automate many operations of the development process. Among others, this includes things ranging from routing, building components, configuring, deploying, testing and installing 3rd party libraries such as Bootstrap.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:35.50000000000001%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAA7EAAAOxAGVKw4bAAABvElEQVQoz2XPy2tTQRTH8bsXXFi1Nmma3DwkNMl9zNybh3nY4gO6EGKVxi78FwRBFEVtEkjUlQv/hmIXrvwLFERxIRWtrUSNNjdUG5KAYN1+ndqAqIsP5xxmOPMbbUPaePNlOpVzeJWF/12osHm2zLqwWLcMxRwx/pn3aGvZNPevXOLW0mWatTp363Wa1Sp3ajXuKc1GgwfXrvIuLfkgTFquoOXYo6pI6y/aqjDQJ3xMBIIEg0GO+Hzo4TBBXcc/Ocm+A2OkxsdZkyZfspLOwjzdi4t458tsnpmjnRF8kgZtdd52bbS3rol9NIah4grLQgqBIyXxeJzI7lL1SD4S5n3aoptTCwtpvNk8Xin7Wyfv0j09i3eyRCejFm5kLKxohOlEAiOZJKVEVcIplS40FVDJA+TDOh/VvW8zOQa3rzNs1Ogv3VC1yrB2k68nCmwVXLaOCbRWziYWCuGPxghEIoyp7x/0+zmkFh5W9qte6CE+Z036czPsvHjGzpvXfH/6hB+rr/j58jm9UwW2cya9ooO2XZI8kkmWxTQPRYIVJ8mK/GNZeeym6JUchscd+gWbQVEwUP2gKPfm3X7kF4szLH6ussZNAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Angular CLI Website Screenshot&quot; title=&quot;Angular CLI Website Screenshot&quot; src=&quot;/static/af8cc62e1d595869f9b79acf7eea8e23/5a190/image-1.png&quot; srcSet=&quot;/static/af8cc62e1d595869f9b79acf7eea8e23/772e8/image-1.png 200w,/static/af8cc62e1d595869f9b79acf7eea8e23/e17e5/image-1.png 400w,/static/af8cc62e1d595869f9b79acf7eea8e23/5a190/image-1.png 800w,/static/af8cc62e1d595869f9b79acf7eea8e23/2bef9/image-1.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;As found on &lt;a href=&quot;https://cli.angular.io/&quot; target=&quot;_blank&quot;&gt;https://cli.angular.io/&lt;/a&gt;&lt;/figcaption&gt;&lt;p&gt;Here&amp;#x27;s a basic overview of the most common commands you&amp;#x27;re likely to use during your Angular development:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;ng version&lt;/code&gt;; Indicates Angular CLI version.&lt;/li&gt;&lt;li&gt;&lt;code&gt;ng help&lt;/code&gt;; Outputs available commands with short descriptions. Moreover, you can inspect any command in detail by following it with &lt;code&gt;--help&lt;/code&gt;, e.g. &lt;code&gt;ng build --help&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;ng new &amp;lt;name&amp;gt;&lt;/code&gt;; Creates a new Angular app&lt;/li&gt;&lt;li&gt;&lt;code&gt;ng generate &amp;lt;schematic&amp;gt;&lt;/code&gt;; Generates files based on the &lt;a href=&quot;https://angular.io/cli/generate#ng-generate&quot; target=&quot;_blank&quot;&gt;specified schematic&lt;/a&gt;, which ranges from &lt;code&gt;interface&lt;/code&gt; and &lt;code&gt;component&lt;/code&gt; to &lt;code&gt;module&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;ng build&lt;/code&gt;; Outputs the compiled app into a directory called dist&lt;/li&gt;&lt;li&gt;&lt;code&gt;ng serve&lt;/code&gt;; Builds and serves the app, including rebuilding upon any file changes&lt;/li&gt;&lt;li&gt;&lt;code&gt;ng lint&lt;/code&gt;; Executes linting tools on the app code&lt;/li&gt;&lt;li&gt;&lt;code&gt;ng test&lt;/code&gt;; Runs unit tests in the project.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Furthermore, you can usually abbreviate each command (including schematics) by their first letter or two, e.g. &lt;code&gt;ng h&lt;/code&gt; and &lt;code&gt;ng g c ComponentName&lt;/code&gt;. Of course, you should check out &lt;a href=&quot;https://angular.io/cli&quot; target=&quot;_blank&quot;&gt;the documentation&lt;/a&gt; for more details, particularly regarding the many options that follow each command. 🧐&lt;/p&gt;&lt;h2 id=&quot;project-structure&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#project-structure&quot; aria-label=&quot;project structure permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Project structure&lt;/h2&gt;&lt;p&gt;Compared to other libraries and frameworks such as React and Vue.js, Angular is known to be quite opinionated, that is, you need to do things the Angular way. Naturally, your project structure isn&amp;#x27;t an exception. Below are screenshots of an Angular project created with the Angular CLI, ranging from highest (leftmost) to the lowest (rightmost) directory level.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:58.00000000000001%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAA7EAAAOxAGVKw4bAAACvUlEQVQoz22TyW7TUBSGr2gEEhIb2LRCVeI5nod4iq/nIVUlBlUCnqM8A6/BGldigaiEeA264FHSAonv4diNWGHpbGLf//+/89+QjMaJH4QvwzB6h5PyvJDbpqfR85Z2r17QOMreeKv4gqYNpbSmrhsslbWW05xSnhNez+eLtziZKEopTka6IoZ4nUAQhMx1PRAEEVRF+2YnwZ+kK8H3ImaaDliWC5bt7VRZ+awlMaR1A45lA55htu2ALCuAgkDqOmRhGIKm6aDrxl6SZOA44dowjG1eZJAkFDD1ZCSK4t3pfNEnaHxWN/sKRTfdBtq2Y6uVz/A7RszUYyvfZ4ZhjjOMLhwvXTvWclvnARRlzZqmxZTWKHoni8IVr1oQp8VQ5DkripJlWT4J4llGsiiEqqoBXyCWNYxJ5igYGvL2ogwgLWqWpRk7IE2Ci6UJES2GuqoAzcBx3AMBIp+bLjRtOwmqqoYJRThFZGoZ2zdlhoIVowllhwN3kqxcxbYNZ207BGE0YeK7Kd2UMFD1yaXGfeR5MYzFiDx/Lajq1k0SKIuCxfGa4X4nZFFWPqWeBU2e/c6LcmdZ9k6SpGlQcEccXduj4H6zOdvjHnZjAbIofuVl9db219DU9dj+fUJJvlUk6YtqObBOC+ja+yBoiKVqEzbRbQ9/rKHDtlz3sAue+y6b3jYoNlBXJfP9gHEcPyIPoiB8mItq7/phn1LaU5r2eEN6QRB6TNkT3Y1usJQbxL1RFOUHHvopicJHTlZ/2UECY5OGaY0tD+OVQsNgLshEMx3iuS5BKrJcLgmSERQk5Ckhj8hhjo9PHp8+OSIyv1gLigZOmDDEYt3mjKHpMO4R/xX585NjopvWwziOZxEOljlDoxkKzsizI/LvuXx/SRAXBTlfkFVwgoRVZYUXP2LjDVCU5ZgwRTyCJA/If56/aTM1uFEfsGkAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Angular Folder Structure&quot; title=&quot;Angular Folder Structure&quot; src=&quot;/static/9049af0d66c2380cd285d9f1669722f2/5a190/image-2.png&quot; srcSet=&quot;/static/9049af0d66c2380cd285d9f1669722f2/772e8/image-2.png 200w,/static/9049af0d66c2380cd285d9f1669722f2/e17e5/image-2.png 400w,/static/9049af0d66c2380cd285d9f1669722f2/5a190/image-2.png 800w,/static/9049af0d66c2380cd285d9f1669722f2/2bef9/image-2.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Angular Folder Structure&lt;/figcaption&gt;&lt;p&gt;Don&amp;#x27;t be frightened by the number of files. You won&amp;#x27;t touch the vast majority of them during your app development. In fact, you&amp;#x27;ll be spending most of your time in the &lt;code&gt;src/app&lt;/code&gt; directory, working on components, modules, services, etc. However, it doesn&amp;#x27;t hurt to do a quick rundown of some Angular specific files and folders, right? 🤷‍♂️ So, starting from left to right on the screenshots above:&lt;/p&gt;&lt;p&gt;Level 1&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;e2e/&lt;/code&gt;; End-to-end testing folder&lt;/li&gt;&lt;li&gt;&lt;code&gt;angular.json&lt;/code&gt;; CLI configuration for all projects in the workspace&lt;/li&gt;&lt;li&gt;&lt;code&gt;tsconfig.json&lt;/code&gt;; TypeScript configuration for apps in the workspace&lt;/li&gt;&lt;li&gt;&lt;code&gt;tslint.json&lt;/code&gt;; TypeScript linting configuration for apps in the workspace&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Level 2&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;app/&lt;/code&gt;; Contains the app&amp;#x27;s logic and data, e.g. components, services, etc.&lt;/li&gt;&lt;li&gt;&lt;code&gt;assets/&lt;/code&gt;; Holds your app&amp;#x27;s assets such as image and CSS files&lt;/li&gt;&lt;li&gt;&lt;code&gt;environments/&lt;/code&gt;; Contains configurations for your build and development environments&lt;/li&gt;&lt;li&gt;&lt;code&gt;browserlist&lt;/code&gt;; Used by autoprefixers to adjust CSS browser support&lt;/li&gt;&lt;li&gt;&lt;code&gt;index.html&lt;/code&gt;; The main HTML page that is served by your site&lt;/li&gt;&lt;li&gt;&lt;code&gt;karma.conf.js&lt;/code&gt;; A test runner used in Angular&lt;/li&gt;&lt;li&gt;&lt;code&gt;main.ts&lt;/code&gt;; The main entry point for your app during compilation&lt;/li&gt;&lt;li&gt;&lt;code&gt;polyfills.ts&lt;/code&gt;; Provides polyfill scripts for browser support&lt;/li&gt;&lt;li&gt;&lt;code&gt;test.ts&lt;/code&gt;; The main entry point for your unit tests&lt;/li&gt;&lt;li&gt;&lt;code&gt;tsconfig.app.json&lt;/code&gt; &amp;amp; &lt;code&gt;tsconfig.spec.json&lt;/code&gt;; Inherit from the workspace-wide &lt;code&gt;tsconfig.json&lt;/code&gt; file&lt;/li&gt;&lt;li&gt;&lt;code&gt;tslint.json&lt;/code&gt;; Inherits from the workspace-wide &lt;code&gt;tslint.json&lt;/code&gt; file&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;As I&amp;#x27;ve mentioned before, you won&amp;#x27;t have to deal with most of these files and folders until your application reaches a certain level of complexity. Rather, and particularly as a beginner, you will mainly focus on the &lt;code&gt;src/app&lt;/code&gt; directory, which I&amp;#x27;m about to get into. 👇&lt;/p&gt;&lt;h2 id=&quot;application-architecture&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#application-architecture&quot; aria-label=&quot;application architecture permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Application architecture&lt;/h2&gt;&lt;p&gt;So how is an Angular app structured? Let&amp;#x27;s take a look at the image below.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:39.49999999999999%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACcUlEQVQozzWSa0jTYRTGH/BTVPohGwQhsqF5yczpKvICQpi0skAyNLw11MQkb1Pc1HROlzEv2IT6UH2REFFLt7mJiZcwTOf237xNzIQsqKCg8rLZ3On9F334wcP7nvfhPO85KOp+j81fJBhecnWal12VL+w7jX227eZ+21Zzn31X3eskJaPmP332HTV/xzPAuRsGHVQ/wJFGWg20maWAsGwyrKRnI08x8KECAu1RIsLTmZ/Qv/kMLdNr4gjRxpko0YbktOhdZJiwnZ0NTn9Dj3UdfK28G4G6MWVa52hqotYQJEZAFefxL+M8gnLOyzRJVJwUN9ZAAGYL5Oes12VkvZrFyCRezxZWSQg+uPkauG8KSWkw+lKNHlu1erhVRl8PIlWLFPvA6T3f4vSI1UsUorRe9FF9Al1OgCU9L8aeUUArhfL9lYJyrz09n+Yy8sVfxBpUTPug0eAv1Yz4U9dU5H7nRCg1mY4QREoHxTSv/DU9UbdAwYr5ZNzbBKUlw5pbLFmU3SUnb5hf5uX1fO4d8dfYVihmfFmHAdK2MSE9nAj3dowHUesrIfGR3YIKRjm3e7ySo4g6a9KBJtZhahIs2UXRttQcsqVkum3Xsjw2PvJteQwhHNlTfOTgKywm1Rrwo86AHZXRbw+JGktEnNpyMpYRr547ldzy9lB4+xpceRmYUusOribEBZqfT5aan43I2L8e7mCDGJ75jn7u31CKnuCYbqz2km5MFq81ikLBjBBdb4GYcVZlwQXNLIJb1/H7Vhqq2YPR7nH0fqSol879rsF1yhladJXoHdslQ47tUv3CXrFpmQrZ6jw2rZLfo3EF/gCS81pwSKRZoQAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Angular Components, Services and Modules Relation&quot; title=&quot;Angular Components, Services and Modules Relation&quot; src=&quot;/static/68aa2a86f9ed09f0f7b1ba108bc12a0a/5a190/figure-1.png&quot; srcSet=&quot;/static/68aa2a86f9ed09f0f7b1ba108bc12a0a/772e8/figure-1.png 200w,/static/68aa2a86f9ed09f0f7b1ba108bc12a0a/e17e5/figure-1.png 400w,/static/68aa2a86f9ed09f0f7b1ba108bc12a0a/5a190/figure-1.png 800w,/static/68aa2a86f9ed09f0f7b1ba108bc12a0a/2bef9/figure-1.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;&lt;p&gt;  Components, Services and Modules relation. &lt;em&gt;Source:&lt;/em&gt; &lt;a href=&quot;https://scrimba.com/p/pQxesM/crdBkAv&quot; target=&quot;_blank&quot;&gt;&lt;em&gt;Dan Wahlin&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;&lt;/figcaption&gt;&lt;p&gt;Any Single Page Application is made up of components, whether it be a menu bar, header, buttons or a grid. However, components represent the UI of your webpage. On the other hand, Angular has &lt;em&gt;services&lt;/em&gt; which represent the business logic of your app. This includes anything from form validation to API requests. Furthermore, these can be reused throughout your application in your components. Lastly, Angular uses modules to organise and register these components and services according to their functionality.&lt;/p&gt;&lt;p&gt;However, this is a very superficial explanation of what&amp;#x27;s going on. So let&amp;#x27;s dive in a bit deeper and take a look at the next image! 🏊‍♂️&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:75.49999999999999%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAD/UlEQVQ4y3WRe0ybVRjG37ZAEWKirpMWaMFudMkWE2KWVeN0JszNmBWQRAg0XBYIZslKJ7iEuQHRiM7Lxs0JEzaD8zogCNRRYMUOOhAo0HJpC6UX+rVfrys1JvrHEl/P92kW+cM3+eU953znefJ+z4GLA8nYOMzHhqFEbCQ0DCXg+5on8BNtJl4eFZOegR/dScXPxrNYPtZK2G9Nw8nkLv9fDZ/1eLcvaQkaBpOiTZr4aNNIAkvjSFy0efSpaKtOtnNlIiPWqsuKXZmQkC6LtU0eiLXc3Rdr0WXtvKdJIncf6SLE44+LA49NgqpVLFF1iCSq9lSCSFLTkS4505KYIVWDiFcOQqiHJ1NeBPmeo3CYWXPKQCivh7SadkGGqj2N1fyjE2WcvZouBHVHGpzrfBrUX6TAua69IGveB6b+BLBdy7zguJ427r4hvb1+/dD4eqdszN0t6dvqEo+ttcvyB66lQnaXgNWx2s4UIEMBvN0l5NT27OHUdgs46rY0TnTi8Tg0JYLty8xF9810dPVmYWDwFaT7X0DXV8+g66YYTZ37P6W6pTDRLY6v7RGw2jrSiSkH/lsVhTmA9wFwCoBq4xyOfA65D3sgZ/ICp3zqEq/04Q3ICXWAwtUSJ6XbAQi7DZiqrq6GyspKqKqqAmHWywClfg6UBgAKrYeg2HECSiJHIed2ERwfeBOUZF3ieBWKljKh2ApQbGF1DIwH4wVut5sbDAZ5gUCA5/V6eecHfHyocIK4bva+uN6MzzVbUH7Zis8Tsj+woOj8MkrrptugaArqv7fwGY3P5+P5/X6ew+HggdPpBGLIEolEAPF3aNORCUsn1XtrZo2yd6bnDjYsTx68tPjL/rp7C4KaX+egcExxdcQG+FcEwuEwEEMWu90OsLa2doxMmedyuU6RrvB5vQqvj1b8GXafzJdnykkqia8PIRQvIJNQ8mvZQnmYsp+wWG25xECxvb2tYPrm5qZiZWVFATRNRywWC5rNZiSHuLy8jIuLi+ihvOihQ7lmwygMWjF+yI1x+p96YdVqP07TfoxGo0gGwIX5eaQoit17PB5kMvyQLH4kvddms/1gNBr7Se8l+z6r1fpsJBIGw+wCV3tXz6VpLxiXlg54KOq7YCh0y+FyfntvenqY8vm+DgSD3ziczlskM3wEKRFBTKbcdf5/kIonHNl1RsLUhUKhHYPBQBNCer0+RF7LRx7oNxLFsY2NDSZsrr6gjKsrKAPCEWNBecj0Rrl/9bQqYDqterBS8pbflF/2YK6gfB62trbWyS+iVqtl0Wg0ODMzgyRsJs+TJFtYXV3lafOU3Dt5Svg5T/mSIVeJswolLpSeQWPFWZwrqsTZUyWozy2h/ga/ZIJl+ZI5iQAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Angular Application Architecture Illustration&quot; title=&quot;Angular Application Architecture Illustration&quot; src=&quot;/static/7d8b02b8a54c0a641553f18e0277082e/5a190/figure-2.png&quot; srcSet=&quot;/static/7d8b02b8a54c0a641553f18e0277082e/772e8/figure-2.png 200w,/static/7d8b02b8a54c0a641553f18e0277082e/e17e5/figure-2.png 400w,/static/7d8b02b8a54c0a641553f18e0277082e/5a190/figure-2.png 800w,/static/7d8b02b8a54c0a641553f18e0277082e/2bef9/figure-2.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;&lt;p&gt;  Angular Application Architecture. &lt;em&gt;Source:&lt;/em&gt; &lt;a href=&quot;https://scrimba.com/p/pQxesM/crdBkAv&quot; target=&quot;_blank&quot;&gt;&lt;em&gt;Dan Wahlin&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;&lt;/figcaption&gt;&lt;p&gt;Every Angular application contains at least one module, with each module corresponding to a feature of your app. Tied to these modules are your routes, which switches your components on your page depending on the URL the user is on. Moreover, components are made up of an HTML Template and TypeScript Code. The former can contain directives, Angular specific HTML attributes that provide additional functionality, and other components in your application. The latter, on the other hand, delegates any business logic to services and calls upon them when needed.&lt;/p&gt;&lt;h2 id=&quot;wrapping-things-up&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#wrapping-things-up&quot; aria-label=&quot;wrapping things up permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Wrapping things up&lt;/h2&gt;&lt;p&gt;Naturally, this article is only meant to give you a simple overview of Angular and only represents the tip of the iceberg. For more details on what I&amp;#x27;ve covered, you should definitely check out &lt;a href=&quot;https://angular.io/docs&quot; target=&quot;_blank&quot;&gt;the documentation&lt;/a&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Understanding The Nuxt.js Folder Structure - The Basics]]></title><description><![CDATA[Building a Vue app is fun, right? And it just got better with Nuxt.js, which is a another framework for Vue.js (frameworkception?! 🤭). Its…]]></description><link>https://dilshankelsen.com/understanding-the-nuxt-js-folder-structure-the-basics/</link><guid isPermaLink="false">https://dilshankelsen.com/understanding-the-nuxt-js-folder-structure-the-basics/</guid><pubDate>Tue, 09 Apr 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Building a Vue app is fun, right? And it just got better with Nuxt.js, which is a another framework for Vue.js (frameworkception?! 🤭). Its main purpose is to create a more streamlined development process. It&amp;#x27;s particularly powerful for creating a universal, static generated or single page application. In case you aren&amp;#x27;t familiar with these first two types of applications, I cover them in a &lt;a href=&quot;/server-side-rendering-and-pre-rendering-with-nuxt-js&quot;&gt;separate article&lt;/a&gt;, including on how to implement them using Nuxt.js..., but I digress. 😅&lt;/p&gt;&lt;p&gt;The team behind the framework created a fantastic scaffolding tool you can initiate with &lt;code&gt;$ npx create-nuxt-app &amp;lt;project-name&amp;gt;&lt;/code&gt;. And guess what! It automatically ships with NPM 5.2.0 and above. No installation needed!&lt;/p&gt;&lt;p&gt;After you generate your Nuxt project, you are greeted with the following folder structure below. I&amp;#x27;ll briefly cover all the main directories, with a focus on a select few. However, the &lt;code&gt;.nuxt&lt;/code&gt; directory contains the compiled output of your application and isn&amp;#x27;t of particular relevance to us.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:280px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:209.50000000000003%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAqCAYAAACz+XvQAAAACXBIWXMAABYlAAAWJQFJUiTwAAAFQ0lEQVRIx41Xa1MaZxj1d1QT5bbsLrDscr9fVBYQiRFFBEUxxqZJM51p2mk7mcn0S6aTf336nBeWoFzMh3dYXPbseS7nPI9bwWAIoZCmjibH1MMIBEJygvDuPf3cdLZ8vgAymQzCZhz/3JXxaVyCEU2gXqvBdV1EItEfApoDej8mq3gkDNvU4PMHFcNQKIS9Pd/s+seYbvn9AaRTaURjFgb7B+i6TbjNFq6urjEcDjEaXSEajU1TooWfB+TNcFhHUFgk7SycdAm2bUsaskgmU0gkEuBLd3Ze4OXLXcXYA10FrBgm5SHNsPHh3MQfwxDsVAmtVhPd7iv5bOPoqIOzs3O8fn2KSqWiXrCRYcSMwBcwcNbQcdH0w07kUCqVcHjYwMHBIcrlCtrtI9RqdeTzhXlOVwLyZtyKQ5NqniZtjHMJJDI5VCtVZLM5YVSF4ySwvb2jwvX5/JtDngJa8IVN/FpJ4fO+g0ylhuHlpWLY6RyjUCiq/LHFvAfXFUYBMmQtbKCcj6JesmBGLKTTaQm1rEJkcXK5vLrWdeP5tmElQ3oUvw3y+Pk0i+q+i7NeD3d3d7i+HmMwGMjnNS4uBqrRvaKsCnveNlooDD1uIJaOIuD/LjvmbfFBU6LZxHLa2BKeFjTx/t8u/v52j0Q8jYbbkHY5Uvnbl4ZntZvNppwWqtXq2jxuLd7QNR22KMYfCM7NgWy868WzFpAMbdtBOlPAt0+n+OttW6R3jIeHB3z8+FHydymSWw5tHehjc4hHEE86iDkO7Lit+o/3+NLd3b2lHnw2ZD4YG3URLWZwLMqYSJVvbm7ExprKKF69OpmDPmtfc5ONmKJrQ7VHTPLJdDCPZGsY5iNzWAn4NCf2sAurnEPbbaHfv8DJyQlOT3tS2Rp6vTPRePkRy6UcPkowRe9YiqEjzBoNV6mDKkkmkwqUzCnDtVX+Xi1pm7CGnBWGFTUVG8+6jo+7iiXZUd90nUW1LDGc9lkQ27sh0bQOQ4D5UL1eVwBsaJoEnYcFIvBaQIJZ4jbU8lUjhqu6DieVR1YGF+2LSqHc6NgsyFPXXuk2jlj+XtjC14GB/463UXMlRCnG7e0tzs/7wnRfRcGZ8nSurGxstkVIZJeImyhlYtCNCCxpGc4VArCpVzX0WumlUiloegx/3hTxvp/DgdvBRNhNJhNV2eeaeYmhF4YhW4MV0VXFN4X2rMF6m8PnN2X8flWA2z5RDG9vJygWS4ohX7A4nDYqhTf5Y1Ma2jCM+YOUWmBmZTze94059DaCFy9e4suXL2L9Z0oZb98+qEPrZ6VHo5H63u/3N49R74L2VSgWVcU5Erh+kBEP/8ZjybilFDfm0FvjfpJm/XpYxIdKHk6ugNHlEOPxjQymC7Ux9GRoEWjRE9cAzoaRMDw0DeREdsl0FnkJm1JjUciMo5RgVMlT2S2Cf7cvGeL6QQlaKomSyO2w0VA65hpCk2Aeqem2DK5EIjlXzhJg0ANkFWtFaPLjtLChOZAdC8SBT/kRmC9hfqltKujp7jhvGwIXZB5nudaJ5MriKByfdBaGzpB5TeWwMJ6N0TgWqz4FnIHmpbppOW6rhZ74X6fTUaFOJnfqYc5kzmoypZtzIyPbZUDP/iUnlnxym43L1EvJZhufTT/OF+aO3/l79i2tbGPI+8KuLK5zJIvmtazEnMlsnXfvflHL53g8VjOG4IsesFzl4PSPYV2upW046bgWc37wmvlj67DJmbNN/248YphzTDhRDT1Zf+/v7xUjWhglx5WYTc5Zs1F6U7DZ9jAzz0VDCCzsOYvX69TyP+YkGcSGohTaAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Nuxt.js Folder Structure&quot; title=&quot;Nuxt.js Folder Structure&quot; src=&quot;/static/8b58ff362bd263dfd5e649b2df940689/908b1/image-1.png&quot; srcSet=&quot;/static/8b58ff362bd263dfd5e649b2df940689/772e8/image-1.png 200w,/static/8b58ff362bd263dfd5e649b2df940689/908b1/image-1.png 280w&quot; sizes=&quot;(max-width: 280px) 100vw, 280px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Nuxt.js Folder Structure&lt;/figcaption&gt;&lt;p&gt;In case you are only interested in certain directories, I&amp;#x27;ve got you covered my friend. Here&amp;#x27;s a list of the sections:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;#assets&quot;&gt;Assets&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;#static&quot;&gt;Static&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;#pages&quot;&gt;Pages&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;#middleware&quot;&gt;Middleware&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;#components&quot;&gt;Components&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;#layouts&quot;&gt;Layouts&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;#plugins&quot;&gt;Plugins&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;#store&quot;&gt;Store&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;#nuxt-configuration-file&quot;&gt;Nuxt Configuration File&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h2 id=&quot;assets&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#assets&quot; aria-label=&quot;assets permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Assets&lt;/h2&gt;&lt;p&gt;The assets folder, as the name implies, holds your un-compiled assets, such as JavaScript, SCSS files and images. Nuxt.js uses webpack to process and serve these assets.&lt;/p&gt;&lt;h2 id=&quot;static&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#static&quot; aria-label=&quot;static permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Static&lt;/h2&gt;&lt;p&gt;On the other hand, if you don&amp;#x27;t want dirty webpack to process your precious assets, you can place them in the static folder. In your files, you can reference these assets with &lt;code&gt;/&lt;/code&gt; as they are served by Nuxt from your root folder. For instance, you can access an image with &lt;code&gt;/image.png&lt;/code&gt; instead of &lt;code&gt;/static/image.png&lt;/code&gt;.&lt;/p&gt;&lt;h2 id=&quot;pages&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#pages&quot; aria-label=&quot;pages permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Pages&lt;/h2&gt;&lt;p&gt;Arguably one of the most important directories in your project, as it automatically creates a route for any &lt;code&gt;.vue&lt;/code&gt; file placed in it. In the image above, both files &lt;code&gt;index.vue&lt;/code&gt; and &lt;code&gt;inspire.vue&lt;/code&gt; would have their own routes set up. Baam, that&amp;#x27;s it! Nuxt takes care of that pesky &lt;code&gt;vue-router&lt;/code&gt; configuration for you. 😏&lt;/p&gt;&lt;p&gt;To create a dynamic route, you simply need to create a &lt;code&gt;.vue&lt;/code&gt; file or a directory prefixed by an underscore. Take a look at this example below.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-text&quot;&gt;TEXT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-text&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;pages/&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  index.vue&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  _slug/&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    index.vue&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    comments.vue&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  users/&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    _id.vue&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This directory structure would essentially create four different paths: &lt;code&gt;/&lt;/code&gt;, &lt;code&gt;/:slug&lt;/code&gt;, &lt;code&gt;/:slug/comments&lt;/code&gt; and &lt;code&gt;/users/:id&lt;/code&gt;.&lt;/p&gt;&lt;h2 id=&quot;middleware&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#middleware&quot; aria-label=&quot;middleware permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Middleware&lt;/h2&gt;&lt;p&gt;While we&amp;#x27;re on the topic of routing, let&amp;#x27;s talk about the middleware folder. It basically contains custom JavaScript functions that run right before a page or group of pages, i.e. a layout, is rendered. For example, imagine you want to check whether a user has the right credentials to access a page. In this case, you might have a file named &lt;code&gt;middleware/auth.js&lt;/code&gt; containing the code below.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; store &lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// some code to check user authentication&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;However, simply placing a custom function in the middleware directory won&amp;#x27;t do any good. We also have to let Nuxt.js know where we want to apply it, that is, on all pages or a select few. Regarding the former, we would have to access our &lt;code&gt;nuxt.config.js&lt;/code&gt; file and add the following:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;router&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;middleware&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;auth&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;However, if you&amp;#x27;d like to do the latter instead, that is, apply the middleware to select few pages and layouts, you do it as such:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;middleware&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;auth&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;components&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#components&quot; aria-label=&quot;components permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Components&lt;/h2&gt;&lt;p&gt;As you might&amp;#x27;ve guessed from its name, the components directory simply contains your single file Vue components. Not really much else to say here... 😬&lt;/p&gt;&lt;h2 id=&quot;layouts&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#layouts&quot; aria-label=&quot;layouts permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Layouts&lt;/h2&gt;&lt;p&gt;Assume you&amp;#x27;ve got a navigation bar and a footer. How would you display them in all your web pages? Perhaps import them into each &lt;code&gt;.vue&lt;/code&gt; file in your pages directory? Well... as you can imagine, that would be quite cumbersome and inefficient. And that&amp;#x27;s exactly what the layouts directory is for. Layouts allow you to easily change the look and feel of your web page.&lt;/p&gt;&lt;p&gt;Each Nuxt generated project has a &lt;code&gt;default.vue&lt;/code&gt; file in the layouts folder, with the following &lt;em&gt;minimum&lt;/em&gt; template structure:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;nuxt&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;&amp;lt;/nuxt&amp;gt;&lt;/code&gt; component is very important as it displays page components, i.e. your &lt;code&gt;.vue&lt;/code&gt; files from the &lt;em&gt;pages&lt;/em&gt; folders.&lt;/p&gt;&lt;p&gt;Of course, you can also create your own custom layouts, including an &lt;a href=&quot;https://nuxtjs.org/guide/views#error-page&quot; target=&quot;_blank&quot;&gt;error page&lt;/a&gt;. Consequently, we tell certain pages (e.g. &lt;code&gt;pages/post.vue&lt;/code&gt;) to use a custom layout (e.g. &lt;code&gt;layouts/blog.vue&lt;/code&gt;) by including the following code:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;&amp;lt;!-- Your template --&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;layout&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript string&quot;&gt;&amp;#x27;blog&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript comment&quot;&gt;// page component definitions&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;plugins&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#plugins&quot; aria-label=&quot;plugins permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Plugins&lt;/h2&gt;&lt;p&gt;In any regular Vue project, you can globally register Vue libraries in the &lt;code&gt;main.js&lt;/code&gt; file. However, this file doesn&amp;#x27;t exist in your Nuxt.js application and as such this responsibility falls on the plugins directory. As an example, let&amp;#x27;s take a look at the Vue plugin &lt;a href=&quot;https://github.com/se-panfilov/vue-notifications&quot; target=&quot;_blank&quot;&gt;vue-notifications&lt;/a&gt;, which allows you to display notification in your application. After installing it via npm, you create the file &lt;code&gt;plugins/vue-notifications.js&lt;/code&gt; containing the code below.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;Vue&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;vue&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;VueNotifications&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;vue-notifications&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;Vue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;VueNotifications&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Nothing strange so far, right? 😋Afterwards, we have to inform Nuxt.js that we would like to use this plugin, by editing the &lt;code&gt;nuxt.config.js&lt;/code&gt; file. The &lt;code&gt;~&lt;/code&gt; has the same function as an &lt;code&gt;@&lt;/code&gt;, meaning it refers to the root folder.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;plugins&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;~/plugins/vue-notifications&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Et voilà! That&amp;#x27;s it! Usually, the documentation of any Vue plugin will layout these steps in detail, so you should be just fine. 😁&lt;/p&gt;&lt;h2 id=&quot;store&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#store&quot; aria-label=&quot;store permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Store&lt;/h2&gt;&lt;p&gt;One topic we haven&amp;#x27;t touched on yet is the application state. For this task, Nuxt.js implements Vuex in its core. At the time of this writing, Nuxt let&amp;#x27;s you chose between two store modes: classic and modules.&lt;/p&gt;&lt;p&gt;With the former, you simply create a &lt;code&gt;store/index.js&lt;/code&gt; file in which you specify all your actions, mutations, etc. However, it&amp;#x27;s been marked as deprecated and will most likely be removed in future releases. Therefore, it&amp;#x27;s recommended to use the latter, in which you have multiple JavaScript files corresponding to different, automatically, namespaced modules.&lt;/p&gt;&lt;p&gt;If you&amp;#x27;d like to understand in detail how to use the store directory, Todd Baur from ITNEXT has a great article on Medium explaining&lt;/p&gt;&lt;h2 id=&quot;nuxt-configuration-file&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#nuxt-configuration-file&quot; aria-label=&quot;nuxt configuration file permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Nuxt Configuration File&lt;/h2&gt;&lt;p&gt;While not a folder, the &lt;code&gt;nuxt.config.js&lt;/code&gt; file constitutes an important piece of your project. Without it, things just won&amp;#x27;t work the way you expect them to. However, diving into the configuration file goes beyond the scope of this article. It includes other configurations beyond the discussed directories, such as the mode and meta tags of your application.&lt;/p&gt;&lt;p&gt;However, this article is meant to give you an introduction to the folder structure of Nuxt.js and only covers the tip of the iceberg. I highly recommend that you check out &lt;a href=&quot;https://nuxtjs.org/guide&quot; target=&quot;_blank&quot;&gt;their great documentation&lt;/a&gt; for further details.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Server Side Rendering And Pre-Rendering With Nuxt.js]]></title><description><![CDATA[In my previous article titled  Single Page Applications vs. Multi Page Applications , I discussed the tradeoffs using each type of…]]></description><link>https://dilshankelsen.com/server-side-rendering-and-pre-rendering-with-nuxt-js/</link><guid isPermaLink="false">https://dilshankelsen.com/server-side-rendering-and-pre-rendering-with-nuxt-js/</guid><pubDate>Tue, 02 Apr 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In my previous article titled &lt;a href=&quot;/single-page-applications-vs-multi-page-applications/&quot;&gt;Single Page Applications vs. Multi Page Applications&lt;/a&gt;, I discussed the tradeoffs using each type of application. One major drawback to using SPAs is that they aren&amp;#x27;t particularly Search Engine Optimisation (SEO) friendly. This makes it hard for search engines such as Google to index them. Fortunately, two methods have been developed to address this issue: Server Side Rendering (SSR) and pre-rendering. In this tutorial, I&amp;#x27;ll give an overview of what each method has to offer and how you can implement them in your Vue.js app with Nuxt.js. 💁‍♂️&lt;/p&gt;&lt;h2 id=&quot;ssr-and-pre-rendering-explained&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#ssr-and-pre-rendering-explained&quot; aria-label=&quot;ssr and pre rendering explained permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;SSR and pre-rendering explained&lt;/h2&gt;&lt;p&gt;While both methods address the issue of SEO, they both differ in many ways beyond that.&lt;/p&gt;&lt;h3 id=&quot;pre-rendering&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#pre-rendering&quot; aria-label=&quot;pre rendering permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Pre-rendering&lt;/h3&gt;&lt;p&gt;If you are solely interested in improving SEO for your SPA, then pre-rendering is the way to go. Sometimes it&amp;#x27;s also referred to as static site generation, and you&amp;#x27;re about to see why. The most fundamental difference between pre-rendering and SSR is that the former does not require any server whatsoever. Instead of being rendered on a server and then served to a client, static HTML files are generated for SPA components/routes of your choice when you build your project.&lt;/p&gt;&lt;p&gt;For instance, imagine you want to pre-render your &lt;code&gt;home.vue&lt;/code&gt; and &lt;code&gt;about.vue&lt;/code&gt; components. As a result, you will receive two static HTML files full of content named &lt;code&gt;home.html&lt;/code&gt; and &lt;code&gt;about.html&lt;/code&gt;, which will enable a web crawler to index your site. Afterwards, you can host these static files on a free hosting website like Github or Netlify.&lt;/p&gt;&lt;p&gt;Besides making your website more SEO friendly, pre-rendering also speeds up your initial load time and gets rid of any accompanying UI flickering (unless you were already using Nuxt.js 😜). However, the more routes you&amp;#x27;d like to pre-render, the longer your project&amp;#x27;s build time is going to be.&lt;/p&gt;&lt;h3 id=&quot;server-side-rendering&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#server-side-rendering&quot; aria-label=&quot;server side rendering permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Server Side Rendering&lt;/h3&gt;&lt;p&gt;SSR is like pre-rendering, but more... and is quite often considered an overkill as it isn&amp;#x27;t easy to implement. So what advantages does this &lt;em&gt;more&lt;/em&gt; include? Well, the most distinguishing feature of SSR, the server, is also its greatest asset. Namely, the server can re-render the application anytime and on the fly.&lt;/p&gt;&lt;p&gt;But in which cases is this useful? 🤔 Well, if you expect the content of your application to undergo a lot of changes after deployment, e.g. due to user generated content such as blog posts, SSR is undoubtedly the way to go my friend! On the other hand, you need to go through the process of setting up a server. Furthermore, it needs to be powerful enough to handle the processing, especially under high traffic. Add to this considerably more development time than if you were to simply implement a pre-rendering solution.&lt;/p&gt;&lt;p&gt;Lastly, server side rendered SPAs can also be referred to as &amp;quot;isomorphic&amp;quot; or &amp;quot;universal&amp;quot; apps since the majority of the app&amp;#x27;s code runs on both the server and the client.&lt;/p&gt;&lt;h2 id=&quot;using-nuxtjs&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#using-nuxtjs&quot; aria-label=&quot;using nuxtjs permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Using Nuxt.js&lt;/h2&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:56.49999999999999%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAABAUA/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEAMQAAABeZcwraFj/8QAHBAAAgICAwAAAAAAAAAAAAAAAQIREwADBCEi/9oACAEBAAEFArpx+T3r8LAA2sb7Xn//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/AT//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/AT//xAAcEAACAgIDAAAAAAAAAAAAAAAAARExAiEiQZH/2gAIAQEABj8Cp+ihUQijBdaHyZ//xAAcEAACAgIDAAAAAAAAAAAAAAABEQAhMUFRYXH/2gAIAQEAAT8hR9UOGWwsmbuQ9lwmUFQB22C7nqXM/9oADAMBAAIAAwAAABDgz//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8QP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8QP//EABoQAQEAAwEBAAAAAAAAAAAAAAERACExQfD/2gAIAQEAAT8QspOPkyuYRQgrOmuYpSaS0XoL5h0QqE5hyEKhN7ZQJgwkauf/2Q==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Nuxt.js Meme&quot; title=&quot;Nuxt.js Meme&quot; src=&quot;/static/f7a09c230f8662babcdaa88dfe35cc2f/4b190/meme-nuxt.jpg&quot; srcSet=&quot;/static/f7a09c230f8662babcdaa88dfe35cc2f/e07e9/meme-nuxt.jpg 200w,/static/f7a09c230f8662babcdaa88dfe35cc2f/066f9/meme-nuxt.jpg 400w,/static/f7a09c230f8662babcdaa88dfe35cc2f/4b190/meme-nuxt.jpg 800w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;Whether you&amp;#x27;re new to or experienced with Vue SPA development, if you haven&amp;#x27;t tried out Nuxt.js yet, you should consider doing so. It&amp;#x27;s a Vue.js framework that abstracts away quite a bit of complexity, including our two favourite topics: SSR and pre-rendering 🤓. Naturally, this will make our development experience much more enjoyable.&lt;/p&gt;&lt;h3 id=&quot;pre-rendering-1&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#pre-rendering-1&quot; aria-label=&quot;pre rendering 1 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Pre-rendering&lt;/h3&gt;&lt;p&gt;So how do we pre-render a SPA with Nuxt.js? To start off, let&amp;#x27;s first take a look below at a folder structure from an example project. In this SPA we&amp;#x27;ve got two routes in the &lt;em&gt;pages&lt;/em&gt; folder: &lt;code&gt;index.vue&lt;/code&gt; and &lt;code&gt;inspire.vue&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:300px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:209%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAqCAYAAACz+XvQAAAACXBIWXMAABYlAAAWJQFJUiTwAAAFQ0lEQVRIx5VX2VIbVxTkN5LyAmiZVbNIQstIaEeABEhgVgnCYoNTyUMcp8rl+DHlfHfn9MEjZJCE83Brljvq22frc7SUTKaQThtIpdKwTQMZx0QikQLfx3u8cv9H1tLqagK5XA6Ol8UfowgfL6pIWx6azSba7Q58P5ADkj8OqDfCICHLdw2EjoFVYWgYprLjgTFgzHQRY2WYzeYQCJOjRgM7m5vodjdxfn6Bq6srjMdjeJ6PadcsBJx8mEwi8PLwcxW4rosgCBGGWbnPYGVlFS9evMSrV6/x+vXyBHQWsDIMwxCWm8Xtvo1P5yl4YQn9fg+bwnZnZxfb2z3s7x9gb2+ATmdDD1jI0DItJFI2dpsmDjeW4YcFtFotVKvrArqF9fWaAjWb9+9IYi4gHe5lPJiuh4N8gOsoj0JURb1eF6B1NBpNNf3ly1dYXl7RtdBkArqOi4Tp4LqSxadWFrlyFcPBAFtbWxqgfH5NfTdt6rzAKKBjOzAFsFZ20a75sB0PlUpFVlXNLRZLKJcjfTbFPYvSR4PiS1qYtoe7/aIEpoRSpY7joyOcnJxq2hwfn+D09BRv3hzCcZzvfPgYVINimiaMtAk37yAbBUiy9FL3yU0zp8vQVmusxQyZ2LaZweXHLfz19Qqhv4Z2py0+3FZzGV2WYbfb1SvLci7g9IbnCsPAk3tD2XFZlj03AHMZBkGAbL6Ifz8M8fldD52NHu7u7vD+/a84OxtNSm46XeYFZml6I+Pa8PNZOHIAU4lKwwPpR+ZfnDYLozy9wR97o11YpTx2e33c3LzFaDTSamGEh8PhJLEXlt7kBSNpSQQNQ0Uhl8sry4xUEgPnCOvnWC49gN1vhuMB7EoBPWE1EEbMxX5/R6LbxmAwfFLLMwEnp1FIgwxSkpeBH6JWq8uqSenllS3vbduWMlyZ78MH6mmYRhpFz4RnmypVe3t7Kl/9fh8HB28QRZGy5UH05ax+owxZz2T3YlnSQSrGSCXVtFarrYlM6dre3lbAngQriipPfPlUvqSWz7sZ3GyYyIgelktlFApF/THLjfJFEKr2PLApwAySto8vQwNfez+j0e1jIOZeXV2LqQdaevyOJj7uKzMT25JUSRsWSjlXJMyHZbsoCru1tYKW3ixGc/VQe4o0JMvx8ftJGb8dl1CptXF7+06Tmnr4v/ryQ3sUabIM6c3W/eQgIBqsGUye7cucHFyZHD6My/jzPEK9tYl3b9+qD0sSnLjcpg+Ya3K8SeCMqLHr3MsVy4wlx33KGK9896w4xCYzLT7//QWHh0datzc3Nzo5HB0dqzCcnZ3h8vJS2wEPWMhQzZEPKpK47BlpSW6yY/7xx5R8RpvPpVJp4UQmgMJQbn6S+vynHeFDowwnm8e1sKMwHB4eatkNh/sK9FwrnTBckcg2bAtFI4VAAFl6bJtcbPR0Qyy0j6exmYDJlQTMZoR0cQ0ViWxD58O2Rpn1TKaccSi2HKTmTWMPgHKquV6GITK1JosjCJs9a5nqwtGk1+vpJBHXNqeJx2zv5YsDpzxUxeyCfFAgw3pDVKajdVyXewoFpweyZvVQibjPVJqupCXqYBzlgkQ0J/Lf2eiqHtI8jnGj0XgiZZQxmk715j1bxVzAnPgk5HTApJYP6StOr7y6355jAJpMKXtq8jfkVXlRF4aRAO4Iu7GwYlJfXPwiQnGrjDnnMPEJOjco35WRKdd0WlOEUy3ZsZ9QxugrAsXl+Lj8vg/KN5NLoY1Q/gnsSyKPx+c6cZEhpwj2F4otffpD4hCDxkIRjxv0EZ/jcls0hnD9B2QvGFNUFgIJAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Nuxt.js Folder Structure Example&quot; title=&quot;Nuxt.js Folder Structure Example&quot; src=&quot;/static/05f8733e89962774958857eaf3eb800d/5a46d/image-1.png&quot; srcSet=&quot;/static/05f8733e89962774958857eaf3eb800d/772e8/image-1.png 200w,/static/05f8733e89962774958857eaf3eb800d/5a46d/image-1.png 300w&quot; sizes=&quot;(max-width: 300px) 100vw, 300px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Nuxt.js Folder Structure&lt;/figcaption&gt;&lt;p&gt;How would we go about pre-rendering these two routes? Amazingly, Nuxt.js automatically takes care of this task for static routes! By simply running the command &lt;code&gt;npm run generate&lt;/code&gt;, we output the rendered HTML files in a &lt;code&gt;dist&lt;/code&gt; folder as seen below. Nuxt also takes care of any asynchronous data for us during the static page generation. 😍&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-text&quot;&gt;TEXT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-text&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;// dist folder&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;index.html&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;inspire/&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  index.html&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;However, for dynamic routes such as &lt;code&gt;pages/users/_id/index.vue&lt;/code&gt;, we will have to inform Nuxt.js how to render these routes since it won&amp;#x27;t know what the value of &lt;code&gt;_id&lt;/code&gt; is. To address this issue, we have to turn towards the &lt;code&gt;nuxt.config.js&lt;/code&gt; file and add a &lt;code&gt;generate&lt;/code&gt; property.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token literal-property property&quot;&gt;generate&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;routes&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;/users/1&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;/users/2&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The code above tells Nuxt that it should generate two HTML pages with the &lt;code&gt;_id&lt;/code&gt; 1 and 2. Pretty easy, right? But we&amp;#x27;re not done yet...&lt;/p&gt;&lt;p&gt;The issue with this solution is that you need to know all of your dynamic routes upfront. This can be difficult and quite a tedious task. Fortunately, we can fetch data, like user IDs, from a database and indicate our desired routes dynamically. In order to do this, we would have to change our previous &lt;em&gt;chunk&lt;/em&gt; of code to something like this:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token literal-property property&quot;&gt;generate&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;routes&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;http://chunkbytes.com/userlist&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; res&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;route&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;/users/&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; user&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;For more information on what the &lt;code&gt;generate&lt;/code&gt; property has to offer, why not check out Nuxt&amp;#x27;s &lt;a href=&quot;https://nuxtjs.org/api/configuration-generate&quot; target=&quot;_blank&quot;&gt;documentation&lt;/a&gt;?&lt;/p&gt;&lt;h3 id=&quot;server-side-rendering-1&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#server-side-rendering-1&quot; aria-label=&quot;server side rendering 1 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Server Side Rendering&lt;/h3&gt;&lt;p&gt;In order to enable SSR in your application, we need to head to our beloved &lt;code&gt;nuxt.config.js&lt;/code&gt; file again and make sure that the &lt;code&gt;mode&lt;/code&gt; property is set to &lt;code&gt;universal&lt;/code&gt;. This tells Nuxt it should create an isomorphic application, i.e. execute SSR and Client Side Rendering. If the &lt;code&gt;mode&lt;/code&gt; property isn&amp;#x27;t declared, it automatically defaults to &lt;code&gt;universal&lt;/code&gt;. And that&amp;#x27;s all there is to it! Yes, I&amp;#x27;m serious... 😅&lt;/p&gt;&lt;span class=&quot;text-center&quot;&gt;&lt;p&gt;&lt;img src=&quot;/static/surprised-509ef2ae3bcfefa1c7143b72d0ec0ad5.gif&quot; alt=&quot;Wtf Reaction GIF to Server Side Rendering and pre-rendering being so easy with Nuxt.js&quot;/&gt;&lt;/p&gt;&lt;/span&gt;&lt;p&gt;To see your application in action, all that&amp;#x27;s left to do is build it with &lt;code&gt;npm run build&lt;/code&gt; and launch it with &lt;code&gt;npm run start&lt;/code&gt;.&lt;/p&gt;&lt;h2 id=&quot;final-thoughts&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#final-thoughts&quot; aria-label=&quot;final thoughts permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Final thoughts&lt;/h2&gt;&lt;p&gt;Nuxt.js is a powerful tool to easily enable SSR and pre-rendering. Yet, the ease of setting these features up should not stop you from thinking twice about whether you truly need them, in particular SSR. However, Nuxt provides a number of other powerful features that should definitely &lt;a href=&quot;https://nuxtjs.org/&quot; target=&quot;_blank&quot;&gt;not be overlooked&lt;/a&gt; and should be worth your time and consideration.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Single Page Applications vs. Multi Page Applications]]></title><description><![CDATA[Single Page Applications (SPAs) are the hottest new craze in web development and they are increasingly replacing the traditional Multi Page…]]></description><link>https://dilshankelsen.com/single-page-applications-vs-multi-page-applications/</link><guid isPermaLink="false">https://dilshankelsen.com/single-page-applications-vs-multi-page-applications/</guid><pubDate>Tue, 26 Mar 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Single Page Applications (SPAs) are the hottest new craze in web development and they are increasingly replacing the traditional Multi Page Application (MPA). As a developer, you might be compelled to learn the latest and greatest JavaScript frameworks to stay on top of these new developments. But before you blow your brains out from information overload 🤪, how about we take a step back and see what it really has to offer…&lt;/p&gt;&lt;h2 id=&quot;multi-page-applications&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#multi-page-applications&quot; aria-label=&quot;multi page applications permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Multi Page Applications&lt;/h2&gt;&lt;p&gt;Since the internet&amp;#x27;s infancy, websites have existed as MPAs. But what exactly do we mean by that? Let&amp;#x27;s take a look at the image below.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:644px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:43.99999999999999%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAJABQDASIAAhEBAxEB/8QAGQAAAgMBAAAAAAAAAAAAAAAAAAECAwQF/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEAMQAAAB7itRkJh//8QAGRAAAgMBAAAAAAAAAAAAAAAAAAIBECER/9oACAEBAAEFAuyaK2Up/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAGBAAAgMAAAAAAAAAAAAAAAAAADEgQZH/2gAIAQEABj8CReDh/8QAGxAAAgIDAQAAAAAAAAAAAAAAAAERMRAhcYH/2gAIAQEAAT8hW2YCcr9Bz6Zof//aAAwDAQACAAMAAAAQUw//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/ED//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/ED//xAAcEAEAAgIDAQAAAAAAAAAAAAABABEhMRBBcVH/2gAIAQEAAT8Qc3gGEMxxujso6+x2tvXDqbfZ/9k=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Request-Response Cycle Figure&quot; title=&quot;Request-Response Cycle Figure&quot; src=&quot;/static/6f683dbfc9cb0c6102b802263e16b0e6/51442/image-1.jpg&quot; srcSet=&quot;/static/6f683dbfc9cb0c6102b802263e16b0e6/e07e9/image-1.jpg 200w,/static/6f683dbfc9cb0c6102b802263e16b0e6/066f9/image-1.jpg 400w,/static/6f683dbfc9cb0c6102b802263e16b0e6/51442/image-1.jpg 644w&quot; sizes=&quot;(max-width: 644px) 100vw, 644px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;&lt;p&gt;  Request-Response Cycle. &lt;em&gt;Source:&lt;/em&gt; &lt;a href=&quot;https://www.ryanckulp.com/software-programming-terminology/request-response-cycle/&quot; target=&quot;_blank&quot;&gt;&lt;em&gt;Ryan Kulp&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;&lt;/figcaption&gt;&lt;p&gt;Imagine we have a client, on the left hand side, making a request to a server at a certain domain (e.g. dilshankelsen.com). As a response, the server sends back a rendered HTML file along with other resources such as images and a CSS file. Each time you want to access a separate page (i.e. subdomain) on that website, you need to fetch a new HTML file with resources from the corresponding server. This is the defining feature that gives it the name &amp;quot;Multi Page&amp;quot;.&lt;/p&gt;&lt;p&gt;However, there&amp;#x27;s one other important aspect of MPAs: server side rendering. In fact, you may have heard about the debate between Server Side Rendering (SSR) vs Client Side Rendering (CSR), which is essentially the one we&amp;#x27;re talking about right now, with CSR being synonymous for SPAs. But I digress... 😄 As I was saying, ahem, SSR is an important element of MPAs as the HTML is rendered (meaning it isn&amp;#x27;t bare bone and has content) by the server before being sent out to the client and consequently read by its browser.&lt;/p&gt;&lt;h2 id=&quot;single-page-applications&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#single-page-applications&quot; aria-label=&quot;single page applications permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Single Page Applications&lt;/h2&gt;&lt;p&gt;On the other hand, SPAs are pretty much the exact opposite of what an MPA embodies. Rather than navigating through a site with multiple request messages, the client initially downloads the entire website at once from a single un-rendered HTML file. Naturally, this method has its drawbacks as we will discuss later.&lt;/p&gt;&lt;p&gt;I would also like to &lt;em&gt;emphasise&lt;/em&gt; here that the website is not rendered on the server, but on the client&amp;#x27;s browser through JavaScript! ☝️ Hence, the name CSR. Generally, you&amp;#x27;ll use a JS framework or library such as Vue.js or React to do this work for you. However, keep in mind that the website cannot be rendered until ALL the JavaScript has been downloaded from the server.&lt;/p&gt;&lt;h2 id=&quot;noticing-the-differences&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#noticing-the-differences&quot; aria-label=&quot;noticing the differences permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Noticing the differences&lt;/h2&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:556px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:89.99999999999999%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAASABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAMEAv/EABYBAQEBAAAAAAAAAAAAAAAAAAECAP/aAAwDAQACEAMQAAAB6lo5lmM6YiYQz//EABwQAAICAgMAAAAAAAAAAAAAAAECABIDQRAhIv/aAAgBAQABBQIv2nrIwmwlTdZpyeP/xAAVEQEBAAAAAAAAAAAAAAAAAAARIP/aAAgBAwEBPwEj/8QAFhEBAQEAAAAAAAAAAAAAAAAAEAER/9oACAECAQE/AdKf/8QAHBAAAQMFAAAAAAAAAAAAAAAAEQAQMQECEkHB/9oACAEBAAY/AsaWkLjASjt5b//EABwQAQACAwEBAQAAAAAAAAAAAAEAESFBUTFxkf/aAAgBAQABPyE79YrGp7SbXBGi5vZljEwLqNm35GOp+x9Z/9oADAMBAAIAAwAAABBvML3/xAAZEQACAwEAAAAAAAAAAAAAAAAAARExQVH/2gAIAQMBAT8QeiV0VDP/xAAZEQACAwEAAAAAAAAAAAAAAAAAARARMUH/2gAIAQIBAT8QXDFZqP/EAB4QAAIDAAIDAQAAAAAAAAAAAAERACExQWFRkcHw/9oACAEBAAE/EEgNKUBrMiQDB3geVVQBSQRYtwoLFhWHf7uBORlaq98CPQCnSDEMg7O/ZhpOhrqUV5M//9k=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Single Page Application vs Multi Page Application Meme&quot; title=&quot;Single Page Application vs Multi Page Application Meme&quot; src=&quot;/static/f66c8f07ea056047e1e6543cb2a3a9a8/7892a/meme-spa.jpg&quot; srcSet=&quot;/static/f66c8f07ea056047e1e6543cb2a3a9a8/e07e9/meme-spa.jpg 200w,/static/f66c8f07ea056047e1e6543cb2a3a9a8/066f9/meme-spa.jpg 400w,/static/f66c8f07ea056047e1e6543cb2a3a9a8/7892a/meme-spa.jpg 556w&quot; sizes=&quot;(max-width: 556px) 100vw, 556px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;p&gt;So how do you spot the difference between these two types of applications? There are a few ways of noticing whether you&amp;#x27;re using a SPA or a MPA, but the easiest and most obvious one are page loads. Let&amp;#x27;s give it a try, shall we? 😉&lt;/p&gt;&lt;br/&gt;&lt;a href=&quot;https://angular.io/&quot; target=&quot;_blank&quot;&gt;Angular.io&lt;/a&gt; is a perfect example of a SPA and &lt;a href=&quot;https://www.bbc.com/&quot; target=&quot;_blank&quot;&gt;bbc.com&lt;/a&gt; of a MPA. If you try browsing a bit on each website, you&amp;#x27;ll notice the former is much faster and smoother without any page loads and feels very much like a desktop application. On the other hand, whenever you click on any internal link on BBC&amp;#x27;s website, your browser has to download an entirely new page from its web server.&lt;p&gt;Another simple, yet subtle way to discover the type of a web application is to inspect its source code. To access it on a Google Chrome browser, right click the website of your choice and select &amp;quot;View Page Source&amp;quot;. The process should be quite similar in other browsers. Let&amp;#x27;s give it a try with our two previous website examples, shall we? 🤓&lt;/p&gt;&lt;h3 id=&quot;bbccom&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#bbccom&quot; aria-label=&quot;bbccom permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;BBC.com&lt;/h3&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:28.500000000000004%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABHElEQVQY0z2Q246DMAxE+f+fa4EEEqBcUkrC9QkqkCrN2u7uPoxiR5njiaO6rtE0LapHjaKsoLMcbefgw4j+NYheg8ez77+19+jcEzV5+v6FaZ4xzwvmZcW+H4ju9ztygmilYYz5V6YzUZ7nUKmCUgqFLaRmT5IksNaCA/U0LPiA67wQJXEiRmMsqqoSEwMtnWVZirhnY9u21Fd0dgghwDmHcRzxfr9F10VAnmgJxqY0TaWWZBklJGmtURSFmDmJo3WwOoI2TSNAhntKeJ4nIoZ8YQq32w0mNwLhe/n2L5j7OI7ljn/FK0iT7xv+Piff9x2RdwPGYcQUJkzThHVdSRtNnmjZM7ZtEy3LgoWXT3ecit/99Vwfx4HP54MfLsGyH24ng8AAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;BBC Page Source Screenshot&quot; title=&quot;BBC Page Source Screenshot&quot; src=&quot;/static/bc115813564b3a0d656cc6cda1ba50c1/5a190/image-2.png&quot; srcSet=&quot;/static/bc115813564b3a0d656cc6cda1ba50c1/772e8/image-2.png 200w,/static/bc115813564b3a0d656cc6cda1ba50c1/e17e5/image-2.png 400w,/static/bc115813564b3a0d656cc6cda1ba50c1/5a190/image-2.png 800w,/static/bc115813564b3a0d656cc6cda1ba50c1/2bef9/image-2.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Bbc.com Page Source&lt;/figcaption&gt;&lt;p&gt;My lord! 🤭 BBC has over 2000 lines of code on their homepage alone! Basically what you see here is a server side rendered HTML file that contains plenty of text and JavaScript. Although, on my screenshot you can only see JS...&lt;/p&gt;&lt;h3 id=&quot;angulario&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#angulario&quot; aria-label=&quot;angulario permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Angular.io&lt;/h3&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:53%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABdElEQVQoz5VT23KrMBDj/z8xmUlpB0IgBBPfbTCq1u3D6cuZxDOLjbG1WmlpnPfo+h7t5xf66xXTfcaiFIx1CDHC+QA540Os7+F3/tkLf/ZkbsDx8dHidDrhcrng2hN0mnAbbpjnGataeTCglIJXRpNzxhfZ3clsXVdoreGZPUpWspPvMo7jeCkaz0uKJUZS9t7COUNG7k/WV8EqoFsMorPwNhJ4J0uJAqsz92Mt9S3AqD2cCfWyfhY8HqB2BU+ug80o2/4eoBoVngu1W5+11BA8WW3YNoId5f2SxU1x+Hw+45PmyPuyLAy2jjHVINFZnJaQS/9L0ozTiK7raqsMw0CgBzVcfkNVwzSBU0qIKVbmsj4q++OfkCcBr22Hob/hPivM04xxXAi8UkdPholmicYO3jlYrmsYy67YCXxw/omcduzUu7GT4gFPZoE6aoy3xNIz2jYROMGwC8ysaZpnX/IPcQkpiIGZFWT27c4khXsb9rzhGwapVuZYA+AQAAAAAElFTkSuQmCC&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Angular.io Page Source Screenshot&quot; title=&quot;Angular.io Page Source Screenshot&quot; src=&quot;/static/58d156d4de78a5bbbb8726e6ff724bfb/5a190/image-3.png&quot; srcSet=&quot;/static/58d156d4de78a5bbbb8726e6ff724bfb/772e8/image-3.png 200w,/static/58d156d4de78a5bbbb8726e6ff724bfb/e17e5/image-3.png 400w,/static/58d156d4de78a5bbbb8726e6ff724bfb/5a190/image-3.png 800w,/static/58d156d4de78a5bbbb8726e6ff724bfb/2bef9/image-3.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;Angular.io Page Source&lt;/figcaption&gt;&lt;p&gt;Wait a minute, only 110 lines of code?! That pales in comparison to our previous example. Furthermore, if you ignore the content outside of the body tags, which are primarily Google Analytics code and asset links, you&amp;#x27;ll notice that there&amp;#x27;s almost little to no relevant code. How come? Because what we&amp;#x27;re seeing here is an un-rendered HTML file. If you want to view the client side rendered version, simply &amp;quot;Inspect&amp;quot; the page in your browser.&lt;/p&gt;&lt;h2 id=&quot;advantages-and-disadvantages&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#advantages-and-disadvantages&quot; aria-label=&quot;advantages and disadvantages permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Advantages and Disadvantages&lt;/h2&gt;&lt;p&gt;Probably the section you&amp;#x27;ve been waiting for! I hope you didn&amp;#x27;t skip the rest. 😤 It should be fairly obvious that none of these applications dominates over the other. Rather, they each have their own strengths and weaknesses. So in which situations should you use them? Let&amp;#x27;s begin with MPAs.&lt;/p&gt;&lt;h3 id=&quot;multi-page-applications-1&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#multi-page-applications-1&quot; aria-label=&quot;multi page applications 1 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Multi Page Applications&lt;/h3&gt;&lt;h4 id=&quot;strengths&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#strengths&quot; aria-label=&quot;strengths permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Strengths&lt;/h4&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Search Engine Optimisation:&lt;/strong&gt; Arguably the greatest benefit that MPAs provide is compatibility with SEO. Search engines, such as Google and Bing, can easily request, crawl and index a rendered HTML file that is already filled with content. This dominant strength makes MPAs a perfect candidate for static and text heavy websites like news sites and blogs.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Maturity:&lt;/strong&gt; As MPAs have existed since the dawn of civil-, I mean the internet, developers have access to more mature and established frameworks, solutions and best practices.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Faster Initial Page Load:&lt;/strong&gt; As previously mentioned, SPAs require you to download the entire website at once and render it on your browser. However, with MPAs you only load what you need, which has already been rendered by the server.&lt;/li&gt;&lt;/ul&gt;&lt;h4 id=&quot;weaknesses&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#weaknesses&quot; aria-label=&quot;weaknesses permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Weaknesses&lt;/h4&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Slower and Frequent Page Loads:&lt;/strong&gt; A downside of MPAs is the need to frequently load other pages as you browse through a website. Moreover, compared to a SPA, you&amp;#x27;ll definitely notice a difference in speed.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Server Side Intensive:&lt;/strong&gt; Since all your content is rendered on and served to the client by the server, you need to make sure that it can handle any load, especially during high traffic. Simultaneously, SSR leads to a tightly coupled backend and frontend.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Less Interactivity:&lt;/strong&gt; While JavaScript can be used to add some interactivity to MPAs, your ability to do so fairly limited compared to SPAs.&lt;/li&gt;&lt;/ul&gt;&lt;h3 id=&quot;single-page-applications-1&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#single-page-applications-1&quot; aria-label=&quot;single page applications 1 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Single Page Applications&lt;/h3&gt;&lt;h4 id=&quot;strengths-1&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#strengths-1&quot; aria-label=&quot;strengths 1 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Strengths&lt;/h4&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Highly Reactive:&lt;/strong&gt; Unlike MPAs, SPAs are very fast and highly reactive to user input, only needing to re-render certain parts of a website. Consequently, it feels very much like a desktop application.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Decoupled Front End:&lt;/strong&gt; SPAs are backend agnostic, meaning they do not depend in any shape or form on the server. They can be easily attached to any backend server with the appropriate API. In turn, this makes it easier to develop SPAs as well, particularly if there is a lack of server side code.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Easier Debugging:&lt;/strong&gt; In relation to the previous point, a decoupled front end makes it easier to debug your web application with frameworks such as React and Vue.js as they have their own debugging tools.&lt;/li&gt;&lt;/ul&gt;&lt;h4 id=&quot;weaknesses-1&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#weaknesses-1&quot; aria-label=&quot;weaknesses 1 permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Weaknesses&lt;/h4&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Search Engine Optimisation:&lt;/strong&gt; Unfortunately, as HTML files for SPAs are not server side rendered, search engines have a hard time indexing these sites; think back to the angular.io website example. Fortunately, Google has been getting better at scraping through JavaScript in SPAs. Likewise, SPAs have come up with a few minor solutions of their own to mitigate this issue. Yet, for the foreseeable future, MPAs are king in SEO.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;JavaScript Dependent:&lt;/strong&gt; Guess what? If a client&amp;#x27;s browser doesn&amp;#x27;t have JavaScript enabled, you&amp;#x27;re sorta screwed...&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Client Side Intensive:&lt;/strong&gt; With the website rendered on the client side, it should go without saying that SPAs can be resource intensive on browsers. Naturally, this restricts their usage to relatively newer versions.&lt;/li&gt;&lt;/ul&gt;&lt;h2 id=&quot;the-choice-is-yours&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#the-choice-is-yours&quot; aria-label=&quot;the choice is yours permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;The choice is yours&lt;/h2&gt;&lt;p&gt;So what shall it be? A MPA or a SPA? Whatever you choose, it&amp;#x27;s important to keep the purpose of your website in mind. Are you trying to build a desktop like application similar to Twitter, Slack or Gmail that doesn&amp;#x27;t require much SEO? Perhaps a SPA is what you&amp;#x27;re looking for. However, if you&amp;#x27;re simply looking to build a static and/or text heavy website such as a blog or simple restaurant page, then a MPA might be the way to go.&lt;/p&gt;&lt;p&gt;Actually, why not combine the two if necessary? In fact, while &lt;a href=&quot;https://angular.io/&quot; target=&quot;_blank&quot;&gt;angular.io&lt;/a&gt; itself is a SPA, it has a blog section that is a MPA. The best (and worst) of both worlds? 😜&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Vue.js Form Validation With Vuelidate - An Introduction]]></title><description><![CDATA[Form Validation is one of those icky subjects that doesn't tickle my fancy and I'm quite certain many others feel the same way. While Vue.js…]]></description><link>https://dilshankelsen.com/introduction-vuejs-form-validation-vuelidate/</link><guid isPermaLink="false">https://dilshankelsen.com/introduction-vuejs-form-validation-vuelidate/</guid><pubDate>Tue, 19 Mar 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Form Validation is one of those icky subjects that doesn&amp;#x27;t tickle my fancy and I&amp;#x27;m quite certain many others feel the same way. While Vue.js enables you to implement form validation on your own, it can quickly become a hot mess the more fields and the more complex your validations become. But don&amp;#x27;t worry! &lt;a href=&quot;https://vuelidate.netlify.com/&quot; target=&quot;_blank&quot;&gt;Vuelidate&lt;/a&gt; is a great lightweight library that&amp;#x27;s going to make our task so much easier and more enjoyable. In this tutorial, I&amp;#x27;ll be giving you an introduction to Vue.js form validation with Vuelidate. So let&amp;#x27;s get started, shall we? 🤞&lt;/p&gt;&lt;h2 id=&quot;installation&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#installation&quot; aria-label=&quot;installation permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Installation&lt;/h2&gt;&lt;p&gt;You can install Vuelidate in your project by running one of the following commands:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;yarn&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; vuelidate&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; vuelidate --save&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;After installing it via your package manager, you&amp;#x27;ll need to register the plugin in your &lt;code&gt;main.js&lt;/code&gt; file.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;Vue&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;vue&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;Vuelidate&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;vuelidate&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports maybe-class-name&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;App.vue&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;Vue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;Vuelidate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Vue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;el&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;#app&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token maybe-class-name&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;adding-validation&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#adding-validation&quot; aria-label=&quot;adding validation permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Adding Validation&lt;/h2&gt;&lt;p&gt;So how does Vuelidate work? Let&amp;#x27;s start with a simple example below.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;@submit.prevent&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;onSubmit&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Email &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;v-model&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;emailField&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;submit&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Submit&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript string&quot;&gt;&amp;quot;HelloWorld&amp;quot;&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript function&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;emailField&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript string&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Above we&amp;#x27;ve got a simple form with a single field in which you can enter your email. Note the two-way binding with &lt;code&gt;emailField&lt;/code&gt; . Now, let&amp;#x27;s imagine we want the text input to be a valid email address as well as a requirement. How would we do this with Vuelidate?&lt;/p&gt;&lt;span class=&quot;text-center&quot;&gt;&lt;p&gt;&lt;img src=&quot;/static/thinking-71d0f336024f8880c6c792b38da6c561.gif&quot; alt=&quot;GIF of Batman thinking how to do Vue.js form validation with Vuelidate&quot;/&gt;&lt;/p&gt;&lt;/span&gt;&lt;h3 id=&quot;built-in-validators&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#built-in-validators&quot; aria-label=&quot;built in validators permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Built-in Validators&lt;/h3&gt;&lt;p&gt;Vuelidate has a number of &lt;a href=&quot;https://vuelidate.netlify.com/#sub-builtin-validators&quot; target=&quot;_blank&quot;&gt;built-in validators&lt;/a&gt; which we can easily implement into our form. However, to do so we will need to import these validators into our component. In particular, to complete our task we will want to import the validators &lt;code&gt;required&lt;/code&gt; and &lt;code&gt;email&lt;/code&gt;.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript comment&quot;&gt;// New code&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript imports punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript imports&quot;&gt; required&lt;/span&gt;&lt;span class=&quot;token script language-javascript imports punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token script language-javascript imports&quot;&gt; email &lt;/span&gt;&lt;span class=&quot;token script language-javascript imports punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript string&quot;&gt;&amp;#x27;vuelidate/lib/validators&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript string&quot;&gt;&amp;quot;HelloWorld&amp;quot;&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript function&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;emailField&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript string&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript comment&quot;&gt;// New code&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;validations&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript literal-property property&quot;&gt;emailField&lt;/span&gt;&lt;span class=&quot;token script language-javascript operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      required&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;      email&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token script language-javascript punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;script&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Besides the import statement at the top of our script section, we&amp;#x27;ve also added a &lt;code&gt;validations&lt;/code&gt; property. In this property, we pass in an object containing the data that we would like to validate. In our case, we want to validate &lt;code&gt;emailField&lt;/code&gt;. Next, we also pass in an object specifying the types of validators we would like, i.e. &lt;code&gt;required&lt;/code&gt; and &lt;code&gt;email&lt;/code&gt;.&lt;/p&gt;&lt;h3 id=&quot;exposing-v&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#exposing-v&quot; aria-label=&quot;exposing v permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Exposing $v&lt;/h3&gt;&lt;p&gt;So far so good! However, this alone won&amp;#x27;t do anything. Rather, we need to make use of information on the validation state that is exposed to us through the Vuelidate property &lt;code&gt;$v&lt;/code&gt;. In fact, by inserting &lt;code&gt;{{ $v }}&lt;/code&gt; into your HTML you can see the entire object it returns, including its properties. Why not give it a try and see for yourself? Although, for the lazy ones of you... below you can see what it would look like anyways.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:10.999999999999998%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAACCAYAAABYBvyLAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAW0lEQVQI102OUQ7AIAhDvf9Nlzmnm4DoRwfGLfsgvIa2Iah2EDFYBMwCkfZxU0UuF+5KINNzm9eZzVdXzrVzHwOBfwWTV6mHUy6zcNsj4pFwmvbxG6+SN+vszz0K15q6iAUazwAAAABJRU5ErkJggg==&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Output of $v&quot; title=&quot;Output of $v&quot; src=&quot;/static/7fc224e83434c7d19e456bb0793b1ce2/5a190/image-1.png&quot; srcSet=&quot;/static/7fc224e83434c7d19e456bb0793b1ce2/772e8/image-1.png 200w,/static/7fc224e83434c7d19e456bb0793b1ce2/e17e5/image-1.png 400w,/static/7fc224e83434c7d19e456bb0793b1ce2/5a190/image-1.png 800w,/static/7fc224e83434c7d19e456bb0793b1ce2/2bef9/image-1.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;$v exposed 😳&lt;/figcaption&gt;&lt;p&gt;As you can see, the entire object contains information about the state of &lt;code&gt;emailField&lt;/code&gt;. &lt;a href=&quot;https://vuelidate.netlify.com/#sub-v-values&quot; target=&quot;_blank&quot;&gt;Vuelidate&lt;/a&gt; has documented what all the &lt;code&gt;$v&lt;/code&gt; values mean. Likewise, &lt;a href=&quot;https://vuelidate.netlify.com/#sub-v-methods&quot; target=&quot;_blank&quot;&gt;Vuelidate&lt;/a&gt; also features &lt;code&gt;$v&lt;/code&gt; methods that you can make use of on events. These methods are built in and as such there&amp;#x27;s no need to import them.&lt;/p&gt;&lt;p&gt;Alright, let&amp;#x27;s move on!&lt;/p&gt;&lt;p&gt;First, let&amp;#x27;s add the &lt;code&gt;$touch()&lt;/code&gt; method to our input field as follows.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;v-model&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;emailField&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;@input&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;$v.emailField.$touch()&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;With this piece of code, we basically want to change the property &lt;code&gt;$dirty&lt;/code&gt; of our &lt;code&gt;emailField&lt;/code&gt; to true on any input. &lt;code&gt;$dirty&lt;/code&gt; basically indicates whether the field has been interacted with by the user at least once. You may be wondering why that&amp;#x27;s necessary, and you&amp;#x27;ll find out about it in the next section! 😜&lt;/p&gt;&lt;h2 id=&quot;displaying-errors&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#displaying-errors&quot; aria-label=&quot;displaying errors permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Displaying errors&lt;/h2&gt;&lt;p&gt;Let&amp;#x27;s insert two lines of text below our input field, each describing their respective error as seen below. Don&amp;#x27;t forget to colour them red! That makes them look more official. 🧐&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;Email &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;v-model&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;emailField&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;@input&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;$v.emailField.$touch()&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;// New code&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;An email is required!&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;The email must be valid!&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Alright. How about controlling when they appear? For that we can use &lt;code&gt;v-if&lt;/code&gt; and our beloved &lt;code&gt;$v&lt;/code&gt; values. In particular, we want to access the &lt;code&gt;required&lt;/code&gt; and &lt;code&gt;email&lt;/code&gt; properties, which indicate true or false on the state of our validation.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;v-if&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;!$v.emailField.required&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;v-if&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;!$v.emailField.email&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now, you may be tempted to call it a day. However, if you have been following this tutorial along on your own, you may have noticed that our &lt;em&gt;required&lt;/em&gt; error displays even when our user hasn&amp;#x27;t entered anything. Well, this is where our handy dandy &lt;code&gt;$dirty&lt;/code&gt; value comes in. By simply adding it to the &lt;code&gt;v-if&lt;/code&gt; statement, we can get rid of this minor issue.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;v-if&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;!$v.emailField.required &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;&amp;amp;&amp;amp; $v.emailField.$dirty&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;custom-validator&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#custom-validator&quot; aria-label=&quot;custom validator permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Custom Validator&lt;/h2&gt;&lt;p&gt;Sooner or later, you might need a validator that Vuelidate doesn&amp;#x27;t have. In such a case, you can resort to creating your own custom validator. Let&amp;#x27;s continue with our previous email example and create a &lt;em&gt;uniqueness&lt;/em&gt; validator for it. For instance, you may assume we are validating the input against an existing list of emails in our database. 📑&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Not in methods property!&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;unique&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; value &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!==&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;test@gmail.com&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;validations&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;emailField&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    required&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    email&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    unique&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In our script section, we first create a &lt;code&gt;unique&lt;/code&gt; function that compares our input to the string &lt;code&gt;&amp;#x27;test@gmail.com&amp;#x27;&lt;/code&gt; and returns true or false accordingly. Fortunately, custom validators automatically receive our input as a parameter, which I named here as &lt;code&gt;value&lt;/code&gt;. Afterwards, the only thing left is to add the function to the validations property under the &lt;code&gt;emailField&lt;/code&gt; property.&lt;/p&gt;&lt;p&gt;If you&amp;#x27;ve still got &lt;code&gt;$v&lt;/code&gt; exposed in you HTML, you will see the addition of our &lt;code&gt;unique&lt;/code&gt; validator. All that&amp;#x27;s left now is to add an error message for the user. 🚨&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;v-if&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;!$v.emailField.unique&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;This email is already taken!&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[Drag And Drop In Vue.js With Vue.Draggable]]></title><description><![CDATA[Adding some drag and drop functionality to an application can greatly enhance the user experience. However, creating such a feature from…]]></description><link>https://dilshankelsen.com/drag-and-drop-in-vuejs-with-vuedraggable/</link><guid isPermaLink="false">https://dilshankelsen.com/drag-and-drop-in-vuejs-with-vuedraggable/</guid><pubDate>Tue, 12 Mar 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Adding some drag and drop functionality to an application can greatly enhance the user experience. However, creating such a feature from scratch on your own may pose a significant challenge. Fortunately, there are a few libraries that make this task as easy as pie. Thank God for those hard working bees 🐝.&lt;/p&gt;&lt;p&gt;In this article, we&amp;#x27;ll be specifically looking at &lt;a href=&quot;https://github.com/SortableJS/Vue.Draggable&quot; target=&quot;_blank&quot;&gt;Vue.Draggable&lt;/a&gt;, which is a Vue.js specific implementation of &lt;a href=&quot;https://github.com/SortableJS/Sortable&quot; target=&quot;_blank&quot;&gt;Sortable.js&lt;/a&gt;. At the time of this writing, it&amp;#x27;s the most popular drag and drop library for Vue.js. Please note that the version of Vue.Draggable under consideration applies to &lt;strong&gt;Vue.js 2.x!&lt;/strong&gt; 🚨&lt;/p&gt;&lt;span class=&quot;text-center&quot;&gt;&lt;p&gt;&lt;img src=&quot;/static/start-5a4cc6ca4e9109bd312a80e436e48708.gif&quot; alt=&quot;Let&amp;#x27;s get it on&quot;/&gt;&lt;/p&gt;&lt;/span&gt;&lt;h2 id=&quot;installation&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#installation&quot; aria-label=&quot;installation permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Installation&lt;/h2&gt;&lt;p&gt;We can install Vue.Draggable in our project with npm or Yarn.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;yarn&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; vuedraggable&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; i -S vuedraggable&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;After installing the library, we have to import it into our component(s) as follows:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword module&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token imports&quot;&gt;draggable&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;vuedraggable&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;components&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    draggable&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;regular-usage&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#regular-usage&quot; aria-label=&quot;regular usage permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Regular Usage&lt;/h2&gt;&lt;p&gt;What makes Vue.Draggable so great is its ability to easily integrate with any UI components, whether it be from Vuetify or Bootstrap Vue. All you have to do is wrap your draggable elements in a &lt;code&gt;&amp;lt;draggable&amp;gt;&lt;/code&gt; tag. And that&amp;#x27;s all there is to it! 😲 However, at a bare minimum you&amp;#x27;ll want to insert a &lt;code&gt;v-model&lt;/code&gt; attached to an array with relevant data. Otherwise, your array won&amp;#x27;t update to any visual changes.&lt;/p&gt;&lt;p&gt;So far, we can only drag and drop items in their own array, but not across others (unless it is nested). In order to do so we need to add the &lt;code&gt;options&lt;/code&gt; prop from &lt;a href=&quot;https://github.com/SortableJS/Sortable#options&quot; target=&quot;_blank&quot;&gt;Sortable.js&lt;/a&gt;, to which we will assign the following object: &lt;code&gt;{ group:&amp;#x27;clothes&amp;#x27; }&lt;/code&gt;. What you name the group property is irrelevant. However, any list of items you&amp;#x27;d like to drag between should share the same name.&lt;/p&gt;&lt;p&gt;The code snippet below is an example of what was just discussed.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-html&quot;&gt;HTML&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-html&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;h2&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;My Clothing List&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;h2&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;draggable&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;v-model&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;tshirtList&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;:options&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;{group:&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;clothes&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;v-for&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;tshirt in tshirtList&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;:key&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;tshirt&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        {{ tshirt }} &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;draggable&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;draggable&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;v-model&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;pantsList&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;:options&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;{group:&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;clothes&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;v-for&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;pants in pantsList&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token tag attr-name&quot;&gt;:key&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag attr-value&quot;&gt;pants&lt;/span&gt;&lt;span class=&quot;token tag attr-value punctuation&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        {{ pants }}&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;draggable&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token tag punctuation&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&quot;gotcha&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#gotcha&quot; aria-label=&quot;gotcha permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Gotcha!&lt;/h3&gt;&lt;p&gt;While working with multiple lists, you may come across a &lt;em&gt;gotcha&lt;/em&gt; moment when trying to drop an item on an empty list. You can easily fix this issue by setting a minimum height for the &lt;code&gt;&amp;lt;draggable&amp;gt;&lt;/code&gt; component. Crisis averted... 😅&lt;/p&gt;&lt;h2 id=&quot;vuex-usage&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#vuex-usage&quot; aria-label=&quot;vuex usage permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Vuex Usage&lt;/h2&gt;&lt;p&gt;If you&amp;#x27;re working on a larger project, chances are that you&amp;#x27;re using Vuex. What you&amp;#x27;ll quickly discover is that &lt;code&gt;v-model&lt;/code&gt; isn&amp;#x27;t compatible with Vuex. This is something I&amp;#x27;ve covered in a previous article titled &lt;a href=&quot;/v-model-with-vuex/&quot;&gt;How To Use V-Model With Vuex&lt;/a&gt;. In order to mend this problem, you can simply add the code seen below:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token literal-property property&quot;&gt;computed&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;tshirtList&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;$store&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;tshirtList&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;$store&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;commit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;updateList&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; value&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;props--events&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#props--events&quot; aria-label=&quot;props  events permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Props &amp;amp; Events&lt;/h2&gt;&lt;h3 id=&quot;props&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#props&quot; aria-label=&quot;props permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Props&lt;/h3&gt;&lt;p&gt;Vue.Draggable provides a host of props that you can use to configure your drag and drop&lt;em&gt;ness&lt;/em&gt;. 😎&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;value&lt;/code&gt;: Input array to the draggable component used with &lt;code&gt;v-model&lt;/code&gt;. The array is immutable, thus recreated on each change,&lt;/li&gt;&lt;li&gt;&lt;code&gt;list&lt;/code&gt;: Alternative to the &lt;code&gt;value&lt;/code&gt; prop with a mutable array updated through the splice method,&lt;/li&gt;&lt;li&gt;&lt;code&gt;options&lt;/code&gt;: Used to initialise the &lt;a href=&quot;https://github.com/SortableJS/Sortable#options&quot; target=&quot;_blank&quot;&gt;sortable object&lt;/a&gt;,&lt;/li&gt;&lt;li&gt;&lt;code&gt;element&lt;/code&gt;: HTML tag type that the draggable component turns into upon compilation. Always defaults to &lt;code&gt;div&lt;/code&gt;,&lt;/li&gt;&lt;li&gt;&lt;code&gt;clone&lt;/code&gt;: Function called on the source component to clone an element when it returns &lt;code&gt;true&lt;/code&gt;,&lt;/li&gt;&lt;li&gt;&lt;code&gt;move&lt;/code&gt;: Cancels drag operation if function returns &lt;code&gt;false&lt;/code&gt;, and&lt;/li&gt;&lt;li&gt;&lt;code&gt;componentData&lt;/code&gt;: Used to set props or events to the child component declared by the element props.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;For more details on the props, check out &lt;a href=&quot;https://github.com/SortableJS/Vue.Draggable#props&quot; target=&quot;_blank&quot;&gt;their documentation&lt;/a&gt;.&lt;/p&gt;&lt;h3 id=&quot;events&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#events&quot; aria-label=&quot;events permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Events&lt;/h3&gt;&lt;p&gt;To sweeten the deal even more with this library, Vue.Draggable supports the following Sortable.js events:&lt;/p&gt;&lt;p&gt;&lt;code&gt;start&lt;/code&gt;, &lt;code&gt;add&lt;/code&gt;, &lt;code&gt;remove&lt;/code&gt;, &lt;code&gt;update&lt;/code&gt;, &lt;code&gt;end&lt;/code&gt;, &lt;code&gt;choose&lt;/code&gt;, &lt;code&gt;sort&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;clone&lt;/code&gt;&lt;/p&gt;&lt;p&gt;Details on their implementation can be referenced from &lt;a href=&quot;https://github.com/SortableJS/Sortable#event-object-demo&quot; target=&quot;_blank&quot;&gt;their documentation&lt;/a&gt; as well.&lt;/p&gt;&lt;h2 id=&quot;explore-the-possibilites&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#explore-the-possibilites&quot; aria-label=&quot;explore the possibilites permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Explore the possibilites&lt;/h2&gt;&lt;p&gt;There are two demos of Vue.Draggable, which you can access &lt;a href=&quot;https://david-desmaisons.github.io/draggable-example/&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt; and &lt;a href=&quot;https://sortablejs.github.io/Vue.Draggable/&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;, that show you the many features of the library, several of which I haven&amp;#x27;t covered. Why not give them a try and see what&amp;#x27;s possible?&lt;/p&gt;</content:encoded></item><item><title><![CDATA[How To Deploy Nuxt.js And Mongoose To Heroku]]></title><description><![CDATA[Deploying an app is certainly not an easy feat and can even be intimidating for many beginners. On top of that, frameworks and distinct…]]></description><link>https://dilshankelsen.com/deploy-nuxt-and-mongoose-to-heroku/</link><guid isPermaLink="false">https://dilshankelsen.com/deploy-nuxt-and-mongoose-to-heroku/</guid><pubDate>Tue, 19 Feb 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Deploying an app is certainly not an easy feat and can even be intimidating for many beginners. On top of that, frameworks and distinct hosting platforms can add an additional layer of complexity to it. When it came time to deploying my own Nuxt.js and Mongoose app to Heroku, the steps weren&amp;#x27;t clear cut and I ended up spending more time on it than I wanted. So now I&amp;#x27;ve decided to write a quick guide for those brave souls that have decided to follow the same path.&lt;/p&gt;&lt;p&gt;It should go without saying that you&amp;#x27;ll need an account on Heroku. And please be aware that your project needs to be using Git to be deployed! My condolences to those poor souls whose projects aren&amp;#x27;t... 😢&lt;/p&gt;&lt;h2 id=&quot;creating-and-connecting-a-heroku-app&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#creating-and-connecting-a-heroku-app&quot; aria-label=&quot;creating and connecting a heroku app permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Creating and connecting a Heroku app&lt;/h2&gt;&lt;p&gt;To deploy your lovely app, you&amp;#x27;ll first need to create an app on your Heroku account. After that, Heroku gives pretty clear instructions on how to deploy your app using the Heroku CLI. Assuming you already have your project set and ready to go, you&amp;#x27;d be taking the following steps:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Installing the Heroku CLI,&lt;/li&gt;&lt;li&gt;Logging into your account using &lt;code&gt;$ heroku login&lt;/code&gt;, and&lt;/li&gt;&lt;li&gt;Adding the remote Heroku repository with &lt;code&gt;$ heroku git:remote -a my-project&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Of course we still have a bit to go before our app will work, so let&amp;#x27;s continue!&lt;/p&gt;&lt;h2 id=&quot;configuring-nuxtjs&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#configuring-nuxtjs&quot; aria-label=&quot;configuring nuxtjs permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Configuring Nuxt.js&lt;/h2&gt;&lt;p&gt;Nuxt.js&amp;#x27; website provides a &lt;a href=&quot;https://nuxtjs.org/faq/heroku-deployment/&quot; target=&quot;_blank&quot;&gt;great guide&lt;/a&gt; on how to configure your project to make it run on Heroku. Although, if you&amp;#x27;re not familiar with deploying a &lt;em&gt;Node.js&lt;/em&gt; application to Heroku, it can seem a bit confusing and daunting how to get there at first. The previous section might make it seem easy, but hindsight is 20/20. 😛&lt;/p&gt;&lt;p&gt;To start off, we first need to make our project production ready using the following commands in our project CLI:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ heroku config:set &lt;/span&gt;&lt;span class=&quot;token assign-left variable&quot;&gt;NPM_CONFIG_PRODUCTION&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;false&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ heroku config:set &lt;/span&gt;&lt;span class=&quot;token assign-left variable&quot;&gt;HOST&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;.0.0&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ heroku config:set &lt;/span&gt;&lt;span class=&quot;token assign-left variable&quot;&gt;NODE_ENV&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;production&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Alternatively, you may set these environment variables in your Heroku app&amp;#x27;s dashboard settings.&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;gatsby-resp-image-wrapper&quot; style=&quot;position:relative;display:block;margin-left:auto;margin-right:auto;max-width:800px&quot;&gt;
      &lt;span class=&quot;gatsby-resp-image-background-image&quot; style=&quot;padding-bottom:24%;position:relative;bottom:0;left:0;background-image:url(&amp;#x27;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAArUlEQVQY022QjQ6FIAiFe/83bbtZ5k8q4Llgq93bwn0TUOHglOIBaoTe+x+lVDQimP3mn/HNWMA0zx94H7D5HTkfdn1c2EPEoUWrNjMa8SBovtR2x60xmAXCjK4CJqJTnYgMLhXOrfgsDovuURtdhd26IeUy/FJPiASssdSK6W0EM6+KQ0x3oYE+9ntATHmou/Kkvn2bmMLX/1BYTLXtcqJj1dZANp7lWR5nNPwv9mWJQumBMbcAAAAASUVORK5CYII=&amp;#x27;);background-size:cover;display:block&quot;&gt;&lt;/span&gt;
  &lt;img class=&quot;gatsby-resp-image-image&quot; alt=&quot;Heroku Config Variables Layout&quot; title=&quot;Heroku Config Variables Layout&quot; src=&quot;/static/0f5423a1671c807c14792f6c3915c504/5a190/image-1.png&quot; srcSet=&quot;/static/0f5423a1671c807c14792f6c3915c504/772e8/image-1.png 200w,/static/0f5423a1671c807c14792f6c3915c504/e17e5/image-1.png 400w,/static/0f5423a1671c807c14792f6c3915c504/5a190/image-1.png 800w,/static/0f5423a1671c807c14792f6c3915c504/2bef9/image-1.png 1024w&quot; sizes=&quot;(max-width: 800px) 100vw, 800px&quot; style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0&quot; loading=&quot;lazy&quot;/&gt;
    &lt;/span&gt;&lt;/p&gt;&lt;figcaption&gt;&lt;strong&gt;Figure 1. &lt;/strong&gt;Config Variables&lt;/figcaption&gt;&lt;p&gt;Next, we need to add an additional npm script in our &lt;code&gt;package.json&lt;/code&gt; file. This line is launched after all our dependencies have been installed.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;heroku-postbuild&amp;quot;&lt;/span&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;npm run build&amp;quot;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In addition to this, we also have to create a file called &lt;code&gt;Procfile&lt;/code&gt;, with no extension. It essentially contains commands that are executed by your app&amp;#x27;s &lt;a href=&quot;https://www.heroku.com/dynos&quot; target=&quot;_blank&quot;&gt;dynos&lt;/a&gt;. Heroku Dyno&amp;#x27;s are a topic of its own, but to keep it simple, imagine them as lightweight Linux containers dedicated to running your application processes.&lt;/p&gt;&lt;p&gt;In your &lt;code&gt;Procfile&lt;/code&gt;, include the following line:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;web: &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; run start&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And that&amp;#x27;s it! The only things left to do now is push your project onto Heroku with &lt;code&gt;git push heroku master&lt;/code&gt; and your app is ready to be used!&lt;/p&gt;&lt;h2 id=&quot;setting-up-mongoose&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#setting-up-mongoose&quot; aria-label=&quot;setting up mongoose permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Setting up Mongoose&lt;/h2&gt;&lt;p&gt;Unfortunately, I have some bad and goods news for my fellow readers. Using the &lt;em&gt;mLab MongoDB&lt;/em&gt; Heroku add-on requires your credit card information to avoid abuse. However, they won&amp;#x27;t charge you as long as you use their free tier plan of 496 MB. The good news? You can bypass this problem by manually creating an account at &lt;a href=&quot;https://mlab.com/&quot; target=&quot;_blank&quot;&gt;mlab.com&lt;/a&gt; and connecting it to your project. This &lt;a href=&quot;https://medium.com/@naumanzafarchaudhry/using-mongodb-on-heroku-without-verifying-your-account-9053a8c42e3c&quot; target=&quot;_blank&quot;&gt;article&lt;/a&gt; provides a decent explanation how to do so.&lt;/p&gt;&lt;p&gt;I&amp;#x27;ll demonstrate how to do the more convenient option since that&amp;#x27;s what I chose myself!&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ heroku addons:create mongolab:sandbox&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Once you run this command line in your project folder, your Mongoose equipped MongoDB database is automatically set up for us! If you haven&amp;#x27;t authenticated your account yet with your credit card information, you&amp;#x27;ll be prompted to do so with a link.&lt;/p&gt;&lt;p&gt;The last and final step is to connect our application to the database. A link to it can be accessed via the environment variable &lt;code&gt;MONGODB_URI&lt;/code&gt;, which you simply need to add in your code base and push to Heroku. On the other hand, you can obtain the link with the command below or via the app&amp;#x27;s dashboard settings and hard code it into your project (not recommended!).&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;$ heroku config:get MONGODB_URI&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;</content:encoded></item><item><title><![CDATA[How To Send Emails With Node.js and Nodemailer]]></title><description><![CDATA[Ever wanted to send emails with your own Node.js application? Well, you're in luck my friend, because with  Nodemailer  it has never been…]]></description><link>https://dilshankelsen.com/send-emails-with-nodemailer/</link><guid isPermaLink="false">https://dilshankelsen.com/send-emails-with-nodemailer/</guid><pubDate>Tue, 15 Jan 2019 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Ever wanted to send emails with your own Node.js application? Well, you&amp;#x27;re in luck my friend, because with &lt;a href=&quot;https://nodemailer.com/about/&quot; target=&quot;_blank&quot;&gt;Nodemailer&lt;/a&gt; it has never been easier to do so. Sit tight, follow this tutorial, and you&amp;#x27;ll learn in no time how to impress your peers and prospective employers with your newly learned skill. 😉&lt;/p&gt;&lt;h2 id=&quot;behind-the-scenes&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#behind-the-scenes&quot; aria-label=&quot;behind the scenes permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Behind the scenes&lt;/h2&gt;&lt;p&gt;Before we get started, you should first understand how Nodemailer actually sends an email. A backend language such as PHP uses a &lt;em&gt;sendmail&lt;/em&gt; binary installed on the backend to deliver emails. In fact, you can even use it &lt;a href=&quot;https://nodemailer.com/transports/sendmail/&quot; target=&quot;_blank&quot;&gt;with Nodemailer&lt;/a&gt;. However, this isn&amp;#x27;t the recommended choice as there&amp;#x27;s no guarantee that it&amp;#x27;s installed on your Node.js server.&lt;/p&gt;&lt;p&gt;&lt;em&gt;&amp;quot;Why&amp;#x27;s this the case?&amp;quot;&lt;/em&gt;, you ask with an inquisitive tone. Well, glad you asked Sherlock! Compared to PHP, which stack is fat, Node.js&amp;#x27; stack is fairly thin. Now, to save you all the mumbo jumbo terminology, it basically means that Node.js is fairly lightweight and small compared other server-side languages. This necessarily entails that certain features won&amp;#x27;t be available right out of the box.&lt;/p&gt;&lt;p&gt;Instead of using &lt;em&gt;sendmail&lt;/em&gt;, Nodemailer actually uses an external Simple Mail Transfer Protocol (SMTP) service to deliver emails.&lt;/p&gt;&lt;h2 id=&quot;who-goes-there&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#who-goes-there&quot; aria-label=&quot;who goes there permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Who goes there!&lt;/h2&gt;&lt;p&gt;Many free email services like Yahoo! Mail and Outlook.com make it easy for you to use your account with SMTP settings enabled. However, there&amp;#x27;s one bad boy who doesn&amp;#x27;t play by the rules: Gmail.&lt;/p&gt;&lt;p&gt;Unless you&amp;#x27;re using OAuth2 authentication, it&amp;#x27;s not recommended using them. Gmail makes it very difficult to use access their SMTP services via a script as they expect an actual person to log in, rather than a robot. Furthermore, features such as &amp;quot;&lt;a href=&quot;https://support.google.com/accounts/answer/6010255?hl=en&quot; target=&quot;_blank&quot;&gt;Less Secure&lt;/a&gt;&amp;quot; and Two Factor Authentication make it even more difficult to do so.&lt;/p&gt;&lt;p&gt;Generally, any paid or free email service should do the trick. If you&amp;#x27;d like to, you can go ahead and try it out with Gmail as well, but do expect to jump through some extra hurdles before you get a result.&lt;/p&gt;&lt;h2 id=&quot;lets-get-started&quot; style=&quot;position:relative&quot;&gt;&lt;a href=&quot;#lets-get-started&quot; aria-label=&quot;lets get started permalink&quot; class=&quot;anchor-heading before&quot;&gt;&lt;svg aria-hidden=&quot;true&quot; focusable=&quot;false&quot; height=&quot;16&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 16 16&quot; width=&quot;16&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;Let&amp;#x27;s get started!&lt;/h2&gt;&lt;p&gt;Now that we&amp;#x27;ve got that part behind us, it&amp;#x27;s time to start with the fun bits! First, we need to install Nodemailer using npm.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-bash&quot;&gt;BASH&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-bash&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; nodemailer --save&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And don&amp;#x27;t forget to import the module into your file, like this:&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; nodemailer &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;nodemailer&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Next, we need to configure our code with the appropriate settings and authentication details so we can connect to our desired SMTP server. Make sure to find the SMTP settings of your email service provider. Keep in mind that in the example below, I&amp;#x27;m connecting to the server via non-SSL settings.&lt;/p&gt;&lt;p&gt;If you&amp;#x27;re running your code outside of your host, such as a localhost, you will have to include the &lt;code&gt;tls&lt;/code&gt; property, or you&amp;#x27;ll run into problems.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; transporter &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; nodemailer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;createTransport&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;mail.website.com&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2525&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;secure&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// true for 465, false for other ports&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;auth&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;user@website.com&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;pass&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;itsasecret&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;tls&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// do not fail on invalid certs&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;rejectUnauthorized&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The following step is fairly easy as it basically consists of entering in the details of your email, as seen below.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; mailOptions &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword module&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;&amp;#x27;Mr. Fox 🦊&amp;#x27; &amp;lt;user@website.com&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// sender address&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;satoshi@bitcoin.com&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// list of receivers&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;subject&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Hello World&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// Subject line&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;Hello World?&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// plain text body&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token literal-property property&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;&amp;lt;p&amp;gt;Hello World?&amp;lt;/p&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// html body&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Believe it or not, we already did most of the work now. &lt;a href=&quot;https://www.youtube.com/watch?v=CDNmLPATW0s&quot; target=&quot;_blank&quot;&gt;&lt;em&gt;&amp;quot;Say whaaat?&amp;quot;&lt;/em&gt;&lt;/a&gt;. Contain that excitement, bruh. All that&amp;#x27;s left now is to actually send the email using the &lt;code&gt;transporter.sendMail&lt;/code&gt; function with a get request as well as handle any potential errors.&lt;/p&gt;&lt;p&gt;In the example below, I&amp;#x27;m using the Express.js framework to run the code.&lt;/p&gt;&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;div class=&quot;badge-btn-wrap&quot;&gt;&lt;div class=&quot;language-badge language-badge-javascript&quot;&gt;JAVASCRIPT&lt;/div&gt;&lt;button class=&quot;btn-copy&quot;&gt;Copy&lt;/button&gt;&lt;/div&gt;&lt;pre class=&quot;prism-code language-javascript&quot;&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; express &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;express&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; bodyParser &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;body-parser&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; nodemailer &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;nodemailer&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; app &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;express&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;bodyParser&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;quot;/&amp;quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;req&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; res&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; transporter &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; nodemailer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;createTransport&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token spread operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// paste code here&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; mailOptions &lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token spread operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// paste other code here&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;// send mail with defined transport object&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    transporter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;sendMail&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;mailOptions&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token parameter punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt; info&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token arrow operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;token keyword control-flow&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token console class-name&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&amp;#x27;Message sent: %s&amp;#x27;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt; info&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token property-access&quot;&gt;messageId&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;        res&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token method function property-access&quot;&gt;sendStatus&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;token-line&quot;&gt;&lt;span class=&quot;token plain&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Ta-da, that&amp;#x27;s it! Nothing more and nothing less. Now go out there and show the world what you can do with your new-found knowledge, you expert, you. 😋&lt;/p&gt;</content:encoded></item><item><title><![CDATA[dummy]]></title><link>https://dilshankelsen.com/__do-not-remove/</link><guid isPermaLink="false">https://dilshankelsen.com/__do-not-remove/</guid><pubDate>Sat, 11 Nov 1111 00:00:00 GMT</pubDate><content:encoded></content:encoded></item></channel></rss>