<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ getting started - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ getting started - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sat, 09 May 2026 16:25:42 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/getting-started/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ Convert the Gatsby default starter to use styled-components ]]>
                </title>
                <description>
                    <![CDATA[ By Scott Spence Let's go through getting styled-components working with the Gatsby default starter. https://www.youtube.com/watch?v=O5sWySCr668 In this example we're going to use the Gatsby default starter you get with CodeSandbox and add in styled-c... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/convert-the-gatsby-default-starter-to-use-styled-components-2018/</link>
                <guid isPermaLink="false">66d852209fbd5815f63ef038</guid>
                
                    <category>
                        <![CDATA[ Gatsby ]]>
                    </category>
                
                    <category>
                        <![CDATA[ getting started ]]>
                    </category>
                
                    <category>
                        <![CDATA[ styled-components ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Thu, 29 Nov 2018 18:06:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2019/06/cover-3.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Scott Spence</p>
<p>Let's go through getting styled-components working with the Gatsby default starter.</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/O5sWySCr668" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<p>In this example we're going to use the Gatsby default starter you get with <a target="_blank" href="https://codesandbox.io">CodeSandbox</a> and add in styled-components, so first up, open a <a target="_blank" href="https://codesandbox.io/s/">new CodeSandbox</a> and select Gatsby from the SERVER TEMPLATES.</p>
<h2 id="heading-1-dependencies">1. Dependencies</h2>
<p>In the dependencies section of the CodeSandbox editor you'll need to add the following:</p>
<pre><code class="lang-bash">gatsby-plugin-styled-components
styled-components
babel-plugin-styled-components
</code></pre>
<h2 id="heading-2-config">2. Config</h2>
<p>Now we need to change <code>gatsby-config.js</code> to incorporate the new dependencies we've just added. It doesn't have any configuration options so it can just go in as an extra line on the config, in this case I'm adding it after the <code>gatsby-plugin-sharp</code> plugin:</p>
<pre><code class="lang-js"><span class="hljs-string">'gatsby-transformer-sharp'</span>,
<span class="hljs-string">'gatsby-plugin-sharp'</span>,
<span class="hljs-string">'gatsby-plugin-styled-components'</span>,
</code></pre>
<p>Don't forget the comma at the end ?</p>
<h2 id="heading-3-global-style">3. Global Style</h2>
<p>Now that we're ready to rock n' roll with styled-components we need to remove the currently applied styled in the default starter and apply our own.</p>
<p>In the <code>src/components/layout.js</code> component there's an import for <code>layout.css</code> this is the CSS reset for the starter if we remove the import from here we'll see the styles for border and font be reset. We can now delete the <code>layout.css</code> file and create out own CSS reset using the <code>createGlobalStyle</code> function from styled-components.</p>
<p>Create a new folder <code>theme</code>, in this example it's in <code>src/theme</code> and add a <code>globalStyle.js</code> file in there.</p>
<p>This will export a <code>GlobalStyle</code> component for us to use throughout the app.</p>
<p>Let's add in a Google font and reset the <code>padding</code> and <code>margin</code>, for visual feedback we're going to add the font directly to the body element.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { createGlobalStyle } <span class="hljs-keyword">from</span> <span class="hljs-string">'styled-components'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> GlobalStyle = createGlobalStyle<span class="hljs-string">`
  @import url('https://fonts.googleapis.com/css?family=Kodchasan:400,700');
  body {
    padding: 0;
    margin: 0;
    font-family: Kodchasan;
  }
  a {
    text-decoration: none;
  }
  ul {
    margin: 0 auto;
    list-style-type: none;
  }
`</span>;
</code></pre>
<p>Ok, now we can use the export component here to apply styles globally in the app. So we need to have this as high up the component tree as possible, in this case that is in the <code>layout.js</code> component as it wraps all the pages in this project.</p>
<p>In <code>layout.js</code> import the <code>GlobalStyle</code> component.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">'./header'</span>;
<span class="hljs-keyword">import</span> { GlobalStyle } <span class="hljs-keyword">from</span> <span class="hljs-string">'../theme/globalStyle'</span>;
</code></pre>
<p>Then add it in with the other components being rendered.</p>
<pre><code class="lang-js">render={<span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> (
  &lt;&gt;
    &lt;GlobalStyle /&gt;
    &lt;Helmet
    ...
</code></pre>
<p>Ok! Global styled applied, we should now see the font change and the margin around the body of the page change.</p>
<p>Time to use styled-components!</p>
<h2 id="heading-4-use-styled-components">4. Use styled-components</h2>
<p>Now let's convert all the inline styles used in the starter to styled-components.</p>
<p>This is the actual styling part, which is moving the existing styles to styled components, working from top to bottom of the file structure, changing:</p>
<pre><code>src/components/header.js
src/components/layout.js
src/pages/index.js
</code></pre><p><strong>header.js</strong></p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> { Link } <span class="hljs-keyword">from</span> <span class="hljs-string">'gatsby'</span>;
<span class="hljs-keyword">import</span> styled <span class="hljs-keyword">from</span> <span class="hljs-string">'styled-components'</span>;

<span class="hljs-keyword">const</span> HeaderWrapper = styled.div<span class="hljs-string">`
  background: rebeccapurple;
  margin-bottom: 1.45rem;
`</span>;

<span class="hljs-keyword">const</span> Headline = styled.div<span class="hljs-string">`
  margin: 0 auto;
  max-width: 960;
  padding: 1.45rem 1.0875rem;
  h1 {
    margin: 0;
  }
`</span>;

<span class="hljs-keyword">const</span> StyledLink = styled(Link)<span class="hljs-string">`
  color: white;
  textdecoration: none;
`</span>;

<span class="hljs-keyword">const</span> Header = <span class="hljs-function">(<span class="hljs-params">{ siteTitle }</span>) =&gt;</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">HeaderWrapper</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Headline</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">StyledLink</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span>&gt;</span>{siteTitle}<span class="hljs-tag">&lt;/<span class="hljs-name">StyledLink</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Headline</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">HeaderWrapper</span>&gt;</span></span>
);

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Header;
</code></pre>
<p><strong>layout.js</strong></p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> PropTypes <span class="hljs-keyword">from</span> <span class="hljs-string">'prop-types'</span>;
<span class="hljs-keyword">import</span> Helmet <span class="hljs-keyword">from</span> <span class="hljs-string">'react-helmet'</span>;
<span class="hljs-keyword">import</span> { StaticQuery, graphql } <span class="hljs-keyword">from</span> <span class="hljs-string">'gatsby'</span>;

<span class="hljs-keyword">import</span> styled <span class="hljs-keyword">from</span> <span class="hljs-string">'styled-components'</span>;

<span class="hljs-keyword">import</span> Header <span class="hljs-keyword">from</span> <span class="hljs-string">'./header'</span>;
<span class="hljs-keyword">import</span> { GlobalStyle } <span class="hljs-keyword">from</span> <span class="hljs-string">'../theme/globalStyle'</span>;

<span class="hljs-keyword">const</span> ContentWrapper = styled.div<span class="hljs-string">`
  margin: 0 auto;
  maxwidth: 960;
  padding: 0px 1.0875rem 1.45rem;
  paddingtop: 0;
`</span>;

<span class="hljs-keyword">const</span> Layout = <span class="hljs-function">(<span class="hljs-params">{ children }</span>) =&gt;</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">StaticQuery</span>
    <span class="hljs-attr">query</span>=<span class="hljs-string">{graphql</span>`
      <span class="hljs-attr">query</span> <span class="hljs-attr">SiteTitleQuery</span> {
        <span class="hljs-attr">site</span> {
          <span class="hljs-attr">siteMetadata</span> {
            <span class="hljs-attr">title</span>
          }
        }
      }
    `}
    <span class="hljs-attr">render</span>=<span class="hljs-string">{data</span> =&gt;</span> (
      <span class="hljs-tag">&lt;&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">GlobalStyle</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Helmet</span> <span class="hljs-attr">title</span>=<span class="hljs-string">{data.site.siteMetadata.title}</span> <span class="hljs-attr">meta</span>=<span class="hljs-string">{[{</span> <span class="hljs-attr">name:</span> '<span class="hljs-attr">description</span>', <span class="hljs-attr">content:</span> '<span class="hljs-attr">Sample</span>' }, { <span class="hljs-attr">name:</span> '<span class="hljs-attr">keywords</span>', <span class="hljs-attr">content:</span> '<span class="hljs-attr">sample</span>, <span class="hljs-attr">something</span>' }]}&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">Helmet</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Header</span> <span class="hljs-attr">siteTitle</span>=<span class="hljs-string">{data.site.siteMetadata.title}</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">ContentWrapper</span>&gt;</span>{children}<span class="hljs-tag">&lt;/<span class="hljs-name">ContentWrapper</span>&gt;</span>
      <span class="hljs-tag">&lt;/&gt;</span></span>
    )}
  /&gt;
);

Layout.propTypes = {
  <span class="hljs-attr">children</span>: PropTypes.node.isRequired,
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Layout;
</code></pre>
<p><strong>index.js</strong></p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> { Link } <span class="hljs-keyword">from</span> <span class="hljs-string">'gatsby'</span>;
<span class="hljs-keyword">import</span> styled <span class="hljs-keyword">from</span> <span class="hljs-string">'styled-components'</span>;

<span class="hljs-keyword">import</span> Layout <span class="hljs-keyword">from</span> <span class="hljs-string">'../components/layout'</span>;
<span class="hljs-keyword">import</span> Image <span class="hljs-keyword">from</span> <span class="hljs-string">'../components/image'</span>;

<span class="hljs-keyword">const</span> ImgWrapper = styled.div<span class="hljs-string">`
  max-width: 300px;
  margin-bottom: 1.45rem;
`</span>;

<span class="hljs-keyword">const</span> IndexPage = <span class="hljs-function">() =&gt;</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Layout</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Hi people<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Welcome to your new Gatsby site.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Now go build something great.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">ImgWrapper</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Image</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">ImgWrapper</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/page-2/"</span>&gt;</span>Go to page 2<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Layout</span>&gt;</span></span>
);

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> IndexPage;
</code></pre>
<h2 id="heading-5-done">5. Done</h2>
<p><strong>Thanks for reading</strong></p>
<p>Here's the <a target="_blank" href="https://codesandbox.io/s/yp3z16yw11">example code</a> we worked on if you need reference. </p>
<blockquote>
<p><strong>You can read other articles like this on <a target="_blank" href="https://thelocalhost.blog/">my blog</a>.</strong></p>
</blockquote>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Using the React Context API  -  getting started ]]>
                </title>
                <description>
                    <![CDATA[ By Scott Spence Let's use the React Context API to change theme in an app! But first, some context! ? Ok terrible puns aside let's have a look at what the React Context API is for and what it does. There's a great one liner from the React docs... Co... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/using-the-react-context-api-getting-started-2018/</link>
                <guid isPermaLink="false">66d85236c1d58e16fbe2b41f</guid>
                
                    <category>
                        <![CDATA[ getting started ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React context ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sat, 08 Sep 2018 16:54:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2019/06/cover-1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Scott Spence</p>
<p>Let's use the React Context API to change theme in an app!</p>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/zmp2k4r128poj1gsws61.gif" alt="Image" width="1512" height="865" loading="lazy"></p>
<h2 id="heading-but-first-some-context">But first, some <strong>context</strong>! ?</h2>
<p>Ok terrible puns aside let's have a look at what the React Context API is for and what it does. There's a great one liner from the <a target="_blank" href="https://reactjs.org/docs/context.html">React docs</a>...</p>
<p>Context provides a way to pass data through the component tree without having to pass props down manually at every level.</p>
<p>Or in other words, you can use the React Context API to avoid <a target="_blank" href="https://blog.kentcdodds.com/prop-drilling-bb62e02cb691">prop drilling</a> if you need more detail on the concept then please do check out the links provided.</p>
<p>I've previously gone over implementing the React Context API in <a target="_blank" href="https://blog.scottspence.me">my Gatsby blog</a> which I documented as I did it; you can see <a target="_blank" href="https://blog.scottspence.me/react-context-api-with-gatsby">how that went here</a>.</p>
<h3 id="heading-explain-the-context-api-to-me">Explain the Context API to me.</h3>
<p>A great resource on explaining the API can be found from <a target="_blank" href="https://twitter.com/leighchalliday">@leighchalliday</a> with a <a target="_blank" href="https://www.youtube.com/watch?v=yzQ_XulhQFw">great usecase</a> on the subject.</p>
<h2 id="heading-what-were-doing">What we're doing...</h2>
<p>For this post we're going to extend the example we created for <a target="_blank" href="https://blog.scottspence.me/styled-components-getting-started">styled-components getting started</a> as it has the majority of the code we'll need to get started with the React Context API.</p>
<p>We're going to extend that example to manage the theme state of the example application.</p>
<p>So in summary:</p>
<ul>
<li>Scaffold out basic CreateReact App</li>
<li>Use styled-components ? for styling</li>
<li>Add themes to switch between with the React Context API</li>
<li>Use the React Context API!</li>
</ul>
<h2 id="heading-what-well-need">What we'll need...</h2>
<p>All we'll be needing is an internet connection and a modern web browser! Because we're going to do all of this online in the awesome <a target="_blank" href="https://codesandbox.io">CodeSandbox</a>!</p>
<p>If you have a GitHub account or not, CodeSandbox will let you get started <a target="_blank" href="https://codesandbox.io/s/new">coding straight away</a>!</p>
<h3 id="heading-versions">Versions:</h3>
<p><strong>This guide is being used with the following dependency versions.</strong></p>
<ul>
<li>react: 16.4.2</li>
<li>react-dom: 16.4.2</li>
<li>react-scripts: 1.1.4</li>
<li>styled-components: 3.4.5</li>
</ul>
<hr>
<h2 id="heading-lets-start">Let's start</h2>
<p>So let's go over theming the basic create react app again, this time instead of adding state into to the component we will use the React Context API to manage the state for us. There will be people that will argue that this is a bit overkill for a theme switch but it is given as an example of <a target="_blank" href="https://reactjs.org/docs/context.html#when-to-use-context">when to use the Context API</a> in the React documentation so I will let you decide on the validity of that point. For this example, I hope it will give you a clearer picture of how to use the Context API in an application.</p>
<h3 id="heading-dependencies">Dependencies</h3>
<p><a target="_blank" href="https://codesandbox.io/s/new">Open a React CodeSandbox</a> and add <code>styled-components</code> as a dependency:</p>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/d49drafvtvz3ws2br9vs.gif" alt="Image" width="1512" height="865" loading="lazy"></p>
<h3 id="heading-file-structure">File structure</h3>
<p>Another area for <a target="_blank" href="https://en.wiktionary.org/wiki/bikeshedding">bikeshedding</a> is file structure, in this scenario we're adding folders for <code>components</code>, <code>contexts</code> and the <code>theme</code> please feel free to structure your files how you see fit, this is how we're going to do it for this example ❤️</p>
<p>Add the directories into the <code>src</code> folder so we can add in some components, the file structure should look something like this:</p>
<pre><code class="lang-bash">context-demo/
├─ public/
├─ src/
│  └─ components/
│  └─ contexts/
│  └─ theme/
└─ package.json
</code></pre>
<h2 id="heading-scaffold-out-a-basic-create-react-app">Scaffold out a basic Create React App</h2>
<p>Ok, so, what we're going to do is add in an <code>App.js</code> component to the <code>components</code> folder then use that in the <code>src/index.js</code> file.</p>
<p>The <code>App.js</code> component can be a <a target="_blank" href="https://reactjs.org/docs/state-and-lifecycle.html#the-data-flows-down">stateless functional component</a> as for this example as we're going to be handling state with the Context API.</p>
<p>Here you can see my sketchy typing as I create the directories and add in the <code>App.js</code> component.</p>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/oyxpggt00q754iv1azp0.gif" alt="Image" width="1512" height="865" loading="lazy"></p>
<p>We can then remove the <code>style.css</code> file and reference in <code>src/index.js</code> as we're going to be styling with styled-components ? and then use our <code>App.js</code> component:</p>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/yyne3q36jc0zca2ld89u.gif" alt="Image" width="1512" height="865" loading="lazy"></p>
<p>Ok, so the reason why I have abstracted the <code>App.js</code> component out of the <code>src/index.js</code> file is so that when we come to using the Context API we can add it to the highest level in our app, which is <code>src/index.js</code>.</p>
<h3 id="heading-what-about-the-rest">What about the rest?</h3>
<p>So this isn't really the Create React App, as we're using CodeSandbox instead, I have gone over the basic styling used in the <a target="_blank" href="https://blog.scottspence.me/styled-components-getting-started">styled-components getting started</a> post so it's time to refer to that to mimic the styles we need.</p>
<p>That means what we're going to do, rather than go into depth on the styling of each of the component parts that make up the basic Create React App appearance, we're going to re-use components, so there's going to be a bit of copy pasting involved now.</p>
<p>The Create React App boilerplate code has one file that we go over styling in the <a target="_blank" href="https://blog.scottspence.me/styled-components-getting-started">styled-components getting started</a> post which is the <code>App.js</code> file, the others are left or deleted, the basic style of <code>App.js</code> is:</p>
<p><strong><code>App.css</code></strong></p>
<pre><code class="lang-css"><span class="hljs-selector-class">.App</span> {
  <span class="hljs-attribute">text-align</span>: center;
}

<span class="hljs-selector-class">.App-logo</span> {
  <span class="hljs-attribute">animation</span>: App-logo-spin infinite <span class="hljs-number">20s</span> linear;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">80px</span>;
}

<span class="hljs-selector-class">.App-header</span> {
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#222</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">150px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">color</span>: white;
}

<span class="hljs-selector-class">.App-title</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.5em</span>;
}

<span class="hljs-selector-class">.App-intro</span> {
  <span class="hljs-attribute">font-size</span>: large;
}

<span class="hljs-keyword">@keyframes</span> App-logo-spin {
  <span class="hljs-selector-tag">from</span> {
    <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">rotate</span>(<span class="hljs-number">0deg</span>);
  }
  <span class="hljs-selector-tag">to</span> {
    <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">rotate</span>(<span class="hljs-number">360deg</span>);
  }
}
</code></pre>
<h2 id="heading-use-styled-components-for-styling">Use styled components for styling</h2>
<p>Now we're going to recreate the styles from the <code>App.css</code> file with styled-components, let's list them out here and go through them:</p>
<pre><code class="lang-bash">AppWrapper
AppHeader
AppTitle
rotate360
AppLogo
<span class="hljs-comment"># We're adding our own styles for</span>
AppIntro
Underline
StyledHyperLink
Button
</code></pre>
<p><code>AppWrapper</code> is the top level wrapper which in a larger component could be used for layout with CSS Grid or Flexbox, in our case we're going to align the text center.</p>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/uc08zkkf4ay1hq8pkt3w.gif" alt="Image" width="1512" height="865" loading="lazy"></p>
<p>Straightforward enough, right? Now the majority of the rest of the components will use the styled-components <a target="_blank" href="https://www.styled-components.com/docs/advanced#theming"><code>ThemeProvider</code></a> which is what we're going to pass our theme to from the Context API.</p>
<h2 id="heading-add-themes-to-switch-between-with-the-react-context-api">Add themes to switch between with the React Context API</h2>
<p>Ok, we need to define some themes to pass to the <code>ThemeProvider</code>, we're going to define several theme aspects we want to change, these are going to be:</p>
<pre><code class="lang-js">primary; <span class="hljs-comment">// colour</span>
secondary; <span class="hljs-comment">// colour</span>
danger; <span class="hljs-comment">// colour</span>
fontHeader; <span class="hljs-comment">// font</span>
fontBody; <span class="hljs-comment">// font</span>
</code></pre>
<p>Create a file to contain the theme object in the <code>theme</code> directory and call it <code>globalStyle.js</code> and add in the following:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { injectGlobal } <span class="hljs-keyword">from</span> <span class="hljs-string">'styled-components'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> themes = {
  <span class="hljs-attr">theme1</span>: {
    <span class="hljs-attr">primary</span>: <span class="hljs-string">'#ff0198'</span>,
    <span class="hljs-attr">secondary</span>: <span class="hljs-string">'#01c1d6'</span>,
    <span class="hljs-attr">danger</span>: <span class="hljs-string">'#e50000'</span>,
    <span class="hljs-attr">fontHeader</span>: <span class="hljs-string">'Old Standard TT, sans, sans-serif'</span>,
    <span class="hljs-attr">fontBody</span>: <span class="hljs-string">'Nunito, sans-serif'</span>,
  },

  <span class="hljs-attr">theme2</span>: {
    <span class="hljs-attr">primary</span>: <span class="hljs-string">'#6e27c5'</span>,
    <span class="hljs-attr">secondary</span>: <span class="hljs-string">'#ffb617'</span>,
    <span class="hljs-attr">danger</span>: <span class="hljs-string">'#ff1919'</span>,
    <span class="hljs-attr">fontHeader</span>: <span class="hljs-string">'Enriqueta, sans-serif'</span>,
    <span class="hljs-attr">fontBody</span>: <span class="hljs-string">'Exo 2, sans, sans-serif'</span>,
  },

  <span class="hljs-attr">theme3</span>: {
    <span class="hljs-attr">primary</span>: <span class="hljs-string">'#f16623'</span>,
    <span class="hljs-attr">secondary</span>: <span class="hljs-string">'#2e2e86'</span>,
    <span class="hljs-attr">danger</span>: <span class="hljs-string">'#cc0000'</span>,
    <span class="hljs-attr">fontHeader</span>: <span class="hljs-string">'Kaushan Script, sans, sans-serif'</span>,
    <span class="hljs-attr">fontBody</span>: <span class="hljs-string">'Headland One, sans-serif'</span>,
  },
};

injectGlobal<span class="hljs-string">`
  @import url('
    https://fonts.googleapis.com/css?family=
    Old+Standard+TT:400,700|
    Nunito:400,700'|
    Enriqueta:400,700|
    Exo+2:400,700|
    Kaushan+Script:400,700|
    Headland+One:400,700|
  ');

  body {
    padding: 0;
    margin: 0;
  }
`</span>;
</code></pre>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/qnxbteccbaw92jbwsq9c.gif" alt="Image" width="1410" height="845" loading="lazy"></p>
<p>Ok, so nothing really happening there apart from setting up the styles for use later.</p>
<p>You will notice that <code>injectGlobal</code> is being used here, this is where we're setting the fonts for use throughout the app, <code>injectGlobal</code> <a target="_blank" href="https://stackoverflow.com/a/42899789/1138354">should be used once</a> in an app to set global styles like this.</p>
<p>Onwards! Let us now focus on getting the basic app styles into the <code>App.js</code> component. We can now start using the <code>ThemeProvider</code> in <code>App.js</code>. To do this, for now, to get some visual feedback we're going to apply one of the themes from the <code>themes</code> object in <code>globalStyle.js</code> this is so, as we are adding in components we can see the theme being applied.</p>
<p>We can do this now with the <code>AppHeader</code> which is a styled div:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> AppHeader = styled.div<span class="hljs-string">`
  height: 12rem;
  padding: 1rem;
  color: <span class="hljs-subst">${({ theme }</span>) =&gt; theme.dark};
  background-color: <span class="hljs-subst">${({ theme }</span>) =&gt; theme.primary};
`</span>;
</code></pre>
<p>You will notice here that we're beginning to use the styled-components, <code>theme</code> props but, if we paste this code in now there won't be any change until the <code>ThemeProvider</code> is passed the <code>theme</code> object so we're going to wrap <code>App.js</code> with the <code>ThemeProvider</code> component so that any component encapsulated by the <code>ThemeProvider</code> is able to receive <code>theme</code> props.</p>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/nuyaw29uoex6qcluf8va.gif" alt="Image" width="1410" height="845" loading="lazy"></p>
<p><code>AppTitle</code> is going to be a h1 so:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> AppTitle = styled.h1<span class="hljs-string">`
  font-family: <span class="hljs-subst">${({ theme }</span>) =&gt; theme.fontHeader};
`</span>;
</code></pre>
<p>For the spinning React logo we can use the asset used previously in the <a target="_blank" href="https://codesandbox.io/s/x26q7l9vyq">styled-components getting started example</a></p>
<p>We can add it in with the imports at the top of the <code>App.js</code> component and add it into the <code>AppLogo</code> styled component as an <code>img</code> tag:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> logo = <span class="hljs-string">`https://user-images.githubusercontent.com/
    234708/37256552-32635a02-2554-11e8-8fe3-8ab5bd969d8e.png`</span>;
</code></pre>
<p>The <code>keyframes</code> helper will need to be imported alongside the <code>ThemeProvider</code> for the animation on the react logo.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> rotate360 = keyframes<span class="hljs-string">`
  from { 
    transform: rotate(0deg); 
  }
  to { 
    transform: rotate(360deg); 
  }
`</span>;

<span class="hljs-keyword">const</span> AppLogo = styled.img<span class="hljs-string">`
  animation: <span class="hljs-subst">${rotate360}</span> infinite 5s linear;
  height: 80px;
  &amp;:hover {
    animation: <span class="hljs-subst">${rotate360}</span> infinite 1s linear;
  }
`</span>;
</code></pre>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/pxe3fb5zqvprvtjthq5b.gif" alt="Image" width="1410" height="845" loading="lazy"></p>
<h3 id="heading-shared-components">Shared components</h3>
<p>Shared components are covered in the <a target="_blank" href="https://blog.scottspence.me/styled-components-getting-started">styled-components getting started</a> guide if you need more information, for this example we're going to bring in the final couple of components as shared ones for the <code>StyledHyperLink</code> and <code>Button</code> in <code>src/Shared.js</code> add the following:</p>
<p><strong><code>src/Shared.js</code></strong></p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> styled, { css } <span class="hljs-keyword">from</span> <span class="hljs-string">'styled-components'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> Button = styled.button<span class="hljs-string">`
  padding: 0.5rem 1rem;
  margin: 0.5rem 1rem;
  color: <span class="hljs-subst">${({ theme }</span>) =&gt; theme.primary};
  font-size: 1rem;
  box-shadow: 0 3px 5px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  border: 2px solid <span class="hljs-subst">${props =&gt; props.border}</span>;
  background-color: Transparent;
  text-transform: uppercase;
  border-radius: 4px;
  transition: all 0.1s;
  &amp;:hover {
    transform: translateY(1px);
    box-shadow: 0 2px 3px rgba(0, 0, 0, 0.15);
  }
  <span class="hljs-subst">${props =&gt;
    props.primary &amp;&amp;
    css`<span class="css">
      <span class="hljs-selector-tag">background</span>: </span><span class="hljs-subst">${({ theme }</span><span class="css">) =&gt; <span class="hljs-selector-tag">theme</span><span class="hljs-selector-class">.primary</span>};
      <span class="hljs-selector-tag">border</span>: 2<span class="hljs-selector-tag">px</span> <span class="hljs-selector-tag">solid</span> </span><span class="hljs-subst">${({ theme }</span><span class="css">) =&gt; <span class="hljs-selector-tag">theme</span><span class="hljs-selector-class">.primary</span>};
      <span class="hljs-selector-tag">color</span>: <span class="hljs-selector-tag">white</span>;
    `</span>}</span>;
  <span class="hljs-subst">${props =&gt;
    props.danger &amp;&amp;
    css`<span class="css">
      <span class="hljs-selector-tag">background</span>: </span><span class="hljs-subst">${({ theme }</span><span class="css">) =&gt; <span class="hljs-selector-tag">theme</span><span class="hljs-selector-class">.danger</span>};
      <span class="hljs-selector-tag">border</span>: 2<span class="hljs-selector-tag">px</span> <span class="hljs-selector-tag">solid</span> </span><span class="hljs-subst">${({ theme }</span><span class="css">) =&gt; <span class="hljs-selector-tag">theme</span><span class="hljs-selector-class">.danger</span>};
      <span class="hljs-selector-tag">color</span>: <span class="hljs-selector-tag">white</span>;
    `</span>}</span>;
  &amp;:hover {
    transform: translateY(2px);
    box-shadow: 0 2px 3px rgba(0, 0, 0, 0.15);
  }
`</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> StyledHyperLink = styled.a<span class="hljs-string">`
  cursor: pointer;
  &amp;:visited,
  &amp;:active {
    color: <span class="hljs-subst">${({ theme }</span>) =&gt; theme.primary};
  }
  &amp;:hover {
    color: <span class="hljs-subst">${({ theme }</span>) =&gt; theme.secondary};
  }
  color: <span class="hljs-subst">${({ theme }</span>) =&gt; theme.primary};
`</span>;
</code></pre>
<p>Then import the components like any other:</p>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/ipi1kdmy83ieiw6sppog.gif" alt="Image" width="1410" height="845" loading="lazy"></p>
<p>The last three components for now, <code>AppIntro</code>, <code>Underline</code> and <code>StyledHyperLink</code>:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> AppIntro = styled.p<span class="hljs-string">`
  color: <span class="hljs-subst">${({ theme }</span>) =&gt; theme.dark};
  font-size: large;
  code {
    font-size: 1.3rem;
  }
  font-family: <span class="hljs-subst">${({ theme }</span>) =&gt; theme.fontBody};
`</span>;

<span class="hljs-keyword">const</span> Underline = styled.span<span class="hljs-string">`
  border-bottom: 4px solid <span class="hljs-subst">${({ theme }</span>) =&gt; theme.secondary};
`</span>;

<span class="hljs-keyword">const</span> StyledHyperLink = SHL.extend<span class="hljs-string">`
  text-decoration: none;
  font-family: <span class="hljs-subst">${({ theme }</span>) =&gt; theme.fontBody};
  color: <span class="hljs-subst">${({ theme }</span>) =&gt; theme.fontDark};
`</span>;
</code></pre>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/smm6hpg2w71sxm6nf3ln.gif" alt="Image" width="1410" height="845" loading="lazy"></p>
<p>Add them in under the <code>AppLogo</code> styled component and then we can add the rest of the components into the <code>App</code> function <code>return</code>, so, ready for another copy pasta? Here:</p>
<pre><code class="lang-js">&lt;AppIntro&gt;
  Bootstrapped <span class="hljs-keyword">with</span>{<span class="hljs-string">' '</span>}
  &lt;Underline&gt;
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">StyledHyperLink</span>
        <span class="hljs-attr">href</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">https:</span>//<span class="hljs-attr">github.com</span>/<span class="hljs-attr">facebook</span>/<span class="hljs-attr">create-react-app</span>`}
        <span class="hljs-attr">target</span>=<span class="hljs-string">"_blank"</span>
        <span class="hljs-attr">rel</span>=<span class="hljs-string">"noopener"</span>
      &gt;</span>
        create-react-app
      <span class="hljs-tag">&lt;/<span class="hljs-name">StyledHyperLink</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span></span>
  &lt;/Underline&gt;.
&lt;/AppIntro&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">AppIntro</span>&gt;</span>
  Components styled with{' '}
  <span class="hljs-tag">&lt;<span class="hljs-name">Underline</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">StyledHyperLink</span>
        <span class="hljs-attr">href</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">https:</span>//<span class="hljs-attr">www.styled-components.com</span>`}
        <span class="hljs-attr">target</span>=<span class="hljs-string">"_blank"</span>
        <span class="hljs-attr">rel</span>=<span class="hljs-string">"noopener"</span>
      &gt;</span>
        styled-components
      <span class="hljs-tag">&lt;/<span class="hljs-name">StyledHyperLink</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Underline</span>&gt;</span>{' '}
  <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">role</span>=<span class="hljs-string">"img"</span> <span class="hljs-attr">aria-label</span>=<span class="hljs-string">"nail polish"</span>&gt;</span>
    ?
  <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">AppIntro</span>&gt;</span></span>
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">AppIntro</span>&gt;</span>
  Fonts picked with{' '}
  <span class="hljs-tag">&lt;<span class="hljs-name">Underline</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">StyledHyperLink</span>
        <span class="hljs-attr">href</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">https:</span>//<span class="hljs-attr">fontjoy.com</span>/`}
        <span class="hljs-attr">target</span>=<span class="hljs-string">"_blank"</span>
        <span class="hljs-attr">rel</span>=<span class="hljs-string">"noopener"</span>
      &gt;</span>
        fontjoy.com
      <span class="hljs-tag">&lt;/<span class="hljs-name">StyledHyperLink</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Underline</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">AppIntro</span>&gt;</span></span>
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Button</span>&gt;</span>Normal Button<span class="hljs-tag">&lt;/<span class="hljs-name">Button</span>&gt;</span></span>
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Button</span> <span class="hljs-attr">primary</span>&gt;</span>Primary Button<span class="hljs-tag">&lt;/<span class="hljs-name">Button</span>&gt;</span></span>
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Button</span> <span class="hljs-attr">danger</span>&gt;</span>Danger Button<span class="hljs-tag">&lt;/<span class="hljs-name">Button</span>&gt;</span></span>
</code></pre>
<p>Sorry for the code wall! Right paste that in under the closing <code>&lt;/AppHeader&gt;</code> tag and we should have the base of what we're going to theme!</p>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/zfcnihvmyvb9my5dn11x.gif" alt="Image" width="1410" height="845" loading="lazy"></p>
<p>Ok? How's it looking?</p>
<p>Now we have a basic React app that uses styled-components!</p>
<h2 id="heading-use-the-react-context-api">Use the React Context API</h2>
<p>Now for the main event! Here we're going to cover:</p>
<p>Making the theme context.</p>
<p>Using the context API with a component.</p>
<p>Consuming the Context API in multiple components.</p>
<p>So, passing state needlessly through components is what we can use the Context API to avoid. If we take a look at the <a target="_blank" href="https://codesandbox.io/s/x26q7l9vyq">styled-components getting started example</a> we can see the state being managed in the <code>App.js</code> component and the <code>handleThemeChange</code> function has to be passed to the <code>ThemeSelect</code> component much the same way as any props would need to be passed down. That is a simplified example but it's quite easy to imagine if that component lived on a footer component or a menu item there would be several other components that would need to have the state passed through them that would not actually need that state or props. Make sense?</p>
<p><strong>example</strong></p>
<pre><code class="lang-js">&lt;App&gt;               {<span class="hljs-comment">/* state begins here */</span>}
  &lt;Header&gt;          {<span class="hljs-comment">/* through here */</span>}
    &lt;Navigation&gt;    {<span class="hljs-comment">/* and here */</span>}
      &lt;ThemeSelect&gt; {<span class="hljs-comment">/* to be used here */</span>}
    &lt;/Navigation&gt;
  &lt;/Header&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Footer</span>/&gt;</span></span>
&lt;/App&gt;
</code></pre>
<h3 id="heading-add-the-site-theme-context">Add the site theme context</h3>
<p>In our <code>src/contexts/</code> directory we're going to make our <code>SiteThemeContext.js</code>, import React and define and export our context:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> SiteThemeContext = React.createContext();
</code></pre>
<h3 id="heading-so-what-is-a-context">So what is a context?</h3>
<p>A context is made up of two things, a provider and a consumer, you have a single provider which will sit up as high as possible in the component tree so that multiple consumers can get the state and props from the provider.</p>
<p>Hopefully you recall the point at which we abstracted the <code>function App</code> component out of the <code>src/index.js</code> file, this is so we could add in the context provider at the highest level of the app, in the <code>src/index.js</code> file. This means that any consumer within the app, no matter how deep into the component tree it is, it can get the state and props from that top level.</p>
<p>Now to create a provider, the provider is a regular React component, so:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> SiteThemeContext = React.createContext()

<span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SiteThemeProvider</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">SiteThemeContext.Provider</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{}</span>&gt;</span>
        {this.props.children}
      <span class="hljs-tag">&lt;/<span class="hljs-name">SiteThemeContext.Provider</span>&gt;</span></span>
    )
  }
}
</code></pre>
<p>What is being returned by <code>&lt;SiteThemeProvider&gt;</code> is the <code>&lt;SiteThemeContext.Provider&gt;</code> and the children of that component, the one prop you have to provide the the provider is a <code>value</code> prop. This is the variable that the consumer has access to. The consumer being <code>&lt;SiteThemeContext.Consumer&gt;</code> (more on this shortly).</p>
<p>So what we can do now is have what is passed into <code>value</code> be an object <code>value={{}}</code> so it can store multiple properties of the state and the functions that are defined in <code>SiteThemeContext</code>.</p>
<p>The state for the context needs to be the <code>theme</code> so we need to import the theme from <code>src/theme/globalStyle</code> and add that to the state, we're going to default the theme (and state) to <code>theme1</code> and add a copy of that into the <code>value</code> prop by spreading into state <code>...❤️</code>, it should look like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> PropTypes <span class="hljs-keyword">from</span> <span class="hljs-string">'prop-types'</span>;

<span class="hljs-keyword">import</span> { themes } <span class="hljs-keyword">from</span> <span class="hljs-string">'../theme/globalStyle'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> SiteThemeContext = React.createContext();

<span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SiteThemeProvider</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  state = {
    <span class="hljs-attr">theme</span>: themes[<span class="hljs-string">'theme1'</span>],
  };

  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">SiteThemeContext.Provider</span>
        <span class="hljs-attr">value</span>=<span class="hljs-string">{{</span>
          <span class="hljs-attr">...this.state</span>,
        }}&gt;</span>
        {this.props.children}
      <span class="hljs-tag">&lt;/<span class="hljs-name">SiteThemeContext.Provider</span>&gt;</span></span>
    );
  }
}
</code></pre>
<p>Ok, it's been a while since I've added a gif, time for another one!</p>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/n2qbxs7cbf7w5opqcri2.gif" alt="Image" width="1410" height="845" loading="lazy"></p>
<p>And bring in the <code>themes</code> and add state:</p>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/y6n32p1gshah5ex747mu.gif" alt="Image" width="1410" height="845" loading="lazy"></p>
<p>Now we can add in a function to the provider to change the theme state based on what has been selected via the <code>handleThemeChange</code> event value:</p>
<pre><code class="lang-js">handleThemeChange = <span class="hljs-function"><span class="hljs-params">e</span> =&gt;</span> {
  <span class="hljs-keyword">const</span> key = e.target.value;
  <span class="hljs-keyword">const</span> theme = themes[key];
  <span class="hljs-built_in">this</span>.setState({ theme });
};
</code></pre>
<p>This can then be consumed by any provider that wants to use it, we're going to need to add it into the <code>value</code> prop, like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> PropTypes <span class="hljs-keyword">from</span> <span class="hljs-string">'prop-types'</span>;

