<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Jesco Wuester</title>
    <description>The latest articles on Forem by Jesco Wuester (@jsco).</description>
    <link>https://forem.com/jsco</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F184554%2F8b31192f-63d8-425a-bf73-d16c27a5ca93.jpg</url>
      <title>Forem: Jesco Wuester</title>
      <link>https://forem.com/jsco</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jsco"/>
    <language>en</language>
    <item>
      <title>How to set up Tailwindcss (jit) with the new Svelte kit</title>
      <dc:creator>Jesco Wuester</dc:creator>
      <pubDate>Wed, 07 Apr 2021 12:18:15 +0000</pubDate>
      <link>https://forem.com/jsco/how-to-set-up-tailwindcss-jit-with-the-new-svelte-kit-1p7b</link>
      <guid>https://forem.com/jsco/how-to-set-up-tailwindcss-jit-with-the-new-svelte-kit-1p7b</guid>
      <description>&lt;p&gt;Svelte kit and the new Tailwindcss just-in-time compiler just got released. Here's a super quick rundown on how to set them up (updated for tailwind 2.1 on the 8/4/2021):&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember: Svelte config files need to use the &lt;code&gt;cjs&lt;/code&gt; extension instead of &lt;code&gt;js&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Step 1: Set up the project
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;my-app
&lt;span class="nb"&gt;cd &lt;/span&gt;my-app
npm init svelte@next
npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-D&lt;/span&gt; tailwindcss autoprefixer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: postcss
&lt;/h3&gt;

&lt;p&gt;Create a &lt;code&gt;postcss.config.cjs&lt;/code&gt; file. Svelte 3 is bundled by Vite which comes with postcss support out of the box.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;autoprefixer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
    &lt;span class="na"&gt;tailwindcss&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: tailwind config
&lt;/h3&gt;

&lt;p&gt;Create a &lt;code&gt;tailwind.config.cjs&lt;/code&gt; file to enable the just-in-time compiler and add the correct paths to be purged. You can also run &lt;code&gt;npx tailwind init&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;purge&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/**/*.svelte&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4: create and import the css file
&lt;/h3&gt;

&lt;p&gt;Add these taggs somewhere in your css so that postcss knows where to insert the tailwind styles. A good place to import these is the &lt;code&gt;$layout.svelte&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="k"&gt;@tailwind&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;@tailwind&lt;/span&gt; &lt;span class="n"&gt;components&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;@tailwind&lt;/span&gt; &lt;span class="n"&gt;utilities&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there you go. If you have any issues you can &lt;a href="https://github.com/jescowuester/svelte-kit-with-tailwind-jit-example"&gt;check out the finished example on github&lt;/a&gt; or shoot me a dm on twitter.&lt;/p&gt;

</description>
      <category>svelte</category>
      <category>tailwindcss</category>
    </item>
    <item>
      <title>How to build bulletproof react components</title>
      <dc:creator>Jesco Wuester</dc:creator>
      <pubDate>Sun, 12 Apr 2020 11:08:45 +0000</pubDate>
      <link>https://forem.com/jsco/how-to-build-bulletproof-react-components-mo7</link>
      <guid>https://forem.com/jsco/how-to-build-bulletproof-react-components-mo7</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;React is a &lt;em&gt;declarative&lt;/em&gt; framework. This means instead of describing what you need to change to get to the next state (which would be &lt;em&gt;imperative&lt;/em&gt;), you just describe what the dom looks like for each possible state and let react figure out how to transition between the states. &lt;/p&gt;

&lt;p&gt;Shifting from an imperative to a declarative mindset is quite hard and often times when I spot bugs or inefficiencies in code it's because the user is still stuck in an imperative mindset. &lt;br&gt;
In this blog post I' ll try to dive deep into the declarative mindset and how you can use it to build unbreakable components.&lt;/p&gt;
&lt;h2&gt;
  
  
  Imperative vs Declarative:
&lt;/h2&gt;

