Use Material-UI's withStyles Higher-Order Component to Customize Styles in a React App

Thomas Greco
InstructorThomas Greco
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 3 years ago

In this lesson we explore the withStyles Higher-Order Component. First, we'll walk through the process of connecting our a React component to the withStyles HOC. Once connected, we'll then see how we can access custom styles using the classes prop withStyles gives us access to.

Material-UI Homepage Material-UI Style API Docs - withStyles HOC

Instructor: [00:01] First thing we're going to want to do here is import the withStyles function from the material-ui/styles library.

[00:12] Now since withStyles is a higher-order component, we need to wrap our Demo component with it. As you see, Demo is the second set of parameters. The first set of parameters is going to be our styles.

[00:29] Now that we are aware of this is going to work, let's import our styles by just saying import styles from styles.shared. Now we can pass it into our higher-order component. Our props is now going to give us access to a classes property. Now classes is going to hold all of our style properties, which in our case are named root and button.

[00:58] To render our div with our root property values, we'll just pass in props.classes.root to React className prop. All right, now I am just going to uncomment this HOCDemo and add it to our page. Once we reload, we'll see that our box is appearing and we're just one step away from replicating yet another button, not just any button though.

[01:27] We're going to use this button import from material-ui. Again, we're going to add a className prop, but this time we're going to target button instead of root. The file structure, I am just going to add "hocs are the pattern for me!"

[01:51] If we reload, we'll see our button appears in the bottom. If we click, we get that ripple effect. Additionally, we can add the variant prop and make this a contained button, and see how it responses to that.

[02:17] The last thing we'll do here is just target the size prop and give it a value of small. We'll see that our button now appears on one line here.

[02:29] One thing to keep in mind is we can actually access classes directly by just restructuring it and then now we'll no longer have the chain props on there. We'll see we get the same result.

egghead
egghead
~ 18 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