How add Style-components to Next.js and start using it 🤪

In this tutorial, I’ll show you how to add Styled-component to Next.js. If you don’t know how to create a basic app with Next.js I’ll suggest to you first read this post here

For this tutorial, I’ll use:

I’ll use VSCode for our example here…


This content originally appeared on DEV Community and was authored by Tiago Brito

In this tutorial, I'll show you how to add Styled-component to Next.js. If you don't know how to create a basic app with Next.js I'll suggest to you first read this post here

For this tutorial, I'll use:

I'll use VSCode for our example here, but you can use any other code editor that you prefer.

How to instal Styled Component? 🤔

On your terminal go to your folder project.
if you are using npm run:

npm install --save styled-components

if you are using yar run:

yarn add styled-components

PS: If you use yarn it is recommended that you go to your package.json file and add the following.

  "resolutions": {
    "styled-components": "^5"
  }

This is to avoid many problems that can happen from multiple versions of styled components being used on your project.

Image showing where to add the code above

Congratulations 👏 🎉 you add Styled-component to your project, easy right?

Well, how do I use it now?🤔

Styled components use tagged templates literal to style your components. So you can give names to H1, p, button tags and so on, it helps to debug and make your code much cleaner to read in my opinion.

So instead of having a component like this👇
Image with sample of old style

You can have it like this👇
Image with sample of new styles

But how do we do it?🤔

Simple, first we need to import the style from the styled component like so 👇

import styled from "styled-components";

and then export a const with the name you choose with the styled template literal like below 👇
Image description

So your file will look like this 👇
Image showing how the file should look like

Then styled components will generate the tags and add unique classes to your tags.

Image showing the class names with a random code

But it also makes it super hard to debug later, as you just have an h1 or div and trying to find which one is not working will be crazy.

To solve this issue we can run in our terminal the following:
If you use yarn

yarn add babel-plugin-styled-components --dev

If you use npm

npm install --save-dev babel-plugin-styled-components

We will need to create a file called .babelrc at the root of our project.
and add the following code:

{
  "presets": ["next/babel"],
  "plugins": [["styled-components", { "ssr": true, "displayName": true }]]
}

Image showing the file to be changed

and voila 💃

Image showing that the class name has the component name in it.

Now the const name we created Title will be added to our tags as part of the class names, making our lives so much easier

Now for the real congratulations 👏 🎉 👏 🎉
We just added styled components to our project and learned how to start using them.

We are the champions

What next?

Well, this is just the tip of the iceberg, Styled components have so much more to be explored that I'll have new posts about it soon.

Until next time 👋


This content originally appeared on DEV Community and was authored by Tiago Brito


Print Share Comment Cite Upload Translate Updates
APA

Tiago Brito | Sciencx (2022-02-15T22:09:27+00:00) How add Style-components to Next.js and start using it 🤪. Retrieved from https://www.scien.cx/2022/02/15/how-add-style-components-to-next-js-and-start-using-it-%f0%9f%a4%aa/

MLA
" » How add Style-components to Next.js and start using it 🤪." Tiago Brito | Sciencx - Tuesday February 15, 2022, https://www.scien.cx/2022/02/15/how-add-style-components-to-next-js-and-start-using-it-%f0%9f%a4%aa/
HARVARD
Tiago Brito | Sciencx Tuesday February 15, 2022 » How add Style-components to Next.js and start using it 🤪., viewed ,<https://www.scien.cx/2022/02/15/how-add-style-components-to-next-js-and-start-using-it-%f0%9f%a4%aa/>
VANCOUVER
Tiago Brito | Sciencx - » How add Style-components to Next.js and start using it 🤪. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/15/how-add-style-components-to-next-js-and-start-using-it-%f0%9f%a4%aa/
CHICAGO
" » How add Style-components to Next.js and start using it 🤪." Tiago Brito | Sciencx - Accessed . https://www.scien.cx/2022/02/15/how-add-style-components-to-next-js-and-start-using-it-%f0%9f%a4%aa/
IEEE
" » How add Style-components to Next.js and start using it 🤪." Tiago Brito | Sciencx [Online]. Available: https://www.scien.cx/2022/02/15/how-add-style-components-to-next-js-and-start-using-it-%f0%9f%a4%aa/. [Accessed: ]
rf:citation
» How add Style-components to Next.js and start using it 🤪 | Tiago Brito | Sciencx | https://www.scien.cx/2022/02/15/how-add-style-components-to-next-js-and-start-using-it-%f0%9f%a4%aa/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.