<?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: Ahmad Allehyany</title>
    <description>The latest articles on DEV Community by Ahmad Allehyany (@ahmadallehyany).</description>
    <link>https://dev.to/ahmadallehyany</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%2F609142%2F4df2106c-310f-4908-a940-f812d8d51e8a.png</url>
      <title>DEV Community: Ahmad Allehyany</title>
      <link>https://dev.to/ahmadallehyany</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahmadallehyany"/>
    <language>en</language>
    <item>
      <title>Why I like using Styled Components in React</title>
      <dc:creator>Ahmad Allehyany</dc:creator>
      <pubDate>Mon, 05 Apr 2021 22:15:15 +0000</pubDate>
      <link>https://dev.to/ahmadallehyany/why-i-like-using-styled-components-in-react-1cbh</link>
      <guid>https://dev.to/ahmadallehyany/why-i-like-using-styled-components-in-react-1cbh</guid>
      <description>&lt;p&gt;As I like to code my side projects in React JS, I went through many style libraries/frameworks to solve the issue of writing CSS for React Apps. The library I liked the most, out of all the ones I tried for CSS-in-JS for React, was &lt;a href="https://styled-components.com/"&gt;styled-components&lt;/a&gt;!&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Styled-Components?
&lt;/h3&gt;

&lt;p&gt;Out of all the libraries and frameworks I have tried, I have found &lt;a href="https://styled-components.com/"&gt;styled-components&lt;/a&gt; to require the least set-up, least learning curve and was the most pleasant to work with. It also makes use of ES6 features like template literals to make writing CSS-in-JS a good experience!&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting Up and Getting Started
&lt;/h3&gt;

&lt;p&gt;To get started with styled-components in your current React project, you have to install the package through npm like this:&lt;br&gt;
&lt;code&gt;npm i -S styled-components&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That's all the set up needed before you integrate it with your project! So far, this is my favorite part of using this library as I do not have to use a build or add another npm command to my &lt;code&gt;package.json&lt;/code&gt; file!&lt;/p&gt;

&lt;p&gt;So after this simple set up step, how do you use it in your project? Again, it's &lt;strong&gt;simple&lt;/strong&gt;! All you have to do is import the library and start writing the &lt;em&gt;styled components&lt;/em&gt; like so:&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;styled&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;styled-components&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;AwesomeButton&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="s2"&gt;`
    background-color: blue;
    border: 1px solid red;
    color: white;

    :hover {
      background-color: yellow;
    }

`&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Container&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;AwesomeButton&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;/AwesomeButton&amp;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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, you can use the styled component you made, &lt;code&gt;AwesomeButton&lt;/code&gt; as if it was a React component! &lt;br&gt;
Also, notice how simple the syntax is? It uses ES6 Tagged Template Literals for styling, and inside those you can write normal CSS as if it was in a CSS file! See that &lt;code&gt;:hover&lt;/code&gt; block inside the bracket? That's the same way you would write the hover in your normal CSS file if you wanted to nest styles!&lt;/p&gt;

&lt;p&gt;The power of styled components does not stop here! Since it uses template literals, that means we can use interpolation inside the backticks. Styled components makes use of this by providing us with the ability to access the props of the component inside the styles! So, if we wanted to provide slightly different styles for a danger and a success button, we could provide a prop to change the styles accordingly. So something like &lt;code&gt;&amp;lt;AwesomeButton danger&amp;gt;Stop!&amp;lt;/AwesomeButton&amp;gt;&lt;/code&gt; should ideally render a red button. To achieve that using styled components, we do it 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;const&lt;/span&gt; &lt;span class="nx"&gt;AwesomeButton&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="s2"&gt;`
    background-color: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;danger&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="nx"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;;
    border: 1px solid red;
    color: white;

    :hover {
      background-color: yellow;
    }

`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can interpolate a function in our styled components, and this function takes the component props as an argument. As you can see, we check for the danger property to change the background to red, otherwise it stays blue. &lt;/p&gt;

&lt;p&gt;Because of the minimum set up required, and not having to learn much but also getting access to powerful features, I really prefer using styled components for writing my React projects! Go check their &lt;a href="https://styled-components.com/docs"&gt;docs&lt;/a&gt; for more info as this article barely scratched the surface of what you can do with styled components!&lt;/p&gt;

&lt;p&gt;Thank you for reading this, and I hope I was able to help you see why styled components are fun!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>css</category>
    </item>
  </channel>
</rss>
