If you're deciding between styled components and vanilla CSS (with BEM), choose the one that best suits your needs.
I have experience with both and believe that both have their merits, but there are some distinctions between them.
For instance, if you need to use variables throughout all components (such as themes), styled components require you to use ThemeProvider and then consume it like this:
const FancyDiv = styled.div`
background: ${props => props.theme.colors.main)``;
On the other hand, with vanilla CSS, you need to import a CSS file in each component's CSS file, like this in SCSS:
// src/components/fancyDiv/styles.scss
@import '../../styles/settings.scss';
.fancyDiv {
background: $main-color;
}
Another difference arises when you need to style an element based on props or state. Consider a scenario where you want to style a navigation link based on whether it is active or not.
const [isActive, setIsActive] = useState(false);
Using styled components:
const FancyLink = styled.a`
color: ${props => props.isActive ? 'red' : 'green'}`;
<FancyLink href="#aboutme" isActive={isActive}>About me</FancyLink>
With vanilla CSS:
.nav__link--active {
color: 'red';
}
<a href="#aboutme" className=`nav-link ${isActive ? 'nav__link--active' : ''}`>About me</a>
Both approaches have their strengths and can be suitable for various situations, albeit in slightly different ways.