<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: joseph emmanuel kayode (iemarjay)</title>
    <description>The latest articles on Forem by joseph emmanuel kayode (iemarjay) (@josephemmanuel).</description>
    <link>https://forem.com/josephemmanuel</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F41112%2F4c00c128-1999-4025-bd14-c3333b6e3ece.jpeg</url>
      <title>Forem: joseph emmanuel kayode (iemarjay)</title>
      <link>https://forem.com/josephemmanuel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/josephemmanuel"/>
    <language>en</language>
    <item>
      <title>How to setup NextJs and Typescript with Styled-component</title>
      <dc:creator>joseph emmanuel kayode (iemarjay)</dc:creator>
      <pubDate>Tue, 13 Dec 2022 14:19:20 +0000</pubDate>
      <link>https://forem.com/josephemmanuel/how-to-setup-nextjs-and-typescript-with-styled-component-2fa6</link>
      <guid>https://forem.com/josephemmanuel/how-to-setup-nextjs-and-typescript-with-styled-component-2fa6</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Follow this guide to setup style components in react components &lt;/p&gt;

&lt;p&gt;Styled-components makes it simple to style your components with CSS by using javascript code. It also makes it simple to design a theme for your entire project. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://styled-components.com/docs" rel="noopener noreferrer"&gt;Learn More&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Table Of Content
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create Nextjs Project&lt;/li&gt;
&lt;li&gt;Add Styled-components Library&lt;/li&gt;
&lt;li&gt;[optional] Styled Components Babel Plugin&lt;/li&gt;
&lt;li&gt;[optional] SSR Stylesheet Rehydration&lt;/li&gt;
&lt;li&gt;[optional] Absolute imports &amp;amp; Module Path Aliases&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Create Nextjs Project
&lt;/h2&gt;

&lt;p&gt;Create Nextjs easily by using &lt;code&gt;create-next-app&lt;/code&gt; by running the command below replace  with your project name&lt;/p&gt;

&lt;p&gt;&lt;code&gt;yarn create next-app &amp;lt;project&amp;gt; --ts&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;or if you need eslint run the command below instead, replace  with your project name&lt;/p&gt;

&lt;p&gt;&lt;code&gt;yarn create next-app &amp;lt;project&amp;gt; --ts --eslint&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://nextjs.org/learn/basics/create-nextjs-app" rel="noopener noreferrer"&gt;learn more on getting started with Nuxtjs&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Add Styled-components Library
&lt;/h2&gt;

&lt;p&gt;In your project root directory run the command below to add styled-components library&lt;/p&gt;

&lt;p&gt;&lt;code&gt;yarn add styled-components &amp;amp;&amp;amp; yarn add -D @types/styled-components&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;globalStyle&lt;/code&gt;, to share styles across all of your pages, for example if you would like;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setting a font-family for all your typography&lt;/li&gt;
&lt;li&gt;Setting the background color on every page&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdtflhfxow%2Fimage%2Fupload%2Fc_scale%2Cw_317%2Fv1669324144%2FScreenshot_2022-11-25_at_12.45.03_AM_c7vjcu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdtflhfxow%2Fimage%2Fupload%2Fc_scale%2Cw_317%2Fv1669324144%2FScreenshot_2022-11-25_at_12.45.03_AM_c7vjcu.png" alt="layout directory"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;create a folder &lt;code&gt;/layout&lt;/code&gt; with file &lt;code&gt;Default.tsx&lt;/code&gt; inside, then add the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createGlobalStyle } from "styled-components";

export const GlobalStyle = createGlobalStyle`
  html,
  body {
    padding: 0;
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
      Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
    }

    a {
      color: inherit;
      text-decoration: none;
    }

    * {
      box-sizing: border-box;
    }
  }

  p {
    color: red;
  }

  // anything else you would like to include
`;

