Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use theme in styles for custom class components

I want to use a theme in parts of my own components that are class based. I cannot get anything to work, all examples in the documentation is for functional components. Basically the theme is defined and I want to use it to style my own components so I can avoid repeating myself and change the code at the higher level and it change everywhere.

My App.js

import { ThemeProvider } from '@material-ui/styles';
import { createMuiTheme } from '@material-ui/core/styles';

const theme = createMuiTheme({
    palette: {
        primary: {
            light: '#757ce8',
            main: '#3f50b5',
            dark: '#002884',
            contrastText: '#fff',
          },
    },
    overrides: {
        MuiOutlinedInput: {
            disabled: true,
            input: {
                color: 'red'
            }
        }
    }
});

export default class App extends React.Component {
    render() {
        return (
            <ThemeProvider theme={theme}>
                <Nav />
            </ThemeProvider>
        );
    }
}

My Problem file, Nav.js

import React from 'react';
import SearchBar from './SearchBar';
import { makeStyles } from '@material-ui/styles';

const styles = makeStyles(theme => ({
    root: {
        background: theme.background,
    },
  }));

class Nav extends React.Component {
    render() {
        const classes = styles();
        return(
            <div className={classes.root}>
                <SearchBar />
            </div>
        )
    }
}

export default Nav;
like image 628
Jiroscopes Avatar asked Sep 17 '19 02:09

Jiroscopes


People also ask

How can you apply dynamic styles to styled components?

One way to dynamically change css properties with styled components is to insert a custom prop into your React component and access said property using the dollar sign and curly braces commonly used for template literals. Our current example is testing to see if our use pointer prop is true.

Can you style a styled component?

Styled Components allow you to style any custom component that you've created. First, the custom component must receive the prop className and pass it to the underlying DOM element. Once that is done, pass the custom component to the styled function and invoke it as a Tagged Template to receive a new Styled Component.


1 Answers

You cannot use makeStyles with class components. makeStyles returns a custom hook which can only be used in function components. For class components, you can leverage all of the same functionality using withStyles. withStyles wraps your component and injects a classes prop.

Below is a working example based on the code in your question:

import React from "react";

import { withStyles } from "@material-ui/core/styles";

class SearchBar extends React.Component {
  render() {
    return <div>My Search Bar</div>;
  }
}

const styles = theme => ({
  root: {
    backgroundColor: theme.palette.primary.main,
    color: theme.palette.primary.contrastText
  }
});

class Nav extends React.Component {
  render() {
    return (
      <div className={this.props.classes.root}>
        <SearchBar />
      </div>
    );
  }
}
export default withStyles(styles)(Nav);

Edit withStyles example

Related answers:

  • What is the benefit of using withStyles over makeStyles?
  • Material-UI withStyles doesn't apply any kind of styles
like image 140
Ryan Cogswell Avatar answered Nov 15 '22 03:11

Ryan Cogswell