<span class="hljs-keyword">import</span> { themes } <span class="hljs-keyword">from</span> <span class="hljs-string">'../theme/globalStyle'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> SiteThemeContext = React.createContext();

<span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SiteThemeProvider</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  state = {
    <span class="hljs-attr">theme</span>: themes[<span class="hljs-string">'theme1'</span>],
  };

  handleThemeChange = <span class="hljs-function"><span class="hljs-params">e</span> =&gt;</span> {
    <span class="hljs-keyword">const</span> key = e.target.value;
    <span class="hljs-keyword">const</span> theme = themes[key];
    <span class="hljs-built_in">this</span>.setState({ theme });
  };

  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">SiteThemeContext.Provider</span>
        <span class="hljs-attr">value</span>=<span class="hljs-string">{{</span>
          <span class="hljs-attr">...this.state</span>,
          <span class="hljs-attr">handleThemeChange:</span> <span class="hljs-attr">this.handleThemeChange</span>,
        }}&gt;</span>
        {this.props.children}
      <span class="hljs-tag">&lt;/<span class="hljs-name">SiteThemeContext.Provider</span>&gt;</span></span>
    );
  }
}
</code></pre>
<p>Ok, that is the site theme context component covered, pretty straight forward, right?</p>
<p>What I should mention is that the <code>e</code> in the <code>handleThemeChange</code> function is going to be the event from the theme select box that we're about to make.</p>
<p>Let's go through adding in the function and adding that to the state:</p>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/3bh3bwi4ekb24uowvm65.gif" alt="Image" width="1410" height="845" loading="lazy"></p>
<p>And now we can add the theme provider to <code>src/index.js</code> so anything lower in the dependency tree can access it via a consumer.</p>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/p8nibx8ecfildi92jscm.gif" alt="Image" width="1512" height="865" loading="lazy"></p>
<h3 id="heading-add-the-theme-select">Add the theme select</h3>
<p>Now we want a want to call the <code>handleThemeChange</code> function that is part of the <code>SiteThemeProvider</code> via the <code>SiteThemeContext</code>! I'm sure this all making perfect sense right now (?) so let's get right in there and define the component that we're going to use to consume the <code>SiteThemeContext.Provider</code> with a <code>ThemeSelect</code> component!</p>
<p>In the <code>src/components</code> directory add a new <code>ThemeSelect.js</code> component, this is where we are going to consume the site theme context with a consumer.</p>
<p>The child of a consumer isn't a component it's a function, so what we're going to need to do is have the theme select inside the return of that function.</p>
<p>Let's first set up the styled-components that will make up the select, which is a select box, some options and a wrapper.</p>
<p>First we'll do it without the consumer then we'll add it in.</p>
<p><strong><code>ThemeSelect.js</code></strong></p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> styled <span class="hljs-keyword">from</span> <span class="hljs-string">'styled-components'</span>;

