Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Render Material-UI Components with Next.js

    Thomas GrecoThomas Greco

    This lesson demonstrates how to add Material-UI components to our project by creating a Header component that we can share throughout our app.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: Inside our root directory, I have this components folder which is just going to hold all our reusable components. In here, I'm going to create this header.js file which as its name suggests, it's going to hold our header component.

    Inside here, I'm just going to import the app bar component from the Material-UI library. As you could see, I'm just using this header constant and passing in our title prop. Immediately follow this, I'm just returning the app bar component that will import in from Material-UI. I'm giving the app bar's title prop, the value of title, should renders that string that were passing in above.

    The last thing I'm going to do is set the show menu icon button to false, so it doesn't render the icon button for us. Now that, our header is ready to go. We can export this as the default export and use inside our index.js file. Inside index.js, we firstly import the header that we just created.

    Once imported, I'm just going to place this ripple our opening div. In order for all this to work, we also need to import our higher order component from the shared MUI folder. Once imported, we then need to pass in our index constant to this with MUI function in order inject Material-UI in your application.

    If we go back to our page and refresh, we should see everything, and looks like a soon beginning error here. Specifically, we see that the compose component.getInitialProps is not a function. By taking out this error message, we see that it's telling us exactly where this error is happening.

    Even if I wasn't sure where this is going on, next would really provide us with helpful error messages here. To fix this issue, I'm just going to comment out this sub props, props defined within a higher order component which is the reason for this error. This is happening, because our index constant isn't utilizing next getInitialProps lifecycle hook.

    Now that, we have our sub props come in to route them, we can return the page and we'll see our app bar being rendered for us. Now, we also know there are app bar has a height of 50 as specify within our getMuiTheme function. Our application is set up to use any in all of the components from Material-UI library.