Styling React Components With Aphrodite

Jamison Dance
InstructorJamison Dance
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 5 years ago

Aphrodite is a library styling React components.

You get all the benefits of inline styles (encapsulation, no build step, no CSS cascade, building up styling with JavaScript instead of a preprocessor language) with all the benefits of CSS (animations and transitions, pseudo-classes, media queries).

Learn how to use Aphrodite to style reusable React components.

[00:00] Here's a simple react button component that we can click on. It's pretty ugly. Let's add some inline styles by creating this style object, and then we'll pass it in as style on our button. Now it's prettier, and it still works the same. Also because we're defining inline styles, they come along for free when anyone uses this component.

[00:29] Now let's add in Aphrodite by importing StyleSheet and css from the aphrodite package. We take our styles object and wrap it in a call, so StyleSheet.create. We also add one level nesting to our styles object. Call everything button. Instead of assigning things to style, we'll use className.

[01:02] We call our css function (styles.button). When we save, everything looks the exact same. If we inspect our element though, we notice that now we have a class instead of inline styles. Also if we look up in the head we see this data-aphrodite style tag, which contains styles that look suspiciously like what we defined here.

[01:32] That's because when you call css and pass in a StyleSheet object, Aphrodite is parsing this code, turning it into css, and injecting it into the DOM. It also uses the object key to generate a unique class name, that it then shares and passes in as the className attribute.

[01:54] Let's use css we already know, to add an active and a hover state to this button. We first define an active pseudo class, to add a boxShadow when someone's clicking on the button. It's beautiful. Next we'll add a hover pseudo class, so that when someone hovers on the button it turns colors.

[02:14] Our button is a little easier to interact with now, and we get all the benefits of css like pseudo classes, along with the benefits of inline styles like encapsulation and avoiding selector wars and specificity problems.

egghead
egghead
~ 2 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today