1
It’s place in the front-end landscape…
and in your .
styled-components
1. Introduction
2. What is CSS-in-JS?
3. Whaaat?
4. The CSS-in-JS Spectrum
5. What is styled-components?
6. How does it work?
7. What does it generate?
8. React’s Best Friend
today’s
agenda
about me
grayson hicks
front-end developer
greenville roots
Graduated from The Iron Yard in 2016.
ed-tech in asheville
Swiss-army knife developer.
agency life
Front-end developer for Mediacurrent.
@jamesgraysonhicks
grayson.hicks@mediacurrent.com
graysonhicks
@graysonhicks
4
What is
CSS-in-JS?
file based
file based
component based
component based
“CSS-in-JS”
refers to a
pattern where
CSS is composed
using JavaScript.
one CSS-in-JS definition
So stupid.
another CSS-in-JS definition
So stupid.
WTF?!
quick
benefits
scale speed power
dead
code
scope
quick
benefits
scalespeedpowerdead
codescope
Scope
• No more crazy long selectors
• Integration w/ third party code
• Inline/hash-classes
scope
quick
benefits
scalespeedpowerdead
code
Dead Code
• Harness JS to evaluate code
• Smaller bundles
• Browser and component 

specific
dead
codescope
quick
benefits
scalespeedpower
Power
• Functions
• Props
• Height/width
• Parent elements
• True global variables
• Minification
powerdead
codescope
quick
benefits
scalespeed
Speed
• Minimal bundle
• Reusability equals smaller 

bundle
• Requires you to leverage
• Matter of time until matches
• Faster dev times as design 

system with components
speedpowerdead
codescopequick
benefits
scale
Scale
• Grows in direct proportion 

to number of components
• See issues with scope
• See issues with dead code
• Refactoring
all good things

come in twos
{ } < />
object component
all good things

come in twos
{ } < />
object component
object style
• Uses className
• Separate stylesheet
• More typical HTML/CSS/JS feel
all good things

come in twos
{ }
object
< />
component
component style
• Still write CSS rules
• More declarative
• Blurs HTML/CSS/JS lines
the spectrum
components objects
styled-components glamorous emotion aphrodite css-modules
24
What is
styled-
components?
styled-components
“The basic idea of styled-components is to
enforce best practices by removing the
mapping between styles and components.”
• Uses declarative nature of components
• Props instead of classes
• Intrinsically links component and style
• Full CSS support
• Full JS support
styled-components
styled-components
28
How does styled-
components
work?
how
it works
styled
function
{ }
props
< />
tagged
template
literals
``
extend
how
it works
styled
function
{ }
extend
props
< />
tagged
template
literals
``
• New ES6 feature
• It’s a function!
how
it works
styled
function
{ }
extend
props
< />
tagged
template
literals
``
• New ES6 feature
• It’s a function!
• Uses interpolations, or string splitting
• If you pass no interpolations, the first argument your
function receives is an array with one string in it.
how
it works
styled
function
{ }
extend
props
< />
tagged
template
literals
``
• New ES6 feature
• If you pass no interpolations, the first argument your
function receives is an array with a string in it.
• Uses interpolations, or string splitting
• If you do pass interpolations, the array contains the
passed string split at the positions of the
interpolations. The rest of the arguments will be the
interpolations, in order. In this case, interpolation is
string, in SC it’s often a function with props.
tagged
template
literals
``
how
it works
styled
function
{ }
extend
props
< />
tagged
template
literals
``
how
it works
styled
function
{ }
extend
props
< />
props
< />
tagged
template
literals
``
how
it works
styled
function
{ }
extend
• Sometimes too many props gets messy
• Can also use the withComponent() method
to change its underlying HTML element
• Probably changing name in next major
extend
props
< />
tagged
template
literals
``
how
it works
styled
function
{ }
• What if they’re not your components? Or
not styled?
• Look familiar?
37
What does styled-
components
generate?
inline styles

vs.
class names
{ } .abc
inline class names
inline styles

vs.
class names
inline styles
• Generates CSS and attaches via

styles attribute
• No full CSS (media queries, animation,

etc.
.a
clas
{ }
inline
{
i
inline styles

vs.
class names
class names
• Generates style tags with hash class 

names
• Full CSS and faster
.abc
class names
Why all of this makes
styled-components
React’s best friend?
react ❤ styled-components
components declarative tightly coupled
react ❤ styled-components tightly coupleddeclarative
components
• Doesn’t just ‘work with’ components, it’s built for it!
• Makes render() function of your component match
• Even comes with Theme component
• Works React-Native
componentsreact ❤ styled-components tightly coupleddeclarative
• React defines itself by its ‘declarative views’
components
react ❤ styled-components
tightly coupleddeclarative
• React defines itself by its ‘declarative views’
declarative
componentsreact ❤ styled-components
tightly coupled
• A pattern seeing in other React libraries, including data
• React started by tightly coupling markup with logic
react ❤ styled-components
components declarative tightly coupled
True love? Your thoughts and questions!
Styled Components & React.js

Styled Components & React.js