How disheartening it would be to see such a review on your app, which you made with blood and sweat. It is not a new discovery that a classy and user-friendly UI is necessary to complete your great idea into a successful app. Every developer knew this struggle until 2011 when Facebook first used React for its newsfeed. Or is there something else?
Before we dive deeper into styled components, let us take a quick look at React.
What is React?
As per a report by Statista, in 2022, React was the second most-used framework, with 42.62% of developers using it. Not just that, there are more than 10 million websites live with react as their development framework.
Now that we have gone through what React and components are let’s understand styled components and their pros and cons.
Styled components is a library built for React and React Native. It allows you to style your react components by writing actual CSS in it, giving developers the flexibility to use their CSS code on multiple projects. This also saves time and effort for the developers.
Styled Components utilizes tagged template literals to style react components, which removes the mapping between style and component. Meaning that when you are creating your styles, you are actually writing a normal react component, which has your styles attached to it.
Yes, the Styled components library is easy, flexible, and amazing, but just like everything else, it also has its pros and cons. What are they? Let’s see:
Pros of Styled-components
- Allows You to Write CSS Inside the Components
As mentioned earlier, styled components literally let you write your actual CSS script in the component. By writing this script in components, you are making it flexible and reusable. This ultimately means that once you write a script in a component if you ever want to use it again for the same project or any other project, it will be there forever.
- Allows Type Checking
Since styled components are React components, they also support type-checking, which helps developers catch mistakes early.
- Supports Theming Out-of-the-box
Themes are a set of predefined graphics, colors, and styles. Styled components support out-of-the-box theming. It uses a wrapper component called ThemeProvider, which attaches the theme to all your other components that are wrapped within it.
- Styled-components Offers React Native Mobile Support
Styled components support both React and React Native. React Native helps you create apps with seamless user experiences. Using styled components, you can create a similar-looking UI for both your website and app easily.
- No-class Policy in Styled-components
Styled components use props instead of class. No class basically means that you do not have to use “this” in your code. Which helps in debugging and improves the overall readability of your script.
- Supports Server Side Rendering
By using style components, you enable your stylesheet to be rendered on the server side. By enabling server-side rendering, your page can load faster, improving user experience. Not just that, when rendering server side, search engines can load and crawl the content faster and more efficiently, which is vital for SEO.
Cons Of Styled-Components
- It’s Not Framework Independent
- Poor Readability
- Styled Components are Compiled at Run Time
- Extra Build Tools are Required
To enjoy all the above-listed pros of styled components, you have to add some extra plugins. These plugins are required to enable your styled components to be written separately, to enable server-side rendering, and more. Some such building tools are Babel plugin, Babel Macros, TypeScript Plugin, Stylelint, etc.
- Mixing CSS Modules and Global CSS Classes is Cumbersome
Global styling, such as header, body, HTML, divs, etc., is not component based. So using them with CSS modules gets difficult for larger applications. Mixing these two requires a lot of wrapping and complex coding, which makes the styled component a rather difficult job.
With all of these pros and cons apart, every developer writes the styling in their own way. Based on the size of the application, the complexity of the UI, and the requirement of styling, you can decide which method will work better for your application and your team.