r/Web_Development • u/breathing-is-good • Jul 23 '20
React - best practice to use Stylesheet or JS variable inside component?
Question is in the title, I'm learning React and I'm curious where everyone puts their styling. For instance, a nav bar. Is it better to only use a JS variable for styling that is dynamic, and put everything else in a stylesheet?
Thanks!
2
Upvotes
3
u/ethorf Jul 23 '20
Personally I'm not a fan of styled components, I feel like they bog down my JS files and I'd much rather separate my concerns as much as I can (though obviously components + their respective styles are intertwined), good ol' external stylesheets w/ SASS is certainly my preferred development environment. That being said I can see the logic of styled components when it applies to the maintainability of larger teams / projects etc.
1
u/tzigane Jul 23 '20
Look into "styled components" via either https://styled-components.com/ or https://emotion.sh/ (which I prefer). It lets you define & use semantic components with styling applied:
You can also do dynamic styling by adding properties:
Then you could use it as
<SomeComponent red={true} />