Home > database >  what is the benefit of using styled components over using css class for the components
what is the benefit of using styled components over using css class for the components

Time:03-25

We write css when we use styled components, and I dont understand the benefit of using it why we just dont use the css we write as a classname for the components. In styled components we have wrappers of the components and when we use plain css we have class names. what is the advantage of wrapping components with styled components

CodePudding user response:

The reason styled components is pushed in React web development is to encapsulate styles to only those components. There is no risk of bleeding css into other components. Rather than learn a new css organisation structure new developers can onboard more quickly.

That being said, it is restrictive, slower to develop with, and adds some page weight by applying same styles you could have shared.

I have found the fastest way to work is create static html webpages with pure css, and organise it in a way you are going to import it into your framework. Then you can have boilerplate html designs that can be tested independently of the compiler, which is so much faster to test in Internet Explorer, Firefox, chrome, mobile devices and all the varying screen sizes.

If you want to use styled components, you can either use this npm plugin to convert your static css into variables to use with style components - https://www.npmjs.com/package/@inspiraller/create-css-vars

or just don't. Nextjs does not support css imports unless its global, so webpack compiling option is a more universal solution.

CodePudding user response:

With styled components you can add logic programming

CodePudding user response:

Main benefits of styled-components are listed in the Motivations page on their website.

styled-components is the result of wondering how we could enhance CSS for styling React component systems. By focusing on a single use case we managed to optimize the experience for developers as well as the output for end users.

Apart from the improved experience for developers, styled-components provides:

  • Automatic critical CSS: styled-components keeps track of which components are rendered on a page and injects their styles and nothing else, fully automatically. Combined with code splitting, this means your users load the least amount of code necessary.
  • No class name bugs: styled-components generates unique class names for your styles. You never have to worry about duplication, overlap or misspellings.
  • Easier deletion of CSS: it can be hard to know whether a class name is used somewhere in your codebase. styled-components makes it obvious, as every bit of styling is tied to a specific component. If the component is unused (which tooling can detect) and gets deleted, all its styles get deleted with it.
  • Simple dynamic styling: adapting the styling of a component based on its props or a global theme is simple and intuitive without having to manually manage dozens of classes.
  • Painless maintenance: you never have to hunt across different files to find the styling affecting your component, so maintenance is a piece of cake no matter how big your codebase is.
  • Automatic vendor prefixing: write your CSS to the current standard and let styled-components handle the rest.

With styled components it's possible to "enhance" CSS classes with JavaScript code because are written in JS.

const Input = styled.input`
  color: ${props => props.hasError ? 'red' : 'black'};
`;

Opposed to writing 2 separate CSS classes and control which to show with JS.

.text {
  color: black;
}

.text--danger {
  color: red;
}
<input className={`text ${hasError ? 'text--danger' : ''}`} />

On the long run it can produce more maintainable code, but it's a matter of personal opinion if it's better to use it or not.

CodePudding user response:

Styled components allow you to still use selectors inside your styling without losing the capabilities of programmability through javascript. This is a big thing because the power of selectors is way more than just using classes, and by having the possibility of making the styles dynamic by using javascript the possibilities are almost infinite.

I will suggest taking a read on https://styled-components.com/docs/basics#motivation

  • Related