<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: sarioglu</title>
    <description>The latest articles on DEV Community by sarioglu (@sarioglu).</description>
    <link>https://dev.to/sarioglu</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%2F264728%2Fbc028487-eb42-47f5-b287-d436ec63cac3.png</url>
      <title>DEV Community: sarioglu</title>
      <link>https://dev.to/sarioglu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sarioglu"/>
    <language>en</language>
    <item>
      <title>Why I prefer "connect" over "useSelector" using redux?</title>
      <dc:creator>sarioglu</dc:creator>
      <pubDate>Mon, 24 May 2021 06:52:31 +0000</pubDate>
      <link>https://dev.to/sarioglu/why-i-prefer-connect-over-useselector-using-redux-2jb9</link>
      <guid>https://dev.to/sarioglu/why-i-prefer-connect-over-useselector-using-redux-2jb9</guid>
      <description>&lt;p&gt;It's been almost two years since new hooks have arrived into &lt;code&gt;react-redux&lt;/code&gt; and I've had enough chance to work on multiple long-running projects that uses both the old and new approaches to connect with Redux. Here is my conclusion for short: &lt;code&gt;connect&lt;/code&gt; is better that &lt;code&gt;useSelector&lt;/code&gt;. Here is why:&lt;/p&gt;

&lt;p&gt;In version 7.1.1, &lt;code&gt;react-redux&lt;/code&gt; introduced their hooks API and updated their website with a &lt;a href="https://react-redux.js.org/api/hooks" rel="noopener noreferrer"&gt;tip&lt;/a&gt; that endorses hooks over the old &lt;code&gt;higher-order component&lt;/code&gt; approach:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We recommend using the React-Redux hooks API as the default approach in your React components.&lt;br&gt;
The existing connect API still works and will continue to be supported, but the hooks API is simpler and works better with TypeScript.&lt;/p&gt;
&lt;/blockquote&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%2Fuploads%2Farticles%2Fik4apyv8wtpgmyyk0980.jpg" 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%2Fuploads%2Farticles%2Fik4apyv8wtpgmyyk0980.jpg" alt="Toy Story scene with "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;React hooks made a great success since it's first introduction. It enables us to write tidy and understandable  logic blocks. Yet, it doesn't mean that it is a one-size-fits-all solution for every use-case.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ease of use
&lt;/h2&gt;

&lt;p&gt;I must admit that hooks are easier to grasp and use than connected components. Using &lt;code&gt;connect&lt;/code&gt; requires more boilerplate code. It also requires to know concepts like higher-order components, &lt;code&gt;bindActionCreators&lt;/code&gt;, etc. to understand how it actually works. On the other hand, knowing how hooks work is enough to understand &lt;code&gt;useSelector&lt;/code&gt; and &lt;code&gt;useDispatch&lt;/code&gt; correctly. However ease of use is not always the most important think that we consider while choosing one approach over another.&lt;/p&gt;

&lt;p&gt;Redux is really powerful when it comes to managing application state. But with great power comes great responsibility. We should give importance to how state is structured and be picky about what should be included into it and what is not. Only the data that is needed to be long-lived and globally available should make into Redux state. From this perspective, ease of use becomes our enemy. As React's &lt;code&gt;useState&lt;/code&gt; hook and Redux's &lt;code&gt;useSelector&lt;/code&gt; hook offer similar API surfaces, developers tend to put most of the state to their Redux state instead of picking only necessary ones. In the long run, it becomes bloated and structured by what components need rather than data itself.&lt;/p&gt;

&lt;p&gt;Consider following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;SomeComponent&lt;/span&gt; &lt;span class="o"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dispatch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useDispatch&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Component content goes here */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&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;dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;openConfirmDialog&lt;/span&gt;&lt;span class="p"&gt;())}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;Me&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ConfirmDialog&lt;/span&gt; &lt;span class="o"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isOpen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useSelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isDialogOpen&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;isOpen&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Dialog content goes here */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt; : null&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In most cases, you don't have to put &lt;code&gt;isOpen&lt;/code&gt; into your global application state. Avoiding prop drilling is not an excuse.&lt;/p&gt;