<span class="hljs-keyword">import</span> { themes } <span class="hljs-keyword">from</span> <span class="hljs-string">'../theme/globalStyle'</span>;

<span class="hljs-keyword">const</span> SelectWrapper = styled.div<span class="hljs-string">`
  margin: 0rem 0.5rem 0rem 0.25rem;
  padding: 0rem 0.5rem 0rem 0.25rem;
`</span>;

<span class="hljs-keyword">const</span> Select = styled.select<span class="hljs-string">`
  margin: 1.5rem 0.5rem;
  padding: 0.25rem 0.5rem;
  font-family: <span class="hljs-subst">${({ theme }</span>) =&gt; theme.fontBody};
  border: 2px solid <span class="hljs-subst">${({ theme }</span>) =&gt; theme.secondary};
  box-shadow: 0px 0px 0px 1px rgba(0, 0, 0, 0.1);
  background: <span class="hljs-subst">${({ theme }</span>) =&gt; theme.foreground};
  border-radius: 4px;
`</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> SelectOpt = styled.option<span class="hljs-string">`
  font-family: <span class="hljs-subst">${({ theme }</span>) =&gt; theme.fontBody};
`</span>;

<span class="hljs-keyword">const</span> ThemeSelect = <span class="hljs-function"><span class="hljs-params">props</span> =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">SelectWrapper</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Select</span>&gt;</span>
        {Object.keys(themes).map((theme, index) =&gt; {
          return (
            <span class="hljs-tag">&lt;<span class="hljs-name">SelectOpt</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{index}</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{theme}</span>&gt;</span>
              Theme {index + 1}
            <span class="hljs-tag">&lt;/<span class="hljs-name">SelectOpt</span>&gt;</span>
          );
        })}
      <span class="hljs-tag">&lt;/<span class="hljs-name">Select</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">SelectWrapper</span>&gt;</span></span>
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> ThemeSelect;
</code></pre>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/43e15llsi8uhlmi1z1ut.gif" alt="Image" width="1512" height="865" loading="lazy"></p>
<p>So from this we can list the this themes available to us in the <code>themes</code> object. But that's it, the function to handle the theme change lives on the <code>SiteThemeProvider</code>.</p>
<p>Back to the <code>SiteThemeContext.Consumer</code> as I mentioned earlier the child of a consumer is a function <code>() =&gt; ()</code> the first section is the <code>value</code> from the provider (<code>&lt;SiteThemeContext.Provider&gt;</code>) so let's take a quick look at what we've previously defined in the provider:</p>
<pre><code class="lang-js">value={{
  ...this.state,
  <span class="hljs-attr">handleThemeChange</span>: <span class="hljs-built_in">this</span>.handleThemeChange
}}
</code></pre>
<p>Available from <code>SiteThemeContext.Provider</code> is the state and a function so any of those items we can extract and pass to the provider, or to put it another way the consumer can access those values.</p>
<p>Here we can use destructuring to pull the <code>handleThemeChange</code> function we need to change the theme.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>