export function LayoutDefault({ children }: { children: any }) {
  return (
    &amp;lt;&amp;gt;
      &amp;lt;GlobalStyle /&amp;gt;
      {children}
    &amp;lt;/&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the GlobalStyle variable is where you define all your global styles.&lt;br&gt;
  In this example, we simply set the font family, link color, and paragraph color for every page LayoutDefault is used&lt;/p&gt;

&lt;p&gt;This isn’t going to apply the styles to the project yet.&lt;/p&gt;

&lt;p&gt;Now we need to use that file to apply the global styles to the project.&lt;/p&gt;

&lt;p&gt;Modify &lt;code&gt;index.tsx&lt;/code&gt; file in the &lt;code&gt;/pages&lt;/code&gt; folder, import the LayoutDefault component and wrap the returned React element in the Home function with LayoutDefault.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import styled from "styled-components";
import LayoutDefault from "../layout/Default";

const StyledHeading = styled.h1`
  color: pink;
`;

export default function Home() {
  return (
    &amp;lt;LayoutDefault&amp;gt;
      &amp;lt;StyledHeading&amp;gt;Title&amp;lt;/StyledHeading&amp;gt;
      &amp;lt;p&amp;gt;hello&amp;lt;/p&amp;gt;
    &amp;lt;/LayoutDefault&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://nextjs.org/docs/basic-features/layouts" rel="noopener noreferrer"&gt;learn more about Layouts&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  [optional] Styled Components Babel Plugin
&lt;/h2&gt;

&lt;p&gt;You need to add the babel styled components plugin, if you want any of the following features &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Smaller bundles&lt;/li&gt;
&lt;li&gt;Server-side rendering compatibility&lt;/li&gt;
&lt;li&gt;Better debugging&lt;/li&gt;
&lt;li&gt;Minification&lt;/li&gt;
&lt;li&gt;Dead code elimination
&lt;a href="https://styled-components.com/docs/tooling#babel-plugin" rel="noopener noreferrer"&gt;learn more about&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;yarn add -D babel-plugin-styled-components&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Create a file &lt;code&gt;.babelrc&lt;/code&gt; at the root of your project with the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "presets": ["next/babel"],
  "plugins": [["styled-components", { "ssr": true }]]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  [optional] SSR Stylesheet Rehydration
&lt;/h2&gt;

&lt;p&gt;ServerStyleSheet is created by Styled-Components. Any styles located in any of the components inside of our  are retrieved from this stylesheet. Later, this is passed into our HTML template.&lt;/p&gt;

&lt;p&gt;create file &lt;code&gt;_document.tsx&lt;/code&gt; in the &lt;code&gt;/pages&lt;/code&gt; directory and add the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Document, { DocumentContext, DocumentInitialProps } from 'next/document';
import { ServerStyleSheet } from 'styled-components';

export default class MyDocument extends Document {
  static async getInitialProps(
    ctx: DocumentContext
  ): Promise&amp;lt;DocumentInitialProps&amp;gt; {
    const sheet = new ServerStyleSheet();
    const originalRenderPage = ctx.renderPage;

    try {
      ctx.renderPage = () =&amp;gt;
        originalRenderPage({
          enhanceApp: (App) =&amp;gt; (props) =&amp;gt;
            sheet.collectStyles(&amp;lt;App {...props} /&amp;gt;),
        });

      const initialProps = await Document.getInitialProps(ctx);
      return {
        ...initialProps,
        styles: (
          &amp;lt;&amp;gt;
            {initialProps.styles}
            {sheet.getStyleElement()}
          &amp;lt;/&amp;gt;
        ),
      };
    } finally {
      sheet.seal();
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  [optional] Absolute imports &amp;amp; Module Path Aliases
&lt;/h2&gt;

&lt;p&gt;If you've ever imported deep components and they appear with a module path alias like this:../../../../DeepComponent, with Module Path Alias you can import them like this instead &lt;code&gt;components/path/DeepComponent&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://nextjs.org/docs/advanced-features/module-path-aliases" rel="noopener noreferrer"&gt;learn about absolut imports and module path aliases&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    "baseUrl": "."
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in &lt;code&gt;/pages/index.tsx&lt;/code&gt; you can do this instead of that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// This
import LayoutDefault from "layout/Default";

// That
import LayoutDefault from "../layout/Default";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>styledcomponents</category>
      <category>react</category>
      <category>nextjs</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