&lt;p&gt;The problem here is not caused by &lt;code&gt;useSelector&lt;/code&gt; itself. However it makes making mistakes easier. On the other hand, if we were using &lt;code&gt;connect&lt;/code&gt; instead, we would think twice to put it into global state.&lt;/p&gt;

&lt;h2&gt;
  
  
  Maintainability
&lt;/h2&gt;

&lt;p&gt;As Software Developers, our primary job is maintenance of existing code, not writing new ones. As once Martin Fowler said,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1385573425360609281-573" src="https://platform.twitter.com/embed/Tweet.html?id=1385573425360609281"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1385573425360609281-573');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1385573425360609281&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;p&gt;Before React's introduction of hooks, we used to use presentational components and container components to make sure that business logic is separate from UI code. Hooks have changed that approach. We can now put business logic into custom hooks and use them in multiple components. However, the same is not correct anymore for UI code. Hooks are directly wired into them and it is not possible to use them by connecting to different data sources.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useSelector&lt;/code&gt; is a good example of such cases. I've seen this in many codebases: Components and their sub-components were so tightly coupled into Redux state and therefore developers tended to copy their UI code and create another component instead of using the already available ones. However the solution is simple: Use &lt;code&gt;connect&lt;/code&gt; to create a container component and let the presentational part independently available for future uses. It provides just a right amount of abstraction without much complexity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Loose coupling
&lt;/h2&gt;

&lt;p&gt;Another problem caused by using &lt;code&gt;useSelector&lt;/code&gt; arises while writing tests for your code. As the hooks are embedded directly into your component it is impossible to test them independent of the application state. Thus, even the simplest components are required to be connected to Redux.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;connect&lt;/code&gt; prevents this from happening as well. You can always test your presentational component independently. This allows us to write unit tests specific to that component without connecting it to Redux populated with mock data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;These differences might seem trivial at first glance. But it becomes more and more obvious as project grows. Therefore I suggest to use &lt;code&gt;connect&lt;/code&gt; instead of &lt;code&gt;useSelector&lt;/code&gt; on your projects as well. It will make things harder, yes, but sometimes in software development making something slightly harder is better to prevent immature decisions taken.&lt;/p&gt;

</description>
      <category>react</category>
      <category>redux</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Avoiding unintended undefined values while using TypeScript Record</title>
      <dc:creator>sarioglu</dc:creator>
      <pubDate>Sun, 28 Feb 2021 13:42:58 +0000</pubDate>
      <link>https://dev.to/sarioglu/avoiding-unintended-undefined-values-while-using-typescript-record-4igo</link>
      <guid>https://dev.to/sarioglu/avoiding-unintended-undefined-values-while-using-typescript-record-4igo</guid>
      <description>&lt;p&gt;If you are already familiar with TypeScript, you must know that it provides different ways to type objects. You can use a &lt;code&gt;type&lt;/code&gt; or an &lt;code&gt;interface&lt;/code&gt; to strongly type your objects:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;MyObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;MyObject&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&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;Sometimes we also use objects as key-value stores where we don't know their properties beforehand. In such cases &lt;code&gt;Record&lt;/code&gt; utility type provided by TypeScript comes in handy:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;K&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;P&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;T&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;There's a catch though. If you use &lt;code&gt;string&lt;/code&gt; as its key, TypeScript will assume that your object holds a value for every possible &lt;code&gt;string&lt;/code&gt;. Consider the following example for clarity:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Colors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Colors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;red&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#FF0000&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;green&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#00FF00&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#0000FF&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;red&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "#FF0000"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;yellow&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;


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

