带Rails的Reactjs,删除重复的createMuiTheme

时间:2018-08-04 17:15:44

标签: ruby-on-rails reactjs material-ui webpacker react-rails

以下代码是我的组件之一。 我正在使用Ruby on Rails框架,react_rails gem和webpacker创建此对象,并在Material UI上进行了实验。

如您所见,我正在用自己选择的字体更改Material UI默认字体主题。下面的代码是成功的。

我的问题是,我是否必须对所有组件重复此步骤? 导入createMuiTheme,说明主题const,并在每个渲染中包装<MuiThemeProvider />吗?

有没有一种通用的方法可以做到这一点,而无需在所有组件中重复进行? 感谢您的建议。

import React from 'react';
import PropTypes from 'prop-types';
import Card from '@material-ui/core/Card';
import CardActions from '@material-ui/core/CardActions';
import CardContent from '@material-ui/core/CardContent';
import Button from '@material-ui/core/Button';
import Popover from '@material-ui/core/Popover';
import Typography from '@material-ui/core/Typography';
import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import ListItemText from '@material-ui/core/ListItemText';
import Avatar from '@material-ui/core/Avatar';
import EmailIcon from '@material-ui/icons/Email';
import HomeIcon from '@material-ui/icons/Home';
import PersonIcon from '@material-ui/icons/Person';
import { MuiThemeProvider, createMuiTheme, withStyles } from '@material-ui/core/styles';


const theme = createMuiTheme({
  typography: {
    fontFamily: 'Bebas',
  },
});


export class SimpleCard extends React.Component {

render () {
 return (

<div >
 <MuiThemeProvider theme={theme}>
  <Card raised="true">
    <CardContent >
    <List>
    <ListItem>
      <Avatar>
        <EmailIcon />
      </Avatar>
      <ListItemText primary="Email" secondary={this.props.order.order_mail} />
    </ListItem>
    </List>
    </CardContent>
  </Card>
 </MuiThemeProvider>
</div>

  );
}
}


export default withStyles(styles)(SimpleCard);

2 个答案:

答案 0 :(得分:0)

您是否尝试将MuiThemeProvider包装在整个网站/应用程序周围?这就是我在React.js中所做的。我在根文件中设置了主题,并将其包装在整个组件中

答案 1 :(得分:0)

import React, { Component } from "react";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";

// Components
import Navbar from "./components/layout/Navbar";
import Footer from "./components/layout/Footer";
import Login from "./components/auth/Login";
import Dashboard from "./components/dashboard/Dashboard";


// Styles
import "./stylesheets/App.css";
import {
  MuiThemeProvider,
  createMuiTheme,
  withTheme
} from "@material-ui/core/styles";
import { grey } from "@material-ui/core/colors";
import { withStyles } from "@material-ui/core";

const theme = createMuiTheme({
  overrides: {
    MuiGrid: {
      container: {
        width: "100%",
        margin: "0"
      }
    }
  },
  palette: {
    primary: {
      light: "#c146b1",
      main: "#8e0081",
      dark: "#5c0054",
      contrastText: "#ffffff"
    },
    secondary: {
      light: "#6bffff",
      main: "#00eae3",
      dark: "#00b7b1",
      contrastText: "#000000"
    }
  }
});

const drawerWidth = 240;

const styles = theme => ({
  app: {
    backgroundColor: grey[200]
  },
  drawerOpen: {
    marginLeft: 0
  },
  drawerClosed: {
    marginLeft: -drawerWidth
  }
});


class App extends Component {
  constructor() {
    super();
    this.state = {
      navOpen: false
    };
  }
  toggleDrawer = () => {
    this.setState({
      navOpen: !this.state.navOpen
    });
  };
  render() {
    const { classes } = this.props;
    return (
        <MuiThemeProvider theme={theme}>
            <div className={classes.app}>
              <Navbar
                toggleDrawer={this.toggleDrawer}
                navOpen={this.state.navOpen}
              />
              <Route exact path="/" component={Dashboard} />
              <Route exact path="/register" component={PatientRegister} />
              <Route exact path="/login" component={Login} />
              <Footer />
            </div>
          </Router>
        </MuiThemeProvider>
    );
  }
}

export default withTheme(theme)(withStyles(styles)(App));

这是我的组件的一个示例,该组件将在root div(又称为整个应用程序)中呈现。请注意如何包装整个应用程序?为了简化起见,我进行了很多介绍,但是如果您使用Redux(真棒),那么我建议您将其作为外部包装,而将其作为内部包装。换句话说:

<Provider store={store}>
  <MuiThemeProvider theme={theme}>
    <div class="App">
      // Your App Here
    </div>
  </MuiThemeProvider>
</Provider>