&lt;p&gt;check out this example:&lt;br&gt;
&lt;iframe src="https://codesandbox.io/embed/imperative-toggle-5vx4p"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Every time you click the button the value toggles between &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt;. If we were to write this in an &lt;strong&gt;imperative&lt;/strong&gt; way it would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;toggle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;toggleState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;toggleState&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// I have to manually update the dom &lt;/span&gt;
  &lt;span class="nx"&gt;toggle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`toggle is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;toggleState&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Full example &lt;a href="https://codesandbox.io/s/imperative-toggle-5vx4p?file=/src/index.js:194-373" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And here is the same thing written in &lt;strong&gt;declarative code&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;toggle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setToggle&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// notice how I never explicitely have to update anything in the dom&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setToggle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;toggle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      toggle is &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;toggle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;full example &lt;a href="https://codesandbox.io/s/declarative-toggle-mi29h" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Every time you want to change the &lt;code&gt;isToggled&lt;/code&gt; value in the first example you have to remember to update the dom as well, which quickly leads to bugs. In React, your code "just works".&lt;/p&gt;

&lt;h2&gt;
  
  
  The Mindset
&lt;/h2&gt;

&lt;p&gt;The core of your new mindset should be this quote:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Your view should be expressed as a pure function of your application state.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;or,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;view = f(application_state)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;or,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7zeu2xsybt6zuk8ublia.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7zeu2xsybt6zuk8ublia.png" alt="view = f(application_state)"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;your data goes through a function and your view comes out the other end&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;React's function components align much closer to this mental model than their old class components. &lt;/p&gt;

&lt;p&gt;This is a bit abstract so let's apply it to our toggle component from above:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;the "toggle is" button should be expressed as a pure function of the &lt;code&gt;isToggled&lt;/code&gt; variable.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;button = f(isToggled)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsthccnpx6z85xmh2d5mj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsthccnpx6z85xmh2d5mj.png" alt="visual explanation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(I'll stick to the mathematical notation from now on but they're basically interchangeable)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's extend this example. Say whenever &lt;code&gt;isToggled&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt; I want the button to be green, otherwise, it should be red.&lt;/p&gt;

&lt;p&gt;A common beginner mistake would be to write something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;isToggled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIsToggled&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setColor&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;green&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="nf"&gt;setIsToggled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;toggle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;setColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;toggle&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;green&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      toggle is &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isToggled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we write this in our mathematical notation we get&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;button = f(isToggled, color)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;right now our &lt;code&gt;application_state&lt;/code&gt; is made out of &lt;code&gt;isToggled&lt;/code&gt; and &lt;code&gt;color&lt;/code&gt;, but if we look closely we can see that &lt;code&gt;color&lt;/code&gt; can be expressed as a function of &lt;code&gt;isToggled&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;color = f(isToggled)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;or as actual code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const color = isToggled ? 'green' : 'red'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This type of variable is often referred to as &lt;code&gt;derived state&lt;/code&gt; (since &lt;code&gt;color&lt;/code&gt; was "derived" from &lt;code&gt;isToggled&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;In the end this means our component still looks like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;button = f(isToggled)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  How to take advantage of this in the real world
&lt;/h2&gt;

&lt;p&gt;In the example above it was quite easy to spot the duplicate state, even without writing it out in our mathematical notation, but as our apps grow more and more complex, it gets harder to keep track of all your application state and duplicates start popping up. &lt;br&gt;
A common symptom of this is a lot of rerenders and stale values.&lt;/p&gt;

&lt;p&gt;Whenever you see a complex piece of logic, take a few seconds to think about all the possible pieces of state you have. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzscgodw8q8ytjw60wzk5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzscgodw8q8ytjw60wzk5.png" alt="Illustration of state in ui"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;dropdown = f(selectedValue, arrowDirection, isOpen, options, placeholder)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;then you can quickly sort out unnecessary state&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;arrowDirection = f(isOpen) -&amp;gt; arrowDirection can be derived&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can also sort what state will be in the component and what will come in as props. &lt;code&gt;isOpen&lt;/code&gt; for example usually doesn't need to be accessed from the outside of a dropdown.&lt;br&gt;
From that we can tell that our component's api is probably going to look like this: &lt;code&gt;&amp;lt;dropdown options={[item1, item2]} selectedValue={null} placeholder='Favorite food' /&amp;gt;&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Writing the component now will be incredibly easy since you already know exactly how it's going to be structured. All you need to do now figure out is how to render your state to the dom.&lt;/p&gt;
&lt;h2&gt;
  
  
  One more example
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fobxotadbml3hk554tmjs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fobxotadbml3hk554tmjs.png" alt="pagination"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This looks like a lot of state at first glance, but if we look closely we can see that most of them can be derived:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;isDisabled = f(selectedValue, range)&lt;/code&gt;&lt;br&gt;
&lt;code&gt;"..." position = f(selectedValue, range)&lt;/code&gt;&lt;br&gt;
&lt;code&gt;middle fields = f(selectedValue, range)&lt;/code&gt;&lt;br&gt;
&lt;code&gt;amount of fields = f(selectedValue, range)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So what remains, in the end, is just&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;pagination = f(selectedValue, range)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;here's my implementation: &lt;br&gt;
&lt;iframe src="https://codesandbox.io/embed/pagination-z2951"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;It's robust, fast and relatively easy to read. &lt;/p&gt;

&lt;p&gt;Let's take it one step further and change the route to &lt;code&gt;/${pageNumber}&lt;/code&gt; whenever the pagination updates.&lt;/p&gt;

&lt;p&gt;Your answer may look somewhat like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;history&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useHistory&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setPage&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPage&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="nf"&gt;setPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;newPage&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;},[])&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"App"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Pagination&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;range&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleChange&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If it does, then I have some bad news: you have duplicate state. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;pageNumber = f(window.href)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;pageNumber doesn't need its own state, instead, the state is stored in the url. &lt;a href="https://codesandbox.io/s/pagination-with-router-gdqjy?file=/src/App.js:234-276" rel="noopener noreferrer"&gt;here&lt;/a&gt; is an implementation of that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other implications
&lt;/h2&gt;

&lt;p&gt;Another big implication of our new mindset is that you should stop thinking in lifecycles.&lt;br&gt;
Since your component is just a function that takes in some state and returns a view it doesn't matter when, where and how your component is called, mounted or updated. Given the same input, it should always return the same output. This is what it means for a component to be &lt;strong&gt;pure&lt;/strong&gt;. &lt;br&gt;
That's one of the reasons why hooks only have &lt;code&gt;useEffect&lt;/code&gt; instead of &lt;code&gt;componentDidMount&lt;/code&gt; / &lt;code&gt;componentDidUpdate&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Your side effects should also always follow this data flow. Say you want to update your database every time your user changes the page, you could do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;newPage&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;updateDatabase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but really you don't want to update your database whenever the user clicks, you want to update your database whenever the value changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;updateDatabase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just like your view, your side effects should also be a function of your state.&lt;/p&gt;

&lt;h2&gt;
  
  
  Going even deeper
&lt;/h2&gt;

&lt;p&gt;There are a couple of exceptions to this rule in react right now, a significant one is data fetching. Think about how we usually fetch data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;isLoading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIsLoading&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nf"&gt;setIsLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;something&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
   &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nf"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="nf"&gt;setIsLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;},[])&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DataComponent&lt;/span&gt; &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;loading...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is a ton of duplicate state here, both &lt;code&gt;isLoading&lt;/code&gt; and &lt;code&gt;data&lt;/code&gt; just depend on whether our fetch promise has been resolved. &lt;br&gt;
We need to do it this way right now because React can't resolve promises yet.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://svelte.dev/docs#await" rel="noopener noreferrer"&gt;Svelte&lt;/a&gt; solves it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight svelte"&gt;&lt;code&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;#await&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- promise is pending --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;waiting for the promise to resolve...&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;:then&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- promise was fulfilled --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;The value is &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;:catch&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- promise was rejected --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Something went wrong: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;/await&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React is working on something similar with &lt;a href="https://reactjs.org/docs/concurrent-mode-suspense.html#what-is-suspense-exactly" rel="noopener noreferrer"&gt;suspense for data fetching&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another big point is animation. Right now, updating state at 60fps is often not possible. A great library that solves that in a declarative way is &lt;a href="https://www.react-spring.io/" rel="noopener noreferrer"&gt;react spring&lt;/a&gt;. &lt;a href="https://svelte.dev/docs#animate_fn" rel="noopener noreferrer"&gt;Svelte again has a native solution for this&lt;/a&gt;  and I wouldn't be surprised if that's something else react will look at in the future.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;whenever&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;your app rerenders often for no real reason&lt;/li&gt;
&lt;li&gt;you have to manually keep things in sync&lt;/li&gt;
&lt;li&gt;you have issues with stale values&lt;/li&gt;
&lt;li&gt;you don't know how to structure complex logic&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;take a step back, look at your code and repeat in your head:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Your view should be expressed as a pure function of your application state.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thanks for reading ❤&lt;/p&gt;

&lt;p&gt;If you didn't have that "aha-moment" yet I recommend building out the pagination or any component that you can think of and follow exactly the steps outlined above.&lt;/p&gt;

&lt;p&gt;If you want to dive deeper into the topic I recommend these 2 posts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://medium.com/@mweststrate/pure-rendering-in-the-light-of-time-and-state-4b537d8d40b1" rel="noopener noreferrer"&gt;https://medium.com/@mweststrate/pure-rendering-in-the-light-of-time-and-state-4b537d8d40b1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://rauchg.com/2015/pure-ui/" rel="noopener noreferrer"&gt;https://rauchg.com/2015/pure-ui/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you think there is something I could make clearer or have any questions/remarks feel free to &lt;a href="https://twitter.com/JescoWuester" rel="noopener noreferrer"&gt;tweet at me&lt;/a&gt; or just leave a comment here.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Webpack, babel and the "Modern Webapp" explained</title>
      <dc:creator>Jesco Wuester</dc:creator>
      <pubDate>Thu, 23 Jan 2020 07:46:51 +0000</pubDate>
      <link>https://forem.com/jsco/webpack-babel-and-the-modern-webapp-explained-5a2</link>
      <guid>https://forem.com/jsco/webpack-babel-and-the-modern-webapp-explained-5a2</guid>
      <description>&lt;p&gt;Webpack and Babel weirded me out for a long time. Luckily Create-react-app always took care of all of that stuff for me so I never had to peek into what it does. Turns out it's not all that complicated and learning it is a crucial step if you want to become a better developer. &lt;/p&gt;

&lt;h2&gt;
  
  
  Webpack?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Webpack is a module bundler.&lt;br&gt;
&lt;strong&gt;Great, but what does that mean?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To understand that we have to go through a bit of history:&lt;br&gt;
In the dark ages javascript was used like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
alert('stay on this page to win 1000000$ !!!1!')
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Javascript was mostly used for small scripts and popups so you didn't need a lot of code. As the web matured and apps became more complex writing all your code in just one js file became unmaintainable due to the sheer length of the files but also because naming collisions became unavoidable. Some hacky solutions like &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/IIFE"&gt;IFFEEs&lt;/a&gt; tried to solve the problem, but they all had vital flaws, IFFEEs were notoriously slow for example. Finally, Webpack was born. &lt;br&gt;
In oversimplified terms, Webpack takes all your javascript files and creates one huge javascript file out of them that you can then include in your HTML page. This huge file is called the &lt;strong&gt;bundle&lt;/strong&gt; (and often named &lt;strong&gt;bundle.js&lt;/strong&gt;. &lt;br&gt;
To do that it renames all variables (or namespaces them). This also includes the javascript files from you &lt;code&gt;node_modules&lt;/code&gt; folder!&lt;br&gt;
So instead of &lt;code&gt;index.js&lt;/code&gt;, &lt;code&gt;button.js&lt;/code&gt; and &lt;code&gt;node_modules/some_package/index.js&lt;/code&gt; you only get 1 &lt;code&gt;bundle.js&lt;/code&gt; file that includes the code from all of them.&lt;/p&gt;

&lt;p&gt;Webpack now also has a ton of &lt;strong&gt;custom loaders&lt;/strong&gt; to include not just js files but also fonts, CSS files, images and pretty much anything else you could imagine. (A custom loader is just a file that tells webpack how to include something like an image in your bundle). It also automatically removes all variables that you don't use from your bundle (this is called threeshaking) and does a ton of other cool stuff to make your bundle smaller (since every user of your website has to download your bundle before being able to use it a smaller bundle means faster page load).&lt;/p&gt;

&lt;p&gt;You can read more about the history of webpack &lt;a href="https://webpack.js.org/concepts/why-webpack/"&gt;here&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Babel
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Babel is a JavaScript compiler.&lt;br&gt;
&lt;strong&gt;Is that different from a module bundler?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Yes! Babel was born out of a need for backwards compatibility. Your websites probably need to support old browsers, sometimes as old as IE9.&lt;br&gt;
But you still want to use new javascript features (like &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt;, &lt;code&gt;class&lt;/code&gt; or &lt;code&gt;.then()&lt;/code&gt;. Babel takes your code and rewrites it as backwards-compatible code (compiles it). So &lt;code&gt;const myName = 'tim'&lt;/code&gt; becomes &lt;code&gt;var myName = 'tim'&lt;/code&gt;. Babel makes sure that everything still works as intended, so for example when you try to reassign &lt;code&gt;myName&lt;/code&gt; it creates an error (since &lt;code&gt;const&lt;/code&gt; can't be reassigned).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y0AHxnOQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/bdukhltw3umswm5ixjx9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y0AHxnOQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/bdukhltw3umswm5ixjx9.png" alt="Example of babel compiled code" width="880" height="250"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Play around with it &lt;a href="https://babeljs.io/repl#?browsers=&amp;amp;build=&amp;amp;builtIns=false&amp;amp;spec=false&amp;amp;loose=false&amp;amp;code_lz=MYewdgzgLgBGCGBbApjAvDARAFQJaMwCgEV0sBBAG2QA9Mg&amp;amp;debug=false&amp;amp;forceAllTransforms=false&amp;amp;shippedProposals=false&amp;amp;circleciRepo=&amp;amp;evaluate=false&amp;amp;fileSize=false&amp;amp;timeTravel=false&amp;amp;sourceType=module&amp;amp;lineWrap=true&amp;amp;presets=es2015%2Creact%2Cstage-2&amp;amp;prettier=false&amp;amp;targets=&amp;amp;version=7.8.3&amp;amp;externalPlugins="&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It can also transform syntax like &lt;a href="https://babeljs.io/repl#?browsers=&amp;amp;build=&amp;amp;builtIns=false&amp;amp;spec=false&amp;amp;loose=false&amp;amp;code_lz=MYewdgzgLgBAQgVylcMC8MAUBKNA-AbwCgYYAnAUygTLExgB4AjJFMPACwoBtuQYAVhAAeDAPQtk4PNiIBfIA&amp;amp;debug=false&amp;amp;forceAllTransforms=false&amp;amp;shippedProposals=false&amp;amp;circleciRepo=&amp;amp;evaluate=false&amp;amp;fileSize=false&amp;amp;timeTravel=false&amp;amp;sourceType=module&amp;amp;lineWrap=true&amp;amp;presets=es2015%2Creact%2Cstage-2&amp;amp;prettier=false&amp;amp;targets=&amp;amp;version=7.8.3&amp;amp;externalPlugins="&gt;react's jsx&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  The Modern Setup
&lt;/h2&gt;

&lt;p&gt;The modern web development setup now takes care in 2 places:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Compilation and Bundling (which happens in NodeJs on your computer)&lt;/li&gt;
&lt;li&gt;Execution (Which usually happens in the user's browser)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Where to go next
&lt;/h2&gt;

&lt;p&gt;If you're interested in getting to know any of these topics deeper I recommend doing any of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set up a complete react setup (with babel and jsx) &lt;strong&gt;using only the webpack and babel docs&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Learn about all the different module systems used today (cjs, es6 import, amd)&lt;/li&gt;
&lt;li&gt;Learn about AST's and write your own babel plugin (I swear its easier than it sounds)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hope this post helped clear some things up. If you have any feedback on this blogpost leave a comment down below, I really appreciate it!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webpack</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What are some real-world scenarios where redux is needed?</title>
      <dc:creator>Jesco Wuester</dc:creator>
      <pubDate>Wed, 18 Dec 2019 12:38:40 +0000</pubDate>
      <link>https://forem.com/jsco/what-are-some-real-world-scenarios-where-redux-is-needed-52nn</link>
      <guid>https://forem.com/jsco/what-are-some-real-world-scenarios-where-redux-is-needed-52nn</guid>
      <description>&lt;p&gt;I'm struggling to see how redux is helpful. In pretty much every case state should be controlled by its own component and not made global (like redux does). There may be some exceptions like a user or shopping cart object or sth like that, but those can be solved with just one context provider. &lt;br&gt;
Has anyone worked on a codebase (or does anyone know of an example) that really needs redux?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>react</category>
    </item>
    <item>
      <title>What eslint config do you use with react</title>
      <dc:creator>Jesco Wuester</dc:creator>
      <pubDate>Wed, 11 Dec 2019 14:11:41 +0000</pubDate>
      <link>https://forem.com/jsco/what-eslint-config-do-you-use-with-react-22k4</link>
      <guid>https://forem.com/jsco/what-eslint-config-do-you-use-with-react-22k4</guid>
      <description>&lt;p&gt;I used to use eslint-plugin-react but found more and more nonsense rules in there like &lt;a href="https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-props-no-spreading.md"&gt;https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-props-no-spreading.md&lt;/a&gt;. I only want to see an error when there's an actual bug or bad practice (like unused variables, same variable name in upper scope, forgot to close a bracket etc). No styling stuff (I have prettier for that)&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>react</category>
    </item>
    <item>
      <title>A comprehensive guide to responsive Images (picture, srcset, source etc)</title>
      <dc:creator>Jesco Wuester</dc:creator>
      <pubDate>Mon, 18 Nov 2019 14:57:37 +0000</pubDate>
      <link>https://forem.com/jsco/a-comprehensive-guide-to-responsive-images-picture-srcset-source-etc-4adj</link>
      <guid>https://forem.com/jsco/a-comprehensive-guide-to-responsive-images-picture-srcset-source-etc-4adj</guid>
      <description>&lt;p&gt;The specifications for responsive images went through years of debate and many iterations + it's a pretty wide area to cover so the syntax is a bit over the place. In this article, I'll try to give you a full understanding of all the different methods for creating responsive images. &lt;br&gt;
I'll break it down into 3 parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why you should use responsive images&lt;/li&gt;
&lt;li&gt;A syntax breakdown&lt;/li&gt;
&lt;li&gt;My recommended setup for responsive images&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Why
&lt;/h2&gt;

&lt;p&gt;As discussed in the &lt;a href="https://dev.to/jsco/understanding-image-formats-on-the-web-4op8"&gt;first part of this series&lt;/a&gt;, big images can make your site incredibly slow. We already cut down the file size by compressing our images, but on phones, you still get the same image resolution as on desktop. Responsive images allow you to serve different resolutions depending on either &lt;strong&gt;screen size&lt;/strong&gt;, &lt;strong&gt;screen resolution&lt;/strong&gt; or &lt;strong&gt;image width&lt;/strong&gt;. &lt;/p&gt;
&lt;h2&gt;
  
  
  Syntax
&lt;/h2&gt;

&lt;p&gt;There is a multitude of ways to make your image responsive, to understand how they work we have to break down a bit of new syntax:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;src&lt;/li&gt;
&lt;li&gt;srcset&lt;/li&gt;
&lt;li&gt;picture&lt;/li&gt;
&lt;li&gt;source&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's start with an example and go over what all the pieces of syntax do.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"file-200px.jpg"&lt;/span&gt;
     &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;"some file"&lt;/span&gt;
     &lt;span class="na"&gt;srcset=&lt;/span&gt;&lt;span class="s"&gt;"file-200px.jpg 1x, 
             file-400px.jpg 2x, 
             file-600px.jpg 3x&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  src
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;src="file-200px.jpg"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This determines the source of the image. It's also used as the source on all browsers that don't support srcset.&lt;/p&gt;

&lt;h4&gt;
  
  
  srcset
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;     srcset="file-200px.jpg 1x, 
             file-400px.jpg 2x, 
             file-600px.jpg 3x
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Srcset&lt;/code&gt; is made up of one or more &lt;strong&gt;image candidates&lt;/strong&gt;. The candidates are separated by a comma and each candidate contains an image URL and an optional condition. If the condition is fulfilled the candidate source will be used instead of the default source.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fjrl30ux9222p3ba40apm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fjrl30ux9222p3ba40apm.png" alt="srcset syntax breakdown"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  the conditions
&lt;/h4&gt;

&lt;h5&gt;
  
  
  1x, 2x etc (display density)
&lt;/h5&gt;

&lt;p&gt;With the introduction of &lt;a href="https://medium.com/@elad/understanding-the-difference-between-css-resolution-and-device-resolution-28acae23da0b" rel="noopener noreferrer"&gt;high density screens&lt;/a&gt; CSS had to make a distinction between a pixel as a measurement unit and an actual pixel on a screen so that the layout wouldn't look tiny on high-resolution screens. &lt;br&gt;
On the iPhone 11, for example, every CSS pixel is actually 2 device pixels big. This means the &lt;strong&gt;device pixel ratio&lt;/strong&gt; is &lt;strong&gt;2x&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;     srcset="file-200px.jpg 1x, 
             file-400px.jpg 2x, 
             file-600px.jpg 3x"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what our example does is it serves users with a high-density screen a higher resolution image so that they can take full advantage of their high resolution. At the same time, we make sure that users with a lower density screen don't have to waste unnecessary data.&lt;/p&gt;

&lt;p&gt;Translated into words the syntax above means&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if the user has a device pixel ratio of 1x, serve him &lt;code&gt;file-200px.jpg&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;if the user has a device pixel ratio of 2x, serve him &lt;code&gt;file-400px.jpg&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;etc&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  200w, 400w etc (width descriptors)
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;     srcset="file-200px.jpg 200w, 
             file-400px.jpg 400w, 
             file-600px.jpg 600w"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The width descriptor describes the resolution of your file. So the actual width in pixels when you open your image in a photo editor or so. The browser will use that width to decide what image source to use. Unfortunately, just the image resolution isn't enough information for the browser to make a decision.&lt;br&gt;
When the browser begins downloading its assets it doesn't know the &lt;strong&gt;width of the slot&lt;/strong&gt; that the image element will eventually fill in the page, so to use width descriptors we &lt;strong&gt;have to tell the browser what size our image will have&lt;/strong&gt;! Width descriptors &lt;strong&gt;can't be used without the sizes attribute&lt;/strong&gt;, so the whole syntax actually looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;     srcset="file-200px.jpg 200w, 
             file-400px.jpg 400w, 
             file-600px.jpg 600w"
     sizes="(min-width: 900px) 700px,
            (min-width: 400px) 80vw,
            100vw"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This syntax can be intimidating at first but is actually fairly straight forward, all we do by specifying the sizes is tell the browser:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"when the screen is more than 900px wide, the width of the slot the image will fill is 700px.&lt;/li&gt;
&lt;li&gt;"when the screen is more than 400px wide the width of the slot the image will fill is 80vw.&lt;/li&gt;
&lt;li&gt;"when none of the media conditions are true the width of the slot the image will fill is 100vw"&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;You can use absolute units (like &lt;em&gt;px&lt;/em&gt; or &lt;em&gt;em&lt;/em&gt;) or a length relative to the viewport (&lt;em&gt;vw&lt;/em&gt;) but &lt;strong&gt;not&lt;/strong&gt; percentages.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The browser is going to use these sizes to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Look at its device width&lt;/li&gt;
&lt;li&gt;Work out which media condition in the sizes list is the first one to be true&lt;/li&gt;
&lt;li&gt;Look at the slot size given to that media query&lt;/li&gt;
&lt;li&gt;Load the image referenced in the srcset list that most closely matches the chosen slot size&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  The picture tag
&lt;/h4&gt;

&lt;p&gt;The syntax above can be really hard to keep track off if not properly formatted and is still a bit limited. Luckily there's an alternative.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;picture&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;source&lt;/span&gt; &lt;span class="na"&gt;srcset=&lt;/span&gt;&lt;span class="s"&gt;"example-200px.jpg"&lt;/span&gt; &lt;span class="na"&gt;media=&lt;/span&gt;&lt;span class="s"&gt;"(min-width: 800px)"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;source&lt;/span&gt; &lt;span class="na"&gt;srcset=&lt;/span&gt;&lt;span class="s"&gt;"example.webp"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"image/webp"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"example.jpg"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/picture&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The picture tag takes in one &lt;code&gt;img&lt;/code&gt; element and zero or more &lt;code&gt;source&lt;/code&gt; tags. A good way to think about it is that the &lt;code&gt;picture&lt;/code&gt; element supercharges the &lt;code&gt;img&lt;/code&gt; element in it with the different &lt;code&gt;sources&lt;/code&gt;. Browsers that don't support the &lt;code&gt;picture&lt;/code&gt; or &lt;code&gt;source&lt;/code&gt; tag will simply ignore it and just render the &lt;code&gt;img&lt;/code&gt; element in it. &lt;/p&gt;

&lt;h4&gt;
  
  
  The source tag
&lt;/h4&gt;

&lt;p&gt;You may have already seen the source tag inside of a &lt;code&gt;video&lt;/code&gt; tag or so. Inside of the &lt;code&gt;picture&lt;/code&gt; tag it can be used to accomplish a variety of things:&lt;/p&gt;

&lt;h5&gt;
  
  
  use modern image formats
&lt;/h5&gt;

&lt;p&gt;In the example above we use &lt;code&gt;&amp;lt;source srcset="example.webp" type="image/webp"&amp;gt;&lt;/code&gt;. Webp is a modern image format that I talked about in &lt;a href="https://sv.fyi/image-formats#webp" rel="noopener noreferrer"&gt;the first part of this series&lt;/a&gt;. Since browser support for it is still a bit spotty we can't use webp as our only src for an image but with the &lt;code&gt;source&lt;/code&gt; tag we can specify a &lt;code&gt;webp&lt;/code&gt; source and all browsers that support webp will use it. Be sure to specify the &lt;code&gt;type&lt;/code&gt; as &lt;code&gt;image/webp&lt;/code&gt; so that the browser can read it.&lt;/p&gt;

&lt;h5&gt;
  
  
  change the src depending on viewport size
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;picture&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;source&lt;/span&gt; &lt;span class="na"&gt;media=&lt;/span&gt;&lt;span class="s"&gt;"(max-width: 800px)"&lt;/span&gt; &lt;span class="na"&gt;srcset=&lt;/span&gt;&lt;span class="s"&gt;"example-small.jpg"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;source&lt;/span&gt; &lt;span class="na"&gt;media=&lt;/span&gt;&lt;span class="s"&gt;"(max-width: 1200px)"&lt;/span&gt; &lt;span class="na"&gt;srcset=&lt;/span&gt;&lt;span class="s"&gt;"example-large.jpg"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"example-default.jpg"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/picture&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This syntax is pretty self-explanatory when the viewport size is smaller than 800px it will display a smaller version of the image. Instead of supplying a lower resolution version of your image you can also supply a completely different image on smaller screens. This can be really useful if you want to show for example a less detailed, or more zoomed-in version of an image on smaller screens.&lt;/p&gt;

&lt;h5&gt;
  
  
  use srcset and size
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;source&lt;/span&gt; &lt;span class="na"&gt;srcset=&lt;/span&gt;&lt;span class="s"&gt;"file-200px.jpg 200w, 
             file-400px.jpg 400w, 
             file-600px.jpg 600w"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;a source can also support a srcset and sizes tag, just like the regular &lt;code&gt;img&lt;/code&gt; tag.&lt;/p&gt;

&lt;h2&gt;
  
  
  How
&lt;/h2&gt;

&lt;p&gt;Your approach should always depend on your use case. I would always use a picture tag since it lets you use webp and is the most flexible. When working with a framework like vue, react, svelte etc I recommend making a reusable component that integrates with your backend setup. For example in react it could be something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MyPictureComponent&lt;/span&gt;
     &lt;span class="na"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="na"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;allSources&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;medium&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="na"&gt;mobile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;allSources&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;small&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="na"&gt;desktop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;allSources&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;large&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; 
&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which would render sth like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;picture&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;source&lt;/span&gt; &lt;span class="na"&gt;media&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"(max-width: 800px)"&lt;/span&gt; &lt;span class="na"&gt;srcset&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;small&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;webp&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"image/webp"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;source&lt;/span&gt; &lt;span class="na"&gt;media&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"(max-width: 1200px)"&lt;/span&gt; &lt;span class="na"&gt;srcset&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;large&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;webp&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"image/webp"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;source&lt;/span&gt; &lt;span class="na"&gt;media&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"(max-width: 800px)"&lt;/span&gt; &lt;span class="na"&gt;srcset&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;small&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;jpg&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;source&lt;/span&gt; &lt;span class="na"&gt;media&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"(max-width: 1200px)"&lt;/span&gt; &lt;span class="na"&gt;srcset&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;large&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;jpg&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;jpg&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;picture&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That way webp is handled by default and the whole thing is a bit less verbose. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do you have a nice abstraction for responsive images? Let me know 👇&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  summary
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;the srcset attribute allows you to supercharge your img tag by providing it with a list of alternative sources and conditions.&lt;/li&gt;
&lt;li&gt;conditions can be based on the screen density and image size.&lt;/li&gt;
&lt;li&gt;the picture tag allows you to specify a bunch of sources for an image to specify different images at different screen sizes and use modern image formats with a fallback.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>html</category>
      <category>webdev</category>
      <category>css</category>
    </item>
    <item>
      <title>Understanding Image formats on the Web</title>
      <dc:creator>Jesco Wuester</dc:creator>
      <pubDate>Thu, 14 Nov 2019 17:44:29 +0000</pubDate>
      <link>https://forem.com/jsco/understanding-image-formats-on-the-web-4op8</link>
      <guid>https://forem.com/jsco/understanding-image-formats-on-the-web-4op8</guid>
      <description>&lt;p&gt;Images are probably the #1 performance bottleneck on websites. Tons of developers spend hours shaving of some kb of their bundle while serving megabytes worth of images. &lt;/p&gt;

&lt;p&gt;Image Optimization consists of 3 main parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using the right image format(s)&lt;/li&gt;
&lt;li&gt;Using responsive images (will be release 18.11)&lt;/li&gt;
&lt;li&gt;Using Progressive/Lazy image loading (will be release 25.11)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this series, I'll go over each off these topics so that hopefully by the end you'll have a full understanding of how images on the web work. This first part covers what format you should use for your images.&lt;/p&gt;

&lt;h1&gt;
  
  
  Using the right image format(s)
&lt;/h1&gt;

&lt;p&gt;There's 4 main formats you should know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;jpg&lt;/li&gt;
&lt;li&gt;png&lt;/li&gt;
&lt;li&gt;svg&lt;/li&gt;
&lt;li&gt;webp&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's go through each of them&lt;/p&gt;

&lt;h2&gt;
  
  
  JPG (or jpeg, its the same)
&lt;/h2&gt;

&lt;p&gt;Probably the most common image format, jpg's are small and can display a wide range of colours so they're &lt;strong&gt;well suited for photos&lt;/strong&gt;. Due to the way JPGs are compressed (which is called "lossy compression") they're pretty bad at showing sharp edges (like between the white and the black in the image below), so they're &lt;strong&gt;not well suited for logos and illustrations&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fgqlux85i63of2z5zup0e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fgqlux85i63of2z5zup0e.png" alt="blurry Icon"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There's also JPEG2000 and JPEG XR which were created after the original jpg spec to improve on it, but browser support is &lt;a href="https://caniuse.com/#search=JPEG%20XR" rel="noopener noreferrer"&gt;so&lt;/a&gt; &lt;a href="https://caniuse.com/#search=JPEG%202000" rel="noopener noreferrer"&gt;slim&lt;/a&gt; that you shouldn't use them on the web.&lt;/p&gt;

&lt;h2&gt;
  
  
  PNG
&lt;/h2&gt;

&lt;p&gt;PNGs use "lossless compression" which makes them much better suited to show sharp edges. They also support opacity. The drawback is that the file size is larger than that of a JPG. You can use PNGs for illustrations and logos but there is often a better option:&lt;/p&gt;

&lt;h2&gt;
  
  
  SVG
&lt;/h2&gt;

&lt;p&gt;SVG stands for "scalable vector graphic". SVGs look a bit like HTML which makes them incredibly small and versatile. SVGs can be edited with CSS and animated in a lot of different ways. They can also be scaled to any size since they're not encoded in pixels (see example down below). If you have the option it's usually best to use SVG for Icons, Logos etc, but often times (especially when dealing with illustrations) you will be stuck with a PNG and you can't reliably convert a png to an svg.&lt;/p&gt;

&lt;p&gt;This is what an svg looks like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;svg width="12" height="12" viewBox="0 0 12 12" fill="none" xmlns="http://www.w3.org/2000/svg"&amp;gt;
&amp;lt;path d="M6 11.377C6.22266 11.377 6.48633 11.2715 6.73242 11.1133C9.92578 9.0625 11.9238 6.60156 11.9238 4.16992C11.9238 1.96094 10.4473 0.460938 8.53125 0.460938C7.5 0.460938 6.63281 0.953125 6 2.00781C5.36719 0.947266 4.49414 0.460938 3.46289 0.460938C1.54688 0.460938 0.0703125 1.96094 0.0703125 4.16992C0.0703125 6.60156 2.07422 9.0625 5.26172 11.1133C5.50781 11.2715 5.77148 11.377 6 11.377Z" fill="currentColor"/&amp;gt;
&amp;lt;/svg&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's a heart :D &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Frn85r1aib34e0mvauynd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Frn85r1aib34e0mvauynd.png" alt="Heart Shape"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  WEBP
&lt;/h2&gt;

&lt;p&gt;WEBP was created by Google in 2010 as an image format for the web. It supports both lossless and lossy compression, supports opacity and is usually smaller than both png and jpg. It's pretty amazing and has &lt;a href="https://caniuse.com/#search=webp" rel="noopener noreferrer"&gt;decent browser support&lt;/a&gt; (80% at the time of writing). WEBP should always be used with a fallback option to cover that remaining 20% (you'll learn how to do that in the next part). It will have a huge positive impact on your site though and support is only expected to grow.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you compress
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;If you want to compress just a few static images use &lt;a href="https://squoosh.app/" rel="noopener noreferrer"&gt;https://squoosh.app/&lt;/a&gt;. It also lets you play around with different types of compression and see the difference in quality and filesize in real-time! &lt;/li&gt;
&lt;li&gt;If you need to compress a lot of images/dynamic images you can use something like &lt;a href="https://github.com/lovell/sharp" rel="noopener noreferrer"&gt;sharp&lt;/a&gt; on your backend to compress your image to multiple versions.&lt;/li&gt;
&lt;li&gt;Alternatively, there are also services like Cloudinary that compress images for you and distribute them over a CDN (which is also usually faster than your server)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use SVG for logos/icons etc or anything you want to animate&lt;/li&gt;
&lt;li&gt;Use WEBP for everything else with either JPG or PNG as a fallback&lt;/li&gt;
&lt;li&gt;Use JPEG for Photos (as a fallback for webp)&lt;/li&gt;
&lt;li&gt;Use PNG for logos/illustrations etc when no SVG is available (as a fallback for webp)&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>html</category>
      <category>webdev</category>
      <category>webperf</category>
    </item>
  </channel>
</rss>
