<?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: Hossein Rahimi</title>
    <description>The latest articles on Forem by Hossein Rahimi (@hrahimi270).</description>
    <link>https://forem.com/hrahimi270</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%2F287660%2F54cb6529-1fb2-4014-aa42-76d6629e220f.jpeg</url>
      <title>Forem: Hossein Rahimi</title>
      <link>https://forem.com/hrahimi270</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hrahimi270"/>
    <language>en</language>
    <item>
      <title>Extend usage of styled-components in React</title>
      <dc:creator>Hossein Rahimi</dc:creator>
      <pubDate>Fri, 27 Dec 2019 08:41:25 +0000</pubDate>
      <link>https://forem.com/hrahimi270/extend-usage-of-styled-components-in-react-2ff0</link>
      <guid>https://forem.com/hrahimi270/extend-usage-of-styled-components-in-react-2ff0</guid>
      <description>&lt;p&gt;If you are a React developer and you are using styled-components as a helpful tool, then this article wrote for you!&lt;/p&gt;

&lt;p&gt;As usual, you might create a styled-component and import it in other components then use it.&lt;br&gt;
But what if you want to change some styles of your component on different pages? Or different components?&lt;br&gt;
You can always use &lt;strong&gt;props&lt;/strong&gt; for styled-components to change some properties. Right? Like this picture.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bXI4BP4p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/8v329xtmwkvvtyh3hsfq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bXI4BP4p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/8v329xtmwkvvtyh3hsfq.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another way is that export &lt;code&gt;css&lt;/code&gt; from styled-components and use it in the body of the component.&lt;br&gt;
The &lt;code&gt;css&lt;/code&gt; function returns a string of passed styles. These styles are &lt;strong&gt;autoperfixed&lt;/strong&gt; for cross-browser styles.&lt;br&gt;
Like this picture.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aIYpwRVH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/wunwqrkjm5v3m0j8aveu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aIYpwRVH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/wunwqrkjm5v3m0j8aveu.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But these are not enough for me! I want more extendable components.&lt;br&gt;
I don’t know if you knew it, but the &lt;code&gt;styled&lt;/code&gt; accepts both &lt;strong&gt;object&lt;/strong&gt; and &lt;strong&gt;string&lt;/strong&gt; for styles.&lt;br&gt;
So this example should be okay. Right?&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OuNpvlJR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/mojigkgxeny3t0i9y8lv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OuNpvlJR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/mojigkgxeny3t0i9y8lv.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So we conclude there are more ways of extending styles of a styled-components!&lt;br&gt;
Now we can be creative and use it to extend our components and create components that can be styled in any way!&lt;/p&gt;

&lt;p&gt;For example, we can use a specific &lt;code&gt;prop&lt;/code&gt; for extending our components.&lt;br&gt;
And if you are using &lt;strong&gt;Typescript&lt;/strong&gt; in your projects, you can control the value of that prop.&lt;br&gt;
Like this picture.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wHIt-3u8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/het4mxnn1mgp9nyozajp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wHIt-3u8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/het4mxnn1mgp9nyozajp.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above picture, we specified a prop named &lt;strong&gt;styles&lt;/strong&gt;. Using &lt;code&gt;React.CSSProperties&lt;/code&gt; we can control the value to only valid CSS properties. That helps us to avoid crashing the app.&lt;/p&gt;

&lt;p&gt;I hope this article helps you create beautiful UI components using styled-components.&lt;br&gt;
I would be happy if you comment to me your opinion.&lt;/p&gt;

</description>
      <category>react</category>
      <category>styledcpmponents</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
