0

Hello so I am trying styled components for the first time so I do not know where I might be going wrong because I have my webpack setup nicely and my .babelrc file.

So when I was reading through the docs of Styled component they mentioned adding this plugin "babel-plugin-styled-components",

Here is my .babelrc

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react",
    "@babel/preset-typescript"
  ],
  "plugins": ["babel-plugin-styled-components"]
}

So I wont be adding webpack config because when searching I never saw anything that linked styled components to webpack so I believe setup is by .babelrc... But if webpack config is required please comment and I will add that.

Here is the basic component where I wanted to use styled components

import React, { FC } from 'react';
import Styled from 'styled-components';

const Title = Styled.h1`
  color:black;
`;

export const App: FC = () => {
  return (
    <div className="App">
      <h1>Brag Diary</h1>
    </div>
  );
};

The style is not made so I do now know where I might be going wrong can I please get some assistant

1 Answer 1

1

You have to refer to the styled component directly:

import React, { FC } from 'react';
import Styled from 'styled-components';

const Title = Styled.h1`
  color:black;
`;

export const App: FC = () => {
  return (
    <div className="App">
      <Title>Brag Diary</Title>
    </div>
  );
};
Sign up to request clarification or add additional context in comments.

2 Comments

Quick Questions before you go let's say I want to style the div having class App I would wrap it around replace the div with whatever variable that I had decided? And is it best practice to isolate style to another file then import?
Yep that's how they work. It's up to you how you organise your components, but as an application grows, it can be better organised if each is in it's own individually responsible file. When you want to share high level things such as colours and font sizes, look into using themes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.