&lt;/div&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%2Fuploads%2Farticles%2Fy1m2kzil0cqp9zggjig9.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%2Fuploads%2Farticles%2Fy1m2kzil0cqp9zggjig9.png" alt="TypeScript treats value as string and does not detect undefined values"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, TypeScript gives no error for &lt;code&gt;colors.yellow&lt;/code&gt; and expects that it is a &lt;code&gt;string&lt;/code&gt;. However it is not. We don't have a value for &lt;code&gt;yellow&lt;/code&gt; in our object. This is especially dangerous if you try to reach to a property of value. In such case we may have &lt;code&gt;Uncaught TypeError&lt;/code&gt; exception and our application may crush.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;red&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// "#ff0000"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;yellow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Uncaught TypeError: can't access property "toLowerCase" of undefined&lt;/span&gt;


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Solution: Use &lt;code&gt;Partial&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;To avoid such cases, we can combine &lt;code&gt;Record&lt;/code&gt; with another utility type, &lt;code&gt;Partial&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Colors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Partial&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;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;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%2Fuploads%2Farticles%2Freikyom3kdikt36ojdgg.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%2Fuploads%2Farticles%2Freikyom3kdikt36ojdgg.png" alt="TypeScript can now warn us about undefined values"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Now we can use our &lt;code&gt;Colors&lt;/code&gt; type safely and let TypScript to warn us about possible type errors.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to store a function using React.useState</title>
      <dc:creator>sarioglu</dc:creator>
      <pubDate>Wed, 09 Sep 2020 19:06:50 +0000</pubDate>
      <link>https://dev.to/sarioglu/how-to-store-a-function-using-react-usestate-4ffh</link>
      <guid>https://dev.to/sarioglu/how-to-store-a-function-using-react-usestate-4ffh</guid>
      <description>&lt;p&gt;Sometimes things that have seen too easy to accomplish fail silently. Recently I have faced with such issue when I try to store a function in a React component to use it later. In this post, I have tried to explain my situation an its solution.&lt;/p&gt;

&lt;p&gt;I was refactoring an old class-based React component into a functional one. I have tried to use &lt;code&gt;useState&lt;/code&gt; hook as it usually happens. But it this case I was trying to store a function.&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setMyFunc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt; &lt;span class="o"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;someFunctionCall&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;setMyFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&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;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&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="nx"&gt;handleClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      A button
    &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;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Things seem alright at first glimpse. However when I run it, I found out that the callback function was invoked immediately.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why?
&lt;/h1&gt;

&lt;p&gt;The code seems straightforward enough. But it took a couple of minutes to realize my mistake. It is because update function of &lt;code&gt;useState&lt;/code&gt; accepts either a value or a function to return a value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;SetStateAction&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;S&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;S&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;S&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;S&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's why when I passed a function to &lt;code&gt;setMyFunc&lt;/code&gt;, React tried to get the return value of &lt;code&gt;callback&lt;/code&gt; function by passing &lt;code&gt;prevState&lt;/code&gt; to it as an argument.&lt;/p&gt;

&lt;h1&gt;
  
  
  Solution
&lt;/h1&gt;

&lt;p&gt;Once you realize the problem, it is easier to solve. All we need is an arrow function to wrap our actual &lt;code&gt;callback&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;setMyFunc&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By doing that, we give React a function to get our &lt;code&gt;callback&lt;/code&gt; function and set it properly.&lt;/p&gt;

&lt;p&gt;You can refer to &lt;a href="https://reactjs.org/docs/hooks-reference.html#functional-updates"&gt;React's Hooks API Reference&lt;/a&gt; to find more info about it.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>hooks</category>
      <category>usestate</category>
    </item>
    <item>
      <title>[Update] Using Svelte with Tailwindcss - A better approach</title>
      <dc:creator>sarioglu</dc:creator>
      <pubDate>Mon, 04 Nov 2019 18:10:00 +0000</pubDate>
      <link>https://dev.to/sarioglu/using-svelte-with-tailwindcss-a-better-approach-47ph</link>
      <guid>https://dev.to/sarioglu/using-svelte-with-tailwindcss-a-better-approach-47ph</guid>
      <description>&lt;p&gt;&lt;em&gt;Updated 2020/01/27&lt;/em&gt;, GitHub link for the Sapper template is added below 🎉&lt;/p&gt;