<span class="hljs-keyword">import</span> { SiteThemeContext } <span class="hljs-keyword">from</span> <span class="hljs-string">'../contexts/SiteThemeContext'</span>

<span class="hljs-keyword">const</span> ThemeSelect = <span class="hljs-function"><span class="hljs-params">props</span> =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">SiteThemeContext.Consumer</span>&gt;</span>
      {({ handleThemeChange }) =&gt; ()}
    <span class="hljs-tag">&lt;/<span class="hljs-name">SiteThemeContext.Consumer</span>&gt;</span></span>
  )
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> ThemeSelect
</code></pre>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/1qq4hc2zqa50t0t2vi5v.gif" alt="Image" width="1512" height="865" loading="lazy"></p>
<p>Currently this isn't going to change the theme because we have that hardcoded into the styled-components <code>ThemeProvider</code>, what we want to do is use a consumer for the currently selected theme in the <code>SiteThemeContext</code>.</p>
<p>Before that we'll also need to add in the <code>onChange</code> event we want to use to pass the event (<code>e</code>) to the <code>handleThemeChange</code> function on <code>SiteThemeContext</code>.</p>
<p>Then in the <code>App</code> component we can import our <code>&lt;SiteThemeContext.Consumer&gt;</code> to consume the <code>theme</code> on the <code>SiteThemeContext</code> state and pass that to the styled-components <code>ThemeProvider</code>.</p>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/jn5u8bzuvufpa56c9ta7.gif" alt="Image" width="1512" height="865" loading="lazy"></p>
<h3 id="heading-want-to-know-more">Want to know more?</h3>
<p>As mentioned at the start of this article a great resource is <a target="_blank" href="https://twitter.com/leighchalliday">@leighchalliday</a> and <a target="_blank" href="https://www.youtube.com/channel/UCWPY8W-FAZ2HdDiJp2RC_sQ">his YouTube channel</a> where you can find his <a target="_blank" href="https://www.youtube.com/watch?v=yzQ_XulhQFw">great usecase</a> for the React Context API.</p>
<p>There's also the <a target="_blank" href="https://spectrum.chat/react">React community on spectrum</a> and <a target="_blank" href="https://spectrum.chat/styled-components">styled-components on spectrum</a>.</p>
<p><a target="_blank" href="https://codesandbox.io/s/5vl16n5oxp">Example code</a> of the walkthrough is available on <a target="_blank" href="https://codesandbox.io">CodeSandbox</a>.</p>
<h3 id="heading-thanks-for-reading">Thanks for reading</h3>
<p>If there is anything I have missed, or if there is a better way to do something then please let me know.</p>
<p>Follow me on <a target="_blank" href="https://twitter.com/spences10">Twitter</a> or <a target="_blank" href="https://github.com/spences10/ama">Ask Me Anything</a> on GitHub.</p>
<blockquote>
<p><strong>You can read other articles like this on <a target="_blank" href="https://thelocalhost.blog/">my blog</a>.</strong></p>
</blockquote>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ styled-components ? getting started ]]>
                </title>
                <description>
                    <![CDATA[ By Scott Spence We're going to style the basic create react app with styled-components to look something like this: But first, preamble✨: I have always struggled with styling sites, it seems to be an aspect of starting web development that is either... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/styled-components-getting-started-2018/</link>
                <guid isPermaLink="false">66d852329fbd5815f63ef03e</guid>
                
                    <category>
                        <![CDATA[ getting started ]]>
                    </category>
                
                    <category>
                        <![CDATA[ styled-components ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 03 Apr 2018 16:44:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2019/06/ezgif.com-gif-maker.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Scott Spence</p>
<p>We're going to style the basic create react app with styled-components to look something like this:</p>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/5dwv10zpqa13wb4pr47l.gif" alt="Image" width="730" height="672" loading="lazy"></p>
<p>But first, preamble✨: I have always struggled with styling sites, it seems to be an aspect of starting web development that is either an afterthought or glossed over. Up until December last year I didn't really like styling anything at all with CSS, it was a chore rather than something I enjoyed doing.</p>
<p>This was until I started using styled-components, when I joined a build to learn project for a <a target="_blank" href="https://medium.com/chingu">Chingu</a> voyage (<a target="_blank" href="https://github.com/chingu-voyage3/grad.then/"><code>grad.then()</code></a> if you're interested) we decided to use a CSS-in-JS package, <a target="_blank" href="https://twitter.com/mar_biletska">Marina</a> who was on my team was such an inspiration for me watching how components were styled and really gave me the confidence to start using styled-components.</p>
<h6 id="heading-me-with-css-before">me with css before</h6>
<p><img src="https://media.giphy.com/media/2rj8VysAig8QE/giphy.gif" alt="Image" width="557" height="413" loading="lazy"></p>
<p>I want to share what I have learned so far by going through styling a basic react application.</p>
<p>There's some basic CSS concepts in this post that I was not aware of before starting out with styled-components that I presume are assumed in styling web pages.</p>
<p>Styling the body element of a site is assumed, so for when you are starting out with a blank canvas there are some defaults for all modern web browsers you add to your site, like leaving font size at 16px (or 1rem) or <code>box-sizing:</code> <code>border-box;</code> there's some packages out there to take care of this for you as well.</p>
<h3 id="heading-install-styled-components">Install styled-components</h3>
<p>Ok let's bootstrap the basic react application you get when using <a target="_blank" href="https://github.com/facebook/create-react-app#create-react-app-">Create React App</a> with <a target="_blank" href="https://medium.com/@maybekatz/introducing-npx-an-npm-package-runner-55f7d4bd282b"><code>npx</code></a>, if you already have Create React App installed globally then you can use the command without <code>npx</code>.</p>
<pre><code class="lang-bash">npx create-react-app style-with-styled-components
<span class="hljs-built_in">cd</span> style-with-styled-components/
npm i styled-components
</code></pre>
<p>Ok, now we have the basic app we can style, thankfully <a target="_blank" href="https://github.com/gaearon">Dan</a> has kindly provided the starting styles for us so let's begin my using them with styled-components.</p>
<p>The way the CRA CSS is laid out, assumes that you will have a corresponding CSS file for each component, which can help with maintaining the CSS and lends to the React idea of having all your files separated into their component parts.</p>
<p>We can start with the <code>App.js</code> file and it's accompanying <code>App.css</code> file. Let's take a look at the <code>App.js</code> first:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> logo <span class="hljs-keyword">from</span> <span class="hljs-string">'./logo.svg'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">header</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App-header"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{logo}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App-logo"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"logo"</span> /&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App-title"</span>&gt;</span>Welcome to React<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App-intro"</span>&gt;</span>
          To get started, edit <span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>src/App.js<span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span> and save to reload.
        <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<p>In styled-components we'd create components for each of these elements that replace the aforementioned <code>className</code>'s. Ok we can start by migrating our styles into components, let's do one component first to get an idea of where we're going with this.</p>
<p>First, import <code>styled</code> into the <code>App.js</code> module:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> styled <span class="hljs-keyword">from</span> <span class="hljs-string">'styled-components'</span>;
</code></pre>
<p>Now let's look at <code>&lt;div className="App"&gt;</code>, it's the top level div for this component and is what I like to call the wrapper for the component. So let's give it an imaginative name AppWrapper.</p>
<p>Referring to the App.css there is <code>text-align: center;</code> which belongs to this, so:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> AppWrapper = styled.div<span class="hljs-string">`
  text-align: center;
`</span>;
</code></pre>
<p>So here we have defined the <code>AppWrapper</code> const as a <code>styled.div</code> followed by back ticks inside of the back ticks we can write any regular CSS with the exact same CSS syntax you wold in a normal <code>.css</code> file.</p>
<p>Now that we have our <code>AppWrapper</code> we can replace the top level div on the <code>App.js</code> component.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> styled <span class="hljs-keyword">from</span> <span class="hljs-string">'styled-components'</span>;
<span class="hljs-keyword">import</span> logo <span class="hljs-keyword">from</span> <span class="hljs-string">'./logo.svg'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">AppWrapper</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">header</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App-header"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{logo}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App-logo"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"logo"</span> /&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App-title"</span>&gt;</span>Welcome to React<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App-intro"</span>&gt;</span>
          To get started, edit <span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>src/App.js<span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span> and save to reload.
        <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">AppWrapper</span>&gt;</span></span>
    );
  }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<h3 id="heading-styled-components-all-the-things">styled-components all the things</h3>
<p>So let's do that for the remaining four CSS classes, and take a look, I'll define them underneath the <code>AppWrapper</code> here:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> rotate360 = keyframes<span class="hljs-string">`
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
`</span>;
<span class="hljs-keyword">const</span> AppLogo = styled.img<span class="hljs-string">`
  animation: <span class="hljs-subst">${rotate360}</span> infinite 120s linear;
  height: 80px;
`</span>;
<span class="hljs-keyword">const</span> AppHeader = styled.div<span class="hljs-string">`
  background-color: #222;
  height: 150px;
  padding: 20px;
  color: white;
`</span>;
<span class="hljs-keyword">const</span> AppTitle = styled.h1<span class="hljs-string">`
  font-size: 1.3em;
`</span>;
<span class="hljs-keyword">const</span> AppIntro = styled.p<span class="hljs-string">`
  font-size: large;
`</span>;
</code></pre>
<p>So first off we've created a variable for the React svg <a target="_blank" href="https://www.styled-components.com/docs/basics#animations">animation</a>, you'll need to import the <code>keyframes</code> helper from styled-components like so:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> styled, { keyframes } <span class="hljs-keyword">from</span> <span class="hljs-string">'styled-components'</span>;
</code></pre>
<p>this can now be used throughout the <code>App.js</code> component and we can add an on <code>hover</code> selector to any of our styled-components within this module. Here we're going to add it to the <code>AppLogo</code> to keep the super sweet rotating React logo.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> AppLogo = styled.img<span class="hljs-string">`
  animation: <span class="hljs-subst">${rotate360}</span> infinite 120s linear;
  height: 80px;
  &amp;:hover {
    animation: <span class="hljs-subst">${rotate360}</span> infinite 1.5s linear;
  }
`</span>;
</code></pre>
<p>Ok, our app shouldn't look any different as we haven't added in our styled-components to the app <code>render()</code> method, so let's do that now.</p>
<p>Let's also change the intro text. You can add a wrapper for the <code>&lt;code&gt;</code> tags something like:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> CodeWrapper = styled.code<span class="hljs-string">`
  font-size: 1.3rem;
`</span>;
</code></pre>
<p>But if you prefer you can nest selectors within the component, like:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> AppIntro = styled.p<span class="hljs-string">`
  color: <span class="hljs-subst">${props =&gt; props.theme.dark}</span>;
  font-size: large;
  code {
    font-size: 1.3rem;
  }
`</span>;
</code></pre>
<p>Let's have a look at the <code>render()</code> method now…</p>
<pre><code class="lang-js">render() {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">AppWrapper</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">AppHeader</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">AppLogo</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{logo}</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"logo"</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">AppTitle</span>&gt;</span>Welcome to React<span class="hljs-tag">&lt;/<span class="hljs-name">AppTitle</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">AppHeader</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">AppIntro</span>&gt;</span>
        Bootstrapped with <span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>create-react-app<span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span>.
      <span class="hljs-tag">&lt;/<span class="hljs-name">AppIntro</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">AppIntro</span>&gt;</span>
        Components styled with <span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>styled-components<span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span>{' '}
        <span class="hljs-tag">&lt;<span class="hljs-name">EmojiWrapper</span> <span class="hljs-attr">aria-label</span>=<span class="hljs-string">"nail polish"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">EmojiWrapper</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">AppIntro</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">AppWrapper</span>&gt;</span></span>
  )
}
</code></pre>
<p>Now all the classes originally used in <code>App.js</code> have been replaced so there's no need for the <code>import './App.css'</code> mapping, remove that aaaaand! Still no change!! Which is a good thing because at the moment we're swapping out the <code>.css</code> files for styled-components.</p>
<p>Cool, we have now replaced all the css with styled-components, now we can take a look at <code>injectGlobal</code>.</p>
<p>Let's take a look at how the <code>App.js</code> file should look before we move on:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> styled, { keyframes } <span class="hljs-keyword">from</span> <span class="hljs-string">'styled-components'</span>;
<span class="hljs-keyword">import</span> logo <span class="hljs-keyword">from</span> <span class="hljs-string">'./logo.svg'</span>;

<span class="hljs-keyword">const</span> AppWrapper = styled.div<span class="hljs-string">`
  text-align: center;
`</span>;

<span class="hljs-keyword">const</span> rotate360 = keyframes<span class="hljs-string">`
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
`</span>;

<span class="hljs-keyword">const</span> AppLogo = styled.img<span class="hljs-string">`
  animation: <span class="hljs-subst">${rotate360}</span> infinite 120s linear;
  height: 80px;
  &amp;:hover {
    animation: <span class="hljs-subst">${rotate360}</span> infinite 1.5s linear;
  }
`</span>;

<span class="hljs-keyword">const</span> AppHeader = styled.div<span class="hljs-string">`
  background-color: #222;
  height: 12rem;
  padding: 1rem;
  color: white;
`</span>;

<span class="hljs-keyword">const</span> AppTitle = styled.h1<span class="hljs-string">`
  font-weight: 900;
`</span>;

<span class="hljs-keyword">const</span> AppIntro = styled.p<span class="hljs-string">`
  font-size: large;
  code {
    font-size: 1.3rem;
  }
`</span>;

<span class="hljs-keyword">const</span> EmojiWrapper = styled.span.attrs({
  <span class="hljs-attr">role</span>: <span class="hljs-string">'img'</span>,
})<span class="hljs-string">``</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">AppWrapper</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">AppHeader</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">AppLogo</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{logo}</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"logo"</span> /&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">AppTitle</span>&gt;</span>Welcome to React<span class="hljs-tag">&lt;/<span class="hljs-name">AppTitle</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">AppHeader</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">AppIntro</span>&gt;</span>
          Bootstrapped with <span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>create-react-app<span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span>.
        <span class="hljs-tag">&lt;/<span class="hljs-name">AppIntro</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">AppIntro</span>&gt;</span>
          Components styled with <span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>styled-components<span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">EmojiWrapper</span> <span class="hljs-attr">aria-label</span>=<span class="hljs-string">"nail polish"</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">AppIntro</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">AppWrapper</span>&gt;</span></span>
    );
  }
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<h3 id="heading-style-the-body-with-injectglobal">Style the body with injectGlobal</h3>
<p>For styling the body of our react app we currently have the <code>index.css</code> file that is being imported into the mounting point of our app in the <code>index.js</code> file.</p>
<p>To style the body we can use <a target="_blank" href="https://www.styled-components.com/docs/api#injectglobal"><code>injectGlobal</code></a> from styled-components which adds the styles directly to the stylesheet.</p>
<p>To do this you bring in the <code>injectGolabl</code> named export from styled-components and add your styles between the back ticks.</p>
<p>The current <code>index.css</code> looks like this:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">font-family</span>: sans-serif;
}
</code></pre>
<p>Let's add a <code>theme</code> folder in the <code>src</code> directory and add a <code>globalStyle.js</code> file where we can keep all our styles we want to use throughout the app, keeping the styles on one place will make changes simpler.</p>
<p>In <code>src/theme/globalStyle.js</code> we'll need to import the <code>injectGlobal</code> named export from styled-components and add the <code>index.css</code> styles into it:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { injectGlobal } <span class="hljs-keyword">from</span> <span class="hljs-string">'styled-components'</span>;

injectGlobal<span class="hljs-string">`
  body {
    padding: 0;
    margin: 0;
    font-family: sans-serif;
  }
`</span>;
</code></pre>
<p>Ok, now we're adding the body style to the stylesheet directly so there is no need for the <code>index.css</code> file mapping that is in <code>index.js</code> it should look like this now:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span> <span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">'react-dom'</span>

<span class="hljs-keyword">import</span> App <span class="hljs-keyword">from</span> <span class="hljs-string">'./App'</span>

<span class="hljs-keyword">import</span> registerServiceWorker <span class="hljs-keyword">from</span> <span class="hljs-string">'./registerServiceWorker'</span>

ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span></span>, <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>))

registerServiceWorker()
</code></pre>
<p>We should still have our nice <code>sans-serif</code> font going on, but let's add in some nice Roboto for the body and Montserrat for the heading in our <code>globalStyle.js</code> module. We can import Google fonts with an <code>@import</code> in <code>injectGlobal</code> and apply Roboto to the body:</p>
<pre><code class="lang-js">injectGlobal<span class="hljs-string">`
  @import url(‘https://fonts.googleapis.com/css?family=Montserrat|Roboto');

  body {
    padding: 0;
    margin: 0;
    font-family: Roboto, sans-serif;
  }
`</span>;
</code></pre>
<p>Cool now we can add our imported font for or app header, and there's the option if we want all our <code>&lt;h1&gt;</code>'s to use the same font we can add that to the injectGlobal in our <code>globalStyle.js</code> module.</p>
<pre><code class="lang-js">injectGlobal<span class="hljs-string">`
  @import url(‘https://fonts.googleapis.com/css?family=Montserrat:400,900|Roboto');
  body {
    padding: 0;
    margin: 0;
    font-family: Roboto, sans-serif;
  }
  h1 {
    font-family: Montserrat;
  }
`</span>;
</code></pre>
<p>Then we can adjust the weight on the <code>AppTitle</code> component:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> AppTitle = styled.h1<span class="hljs-string">`
  font-weight: 900;
`</span>;
</code></pre>
<p>To add the additional styles for fonts like Montserrat and Roboto you can specify them in the <code>@import url()</code> you'll notice that Montserrat has <code>:400,900</code> after it that is specifying the styles regular (400) and black (900), you can import as many as you like from Google fonts (CDN) but the more you import the longer it will take to load them, if you have a lot of fonts and styles you want in your app then consider adding them to a folder in the project, like:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> Montserrat <span class="hljs-keyword">from</span> <span class="hljs-string">'./fonts/Montserrat-Regular.ttf'</span>;

injectGlobal<span class="hljs-string">`@font-face { font-family: Montserrat; src: url(<span class="hljs-subst">${Montserrat}</span>); }`</span>;
</code></pre>
<h3 id="heading-theming">Theming</h3>
<p>Themes are often used to change the look and feel of a wide range of things at once. For example, you may have a night and day mode like in Twitter. You can create your own themes in styled-components too.</p>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/gwn8czgagns1n1545zgn.png" alt="Image" width="800" height="572" loading="lazy"></p>
<h3 id="heading-use-the-styled-components-themeprovider">Use the styled-components ThemeProvider</h3>
<p>Now say we want to have several components in our app that use a CSS colour property <code>color: #6e27c5</code> instead of hard coding it through the app for every component that uses it we can use the styled-components <code>ThemeProvider</code>.</p>
<p>For this we will need to import the <code>ThemeProvider</code> named export from styled-components, then define a <code>theme</code> object where our colour is going to live:</p>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> theme = {
  <span class="hljs-attr">primary</span>: <span class="hljs-string">'#6e27c5'</span>,
};
</code></pre>
<p>Let's add the newly created <code>theme</code> to the <code>globalStyle</code> module we created previously.</p>
<p>To make the theme object available throughout the app component we'll wrap our app component in the <code>ThemeProvider</code> and import our awesome theme for use in the <code>ThemeProvider</code>:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> styled, { keyframes, ThemeProvider } <span class="hljs-keyword">from</span> <span class="hljs-string">'styled-components'</span>;
<span class="hljs-keyword">import</span> logo <span class="hljs-keyword">from</span> <span class="hljs-string">'./logo.svg'</span>;
<span class="hljs-keyword">import</span> { theme } <span class="hljs-keyword">from</span> <span class="hljs-string">'./theme/globalStyle'</span>;

<span class="hljs-comment">// our styled-components</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
  render() {
    <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ThemeProvider</span> <span class="hljs-attr">theme</span>=<span class="hljs-string">{theme}</span>&gt;</span>{/* all children can access the theme object */}<span class="hljs-tag">&lt;/<span class="hljs-name">ThemeProvider</span>&gt;</span></span>;
  }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<p>Now the <code>theme</code> properties can be used as props in our styled-components, let's change the <code>background-color:</code> in the <code>AppHeader</code> component, whilst we're at it let's add a <code>dark: #222</code> property to our <code>theme</code> object and use that for the <code>color</code> property:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> AppHeader = styled.div<span class="hljs-string">`
  height: 12rem;
  padding: 1rem;
  color: <span class="hljs-subst">${props =&gt; props.theme.dark}</span>;
  background-color: <span class="hljs-subst">${props =&gt; props.theme.primary}</span>;
`</span>;
</code></pre>
<p>Now we can change our app theme globally</p>
<h3 id="heading-ok-cool-can-you-change-theme">Ok cool, can you change theme?</h3>
<p>This is what I was thinking and it turns out you can, there's a great <a target="_blank" href="https://stackoverflow.com/a/42899979/1138354">Stack Overflow answer</a> from <a target="_blank" href="https://twitter.com/mxstbr">Max</a> on it.</p>
<p>It got me thinking if you can switch between themes rather than define them for different sections like in the SO answer.</p>
<p>I started off by defining two themes (with imaginative names) in the <code>globalStyle.js</code> module:</p>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> theme1 = {
  <span class="hljs-attr">primary</span>: <span class="hljs-string">'#ff0198'</span>,
  <span class="hljs-attr">secondary</span>: <span class="hljs-string">'#01c1d6'</span>,
  <span class="hljs-attr">danger</span>: <span class="hljs-string">'#eb238e'</span>,
  <span class="hljs-attr">light</span>: <span class="hljs-string">'#f4f4f4'</span>,
  <span class="hljs-attr">dark</span>: <span class="hljs-string">'#222'</span>,
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> theme2 = {
  <span class="hljs-attr">primary</span>: <span class="hljs-string">'#6e27c5'</span>,
  <span class="hljs-attr">secondary</span>: <span class="hljs-string">'#ffb617'</span>,
  <span class="hljs-attr">danger</span>: <span class="hljs-string">'#f16623'</span>,
  <span class="hljs-attr">light</span>: <span class="hljs-string">'#f4f4f4'</span>,
  <span class="hljs-attr">dark</span>: <span class="hljs-string">'#222'</span>,
};
</code></pre>
<p>Now we need a way to switch between the two <code>theme</code> objects, let's use a select box for them, let's create a components folder and in there make a <code>ThemeSelect.js</code> component, we can worry about refactoring the <code>App.js</code> component when I'm not here :</p>
<h4 id="heading-themeselectjs">ThemeSelect.js</h4>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> styled <span class="hljs-keyword">from</span> <span class="hljs-string">'styled-components'</span>;

<span class="hljs-keyword">const</span> Select = styled.select<span class="hljs-string">`
  margin: 2rem 0.5rem;
  padding: 0rem 0.5rem;
  font-family: Roboto;
  font-size: 1rem;
  border: 1px solid <span class="hljs-subst">${props =&gt; props.theme.light}</span>;
  box-shadow: 0px 0px 0px 1px rgba(0, 0, 0, 0.1);
  background: <span class="hljs-subst">${props =&gt; props.theme.light}</span>;
  border-radius: 2px;
`</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> SelectOpt = styled.option<span class="hljs-string">`
  font-family: Roboto;
  font-size: 1rem;
`</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ThemeSelect</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Select</span> <span class="hljs-attr">onChange</span>=<span class="hljs-string">{e</span> =&gt;</span> this.props.handleThemeChange(e)}&gt;
          <span class="hljs-tag">&lt;<span class="hljs-name">SelectOpt</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"theme1"</span>&gt;</span>Theme 1<span class="hljs-tag">&lt;/<span class="hljs-name">SelectOpt</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">SelectOpt</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"theme2"</span>&gt;</span>Theme 2<span class="hljs-tag">&lt;/<span class="hljs-name">SelectOpt</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">Select</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> ThemeSelect;
</code></pre>
<p>You've probably noticed the <code>onChange={e =&gt; this.props.handleThemeChange(e)</code> event, we're going to add that method to the <code>App.js</code> component along with some state to manage what theme is selected.</p>
<h4 id="heading-appjs">App.js</h4>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> styled, { keyframes, ThemeProvider } <span class="hljs-keyword">from</span> <span class="hljs-string">'styled-components'</span>;

<span class="hljs-keyword">import</span> logo <span class="hljs-keyword">from</span> <span class="hljs-string">'./logo.svg'</span>;

<span class="hljs-keyword">import</span> { theme1, theme2 } <span class="hljs-keyword">from</span> <span class="hljs-string">'./theme/globalStyle'</span>;
<span class="hljs-keyword">import</span> ThemeSelect <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/ThemeSelect'</span>;

<span class="hljs-comment">// our lovely styled-components here</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
  state = {
    <span class="hljs-attr">theme</span>: theme1,
  };
  handleThemeChange = <span class="hljs-function"><span class="hljs-params">e</span> =&gt;</span> {
    <span class="hljs-keyword">let</span> theme = e.target.value;
    theme === <span class="hljs-string">'theme1'</span> ? (theme = theme1) : (theme = theme2);
    <span class="hljs-built_in">this</span>.setState({ theme });
  };
  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ThemeProvider</span> <span class="hljs-attr">theme</span>=<span class="hljs-string">{this.state.theme}</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">AppWrapper</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">AppHeader</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">AppLogo</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{logo}</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"logo"</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">AppTitle</span>&gt;</span>Welcome to React<span class="hljs-tag">&lt;/<span class="hljs-name">AppTitle</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">AppHeader</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">AppIntro</span>&gt;</span>
            Bootstrapped with <span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>create-react-app<span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span>.
          <span class="hljs-tag">&lt;/<span class="hljs-name">AppIntro</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">AppIntro</span>&gt;</span>
            Components styled with <span class="hljs-tag">&lt;<span class="hljs-name">code</span>&gt;</span>styled-components<span class="hljs-tag">&lt;/<span class="hljs-name">code</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">EmojiWrapper</span> <span class="hljs-attr">aria-label</span>=<span class="hljs-string">"nail polish"</span> /&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">AppIntro</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">ThemeSelect</span> <span class="hljs-attr">handleThemeChange</span>=<span class="hljs-string">{this.handleThemeChange}</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">AppWrapper</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">ThemeProvider</span>&gt;</span></span>
    );
  }
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<p>To summarise what we have done with <code>App.js</code> here is, add some state to default to theme1 where the two themes are imported as named exports of the <code>globalStyle.js</code> module.</p>
<p>Add a method to handle the change of the <code>ThemeSelect.js</code> component <code>handleThemeChange</code> this is where we can switch between the two <code>theme</code> objects.</p>
<p>Let's try it out, we should be able to switch between the two themes we've defined now.</p>
<h3 id="heading-extending-styled-components">Extending styled-components</h3>
<p>So far our app hasn't got many styled-components that are similar but what if we were to add some buttons…</p>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> Button = styled.button<span class="hljs-string">`
  font-size: 1rem;
  border-radius: 5px;
  padding: 0.25rem 1rem;
  margin: 0 1rem;
  background: transparent;
  color: <span class="hljs-subst">${props =&gt; props.theme.primary}</span>;
  border: 2px solid <span class="hljs-subst">${props =&gt; props.theme.primary}</span>;
  <span class="hljs-subst">${props =&gt;
    props.primary &amp;&amp;
    css`<span class="css">
      <span class="hljs-selector-tag">background</span>: </span><span class="hljs-subst">${props =&gt; props.theme.primary}</span><span class="css">;
      <span class="hljs-selector-tag">color</span>: <span class="hljs-selector-tag">white</span>;
    `</span>}</span>;
`</span>;
</code></pre>
<p>Here I've added a <code>Button</code> component to the <code>globalStyle.js</code> for us to use in the <code>App.js</code> component. For the sake of convenience we're going to add it here, you may find if you have a lot of similar components that you are reusing throughout your app that it may be a good idea to add them all to a <code>components</code> folder.</p>
<p>We can import the <code>Button</code> as you would any other component and use it in the module, as we're extending it this means we only need to apply the specific styles we want for that button. But first in the <code>App.js</code> component we can specify a normal and a primary button:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>Normal Button<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">primary</span>&gt;</span>Primary Button<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
</code></pre>
<p>Now to specify another button with the same css as the imported button we can extend it, like in this example we'll make the button take up 40% of the screen width and make the corners more rounded:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> BigButt = Button.extend<span class="hljs-string">`
  height: 3rem;
  font-size: 2rem;
  width: 40vw;
  border-radius: 30px;
`</span>;
</code></pre>
<p>Let's also apply the theme for an underline on <code>create-react-app</code> and <code>styled-components</code> by adding in an <code>Underline</code> styled-component:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Underline = styled.span<span class="hljs-string">`
  border-bottom: 4px solid <span class="hljs-subst">${props =&gt; props.theme.secondary}</span>;
`</span>;
</code></pre>
<p>Now we can switch the theme and have it applied to our components using the theme, pretty neat, right?</p>
<p>I have put all of the examples we have gone over here in a working example for you to play around with the theming and styled-components, enjoy.</p>
<p><a target="_blank" href="https://codesandbox.io/s/x26q7l9vyq?from-embed">https://codesandbox.io/s/x26q7l9vyq?from-embed</a></p>
<h3 id="heading-want-to-know-more">Want to know more?</h3>
<p>A great resource for getting started with styled-components which really helped me is <a target="_blank" href="https://twitter.com/simonswiss">Simon Vrachliotis</a>'s <a target="_blank" href="https://egghead.io/">egghead.io</a> styled-components <a target="_blank" href="https://egghead.io/playlists/styled-components-4169206d">playlist</a> which is a great foundation for starting out with styled-components ? the first lesson is for pro members but the rest are currently available to watch for free.</p>
<p>There's also the <a target="_blank" href="https://spectrum.chat/?t=54887141-57a9-4386-807c-ed950c4d5132">spectrum.chat</a> community and of course <a target="_blank" href="https://stackoverflow.com/questions/tagged/styled-components">Stack Overflow</a>.</p>
<h3 id="heading-thanks-for-reading">Thanks for reading</h3>
<p>If there is anything I have missed, or if you have a better way to do something then please let me know.</p>
<p>Find me on <a target="_blank" href="https://twitter.com/spences10">Twitter</a> or <a target="_blank" href="https://github.com/spences10/ama">Ask Me Anything</a> on GitHub.</p>
<blockquote>
<p><strong>You can read other articles like this on <a target="_blank" href="https://thelocalhost.blog/">my blog</a>.</strong></p>
</blockquote>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