&lt;p&gt;I've been using Tailwind since its early days and it is a complete life changer for me. That's why I tried to use it on a project written using Svelte. Existing methods to combine these two weren't sufficient in terms of developer experience that they have provided. So, I've tried to come up with a better approach. Wish you enjoy reading!&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;p&gt;I've combined Svelte's preprocessing feature and PostCSS using &lt;code&gt;svelte-preprocess&lt;/code&gt; to handle Tailwind. You can skip the tutorial and use the template that I've published on GitHub: &lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i3JOwpme--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/github-logo-ba8488d21cd8ee1fee097b8410db9deaa41d0ca30b004c0c63de0a479114156f.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/sarioglu"&gt;
        sarioglu
      &lt;/a&gt; / &lt;a href="https://github.com/sarioglu/svelte-tailwindcss-template"&gt;
        svelte-tailwindcss-template
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Template for building basic applications with Svelte
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;p&gt;&lt;em&gt;Looking for a shareable component template? Go here --&amp;gt; &lt;a href="https://github.com/sveltejs/component-template"&gt;sveltejs/component-template&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
svelte-tailwindcss-template&lt;/h1&gt;
&lt;p&gt;This is a fork of Svelte's project template to enable usage of Tailwindcss. Refer to &lt;a href="https://github.com/sveltejs/template"&gt;https://github.com/sveltejs/template&lt;/a&gt; for more info.&lt;/p&gt;
&lt;p&gt;To create a new project based on this template using &lt;a href="https://github.com/Rich-Harris/degit"&gt;degit&lt;/a&gt;:&lt;/p&gt;
&lt;div class="highlight highlight-source-shell position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;npx degit sarioglu/svelte-tailwindcss-template svelte-app
&lt;span class="pl-c1"&gt;cd&lt;/span&gt; svelte-app&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;em&gt;Note that you will need to have &lt;a href="https://nodejs.org" rel="nofollow"&gt;Node.js&lt;/a&gt; installed.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
Get started&lt;/h2&gt;
&lt;p&gt;Install the dependencies...&lt;/p&gt;
&lt;div class="highlight highlight-source-shell position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;&lt;span class="pl-c1"&gt;cd&lt;/span&gt; svelte-app
npm install&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;...then start &lt;a href="https://rollupjs.org" rel="nofollow"&gt;Rollup&lt;/a&gt;:&lt;/p&gt;
&lt;div class="highlight highlight-source-shell position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;npm run dev&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Navigate to &lt;a href="http://localhost:5000" rel="nofollow"&gt;localhost:5000&lt;/a&gt;. You should see your app running. Edit a component file in &lt;code&gt;src&lt;/code&gt;, save it, and reload the page to see your changes.&lt;/p&gt;
&lt;p&gt;By default, the server will only respond to requests from localhost. To allow connections from other computers, edit the &lt;code&gt;sirv&lt;/code&gt; commands in package.json to include the option &lt;code&gt;--host 0.0.0.0&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;If you're using &lt;a href="https://code.visualstudio.com/" rel="nofollow"&gt;Visual Studio Code&lt;/a&gt; we recommend installing the official extension &lt;a href="https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode" rel="nofollow"&gt;Svelte for VS Code&lt;/a&gt;. If you are…&lt;/p&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/sarioglu/svelte-tailwindcss-template"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Existing methods
&lt;/h2&gt;

&lt;p&gt;There are several other works to integrate Tailwind into Svelte. You can even find a couple of examples under Tailwind's GitHub account.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/tailwindcss/setup-examples/tree/master/examples/svelte"&gt;setup-examples/examples/svelte at master · tailwindcss/setup-examples · GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/tailwindcss/setup-examples/tree/master/examples/sapper"&gt;setup-examples/examples/sapper at master · tailwindcss/setup-examples · GitHub&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, these methods have some structural weaknesses:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They create another pipeline alongside Svelte to process external css. Tailwind will be processed by PostCSS while component styles are being processed by Svelte. That's why developers need to reconsider everything from transpiling to minimization.&lt;/li&gt;
&lt;li&gt;They make it impossible to use directives of Tailwind (like &lt;code&gt;@apply&lt;/code&gt; or &lt;code&gt;@screen&lt;/code&gt;) in component styles.&lt;/li&gt;
&lt;li&gt;They create an auto-generated css file within the codebase.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's why I've come up with a new approach to make this integration smoother. So let's start with it:&lt;/p&gt;

&lt;h2&gt;
  
  
  1-Create a Svelte app
&lt;/h2&gt;

&lt;p&gt;First, we need to initialize a Svelte app using the following commands. If you already have an existing one, you can skip this section.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx degit sveltejs/template &lt;span class="o"&gt;[&lt;/span&gt;my-svelte-project]
&lt;span class="nb"&gt;cd&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;my-svelte-project]

npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;These commands clone the official Svelte app template and install required dependencies.&lt;/p&gt;
&lt;h2&gt;
  
  
  2-Initialize Tailwind
&lt;/h2&gt;

&lt;p&gt;Following the previous step, install required dependencies for Tailwind integration using the following command:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i &lt;span class="nt"&gt;-D&lt;/span&gt; @fullhuman/postcss-purgecss postcss postcss-load-config svelte-preprocess tailwindcss
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Then, run the following command to initialize Tailwind:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx tailwind init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This will create a file named &lt;code&gt;tailwind.config.js&lt;/code&gt; in your codebase. You can edit or replace this file to extend your Tailwind config.&lt;/p&gt;
&lt;h2&gt;
  
  
  3-Make the integration
&lt;/h2&gt;

&lt;p&gt;In order to make the integration we'll need following two files. We'll use &lt;code&gt;postcss.config.js&lt;/code&gt; to configure PostCSS to process styles with Tailwind. Note that PostCSS uses Purgecss to get rid of unused styles in production mode. We'll also need to whitelist css classes generated by Svelte itself since Svelte itself takes are of these.&lt;/p&gt;
&lt;h3&gt;
  
  
  postcss.config.js
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;purgecss&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@fullhuman/postcss-purgecss&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)({&lt;/span&gt;
  &lt;span class="na"&gt;content&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/**/*.html&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;./src/**/*.svelte&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;

  &lt;span class="na"&gt;whitelistPatterns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;/svelte-/&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;

  &lt;span class="na"&gt;defaultExtractor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;A-Za-z0-9-_:&lt;/span&gt;&lt;span class="se"&gt;/]&lt;/span&gt;&lt;span class="sr"&gt;+/g&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;production&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ROLLUP_WATCH&lt;/span&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="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tailwindcss&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="nx"&gt;production&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;purgecss&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;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;Tailwindcss.svelte&lt;/code&gt; file includes a Svelte component which only has a style definition. We'll use it to inject our utility classes into the app. &lt;code&gt;global&lt;/code&gt; directive here means that styles of this component will be available globally.&lt;/p&gt;
&lt;h3&gt;
  
  
  src/Tailwindcss.svelte
&lt;/h3&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;style &lt;/span&gt;&lt;span class="na"&gt;global&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&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;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We need to import this component into our app:&lt;/p&gt;
&lt;h3&gt;
  
  
  src/App.svelte
&lt;/h3&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;script&amp;gt;&lt;/span&gt;
  &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Tailwindcss&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Tailwindcss.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;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

...
&lt;span class="nt"&gt;&amp;lt;Tailwindcss&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;By doing that, we'll be able to use the classes provided by Tailwind in our app.&lt;/p&gt;

&lt;p&gt;Finally, we'll tweak the rollup config to use &lt;code&gt;svelte-preprocess&lt;/code&gt; to process components' styles.&lt;/p&gt;
&lt;h3&gt;
  
  
  rollup.config.js
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;sveltePreprocess&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;svelte-preprocess&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="nx"&gt;svelte&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
  &lt;span class="na"&gt;preprocess&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;sveltePreprocess&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;postcss&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="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;h2&gt;
  
  
  Results
&lt;/h2&gt;

&lt;p&gt;Using this new approach will enable us to benefit from every feature of Tailwind by combining Svelte's preprocessing ability and PostCSS. You can use utility classes, or call directives to combine them into component styles. All those styles will be processed by Svelte without creating additional pipeline.&lt;/p&gt;

&lt;p&gt;To demonstrate the outcome let's run the app using &lt;code&gt;npm run dev&lt;/code&gt; command and change some styles in &lt;code&gt;App.svelte&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;@apply&lt;/span&gt; &lt;span class="err"&gt;bg-black&lt;/span&gt; &lt;span class="err"&gt;text-white;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;You will see that styles provided by Tailwind are perfectly applied to our mighty &lt;code&gt;Hello world!&lt;/code&gt; message. So you can start using them for a better cause!&lt;/p&gt;
&lt;h2&gt;
  
  
  What about Sapper?
&lt;/h2&gt;

&lt;p&gt;Not a problem! You can apply the same steps to integrate Tailwind into your Sapper app. Just be sure that you've changed both client and server configs.&lt;/p&gt;

&lt;p&gt;I've published the Sapper template to GitHub. Since it is based on the official template, you can use either of rollup and webpack setups. Here is the link:&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i3JOwpme--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/github-logo-ba8488d21cd8ee1fee097b8410db9deaa41d0ca30b004c0c63de0a479114156f.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/sarioglu"&gt;
        sarioglu
      &lt;/a&gt; / &lt;a href="https://github.com/sarioglu/sapper-tailwindcss-template"&gt;
        sapper-tailwindcss-template
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Starter template for Sapper apps
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;h1&gt;
sapper-template&lt;/h1&gt;
&lt;p&gt;This is a fork of Sapper's project template to enable usage of Tailwindcss. Refer to &lt;a href="https://github.com/sveltejs/sapper"&gt;Sapper&lt;/a&gt; for more info.&lt;/p&gt;
&lt;h2&gt;
Getting started&lt;/h2&gt;
&lt;h3&gt;
Using &lt;code&gt;degit&lt;/code&gt;
&lt;/h3&gt;
&lt;p&gt;To create a new Sapper project based on Rollup locally, run&lt;/p&gt;
&lt;div class="highlight highlight-source-shell position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;npx degit &lt;span class="pl-s"&gt;&lt;span class="pl-pds"&gt;"&lt;/span&gt;sarioglu/sapper-tailwindcss-template#rollup&lt;span class="pl-pds"&gt;"&lt;/span&gt;&lt;/span&gt; my-app&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;For a webpack-based project, instead run&lt;/p&gt;
&lt;div class="highlight highlight-source-shell position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;npx degit &lt;span class="pl-s"&gt;&lt;span class="pl-pds"&gt;"&lt;/span&gt;sarioglu/sapper-tailwindcss-template#webpack&lt;span class="pl-pds"&gt;"&lt;/span&gt;&lt;/span&gt; my-app&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;a href="https://github.com/Rich-Harris/degit"&gt;&lt;code&gt;degit&lt;/code&gt;&lt;/a&gt; is a scaffolding tool that lets you create a directory from a branch in a repository.&lt;/p&gt;
&lt;p&gt;Replace &lt;code&gt;my-app&lt;/code&gt; with the path where you wish to create the project.&lt;/p&gt;
&lt;h3&gt;
Using GitHub templates&lt;/h3&gt;
&lt;p&gt;Alternatively, you can create the new project as a GitHub repository using GitHub's template feature.&lt;/p&gt;
&lt;p&gt;Go to either &lt;a href="https://github.com/sveltejs/sapper-template-rollup"&gt;sapper-template-rollup&lt;/a&gt; or &lt;a href="https://github.com/sveltejs/sapper-template-webpack"&gt;sapper-template-webpack&lt;/a&gt; and click on "Use this template" to create a new project repository initialized by the template.&lt;/p&gt;
&lt;h3&gt;
Running the project&lt;/h3&gt;
&lt;p&gt;Once you have created the project, install dependencies and run the project in development mode:&lt;/p&gt;
&lt;div class="highlight highlight-source-shell position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;&lt;span class="pl-c1"&gt;cd&lt;/span&gt; my-app
npm install &lt;span class="pl-c"&gt;&lt;span class="pl-c"&gt;#&lt;/span&gt; or yarn&lt;/span&gt;
npm run dev&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;…&lt;/p&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/sarioglu/sapper-tailwindcss-template"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;



&lt;h2&gt;
  
  
  Other benefits
&lt;/h2&gt;

&lt;p&gt;Using &lt;code&gt;svelte-preprocess&lt;/code&gt; to let PostCSS to handle component styles provides various other side benefits. You can use &lt;code&gt;postcss.config.js&lt;/code&gt; to import some other PostCSS plugins like &lt;code&gt;autoprefixer&lt;/code&gt;, etc. Those plugins will immediately take care of your component styles.&lt;/p&gt;

</description>
      <category>svelte</category>
      <category>sapper</category>
      <category>tailwindcss</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
