在Material UI中引用主题的主要颜色而不是特定颜色

时间:2017-04-07 14:45:32

标签: reactjs material-ui

使用ReactJS和Material UI,我有一个项目,我在其中更改了主题颜色。

const newTheme = getMuiTheme({
    fontFamily: 'Roboto, sans-serif',
    palette: {
        primary1Color: cyan500,
        primary2Color: cyan700,
        primary3Color: grey400,
        accent1Color: amberA400,
        accent2Color: grey100,
        accent3Color: grey500,
        textColor: darkBlack,
        alternateTextColor: white,
        canvasColor: white,
        borderColor: grey300,
        disabledColor: fade(darkBlack, 0.3),
        pickerHeaderColor: cyan500,
        clockCircleColor: fade(darkBlack, 0.07),
        shadowColor: fullBlack,
    },
});

  // App class
  render() {
    return(
        <MuiThemeProvider muiTheme={newTheme}>
            <Project />
        </MuiThemeProvider>
    )
  }

一切都按预期工作。某些组件(如按钮)可以根据主要道具设置颜色。但是,我有一个使用需要主色的图标的组件。我可以导入颜色并直接设置它:

import React from 'react';
import ActionLock from 'material-ui/svg-icons/action/lock';
import {cyan500} from 'material-ui/styles/colors';

export default class LockIcon extends React.Component {
    render() {
        return(
            <ActionLock color={cyan500} />
        )
    }
}

有没有办法引用主题的主要颜色,而不是单独设置每个组件的颜色?类似的东西:

import React from 'react';
import ActionLock from 'material-ui/svg-icons/action/lock';
import {primary1Color} from 'somewhere';

export default class LockIcon extends React.Component {
    render() {
        return(
            <ActionLock color={primary1Color} />
        )
    }
}

5 个答案:

答案 0 :(得分:11)

如果您使用的是React v16.8.0和Material-UI v3.5.0或更高版本,则可以使用useTheme()钩子:

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

function LockIcon = () => {
  const theme = useTheme();

  return (
    <ActionLock color={theme.palette.primary1Color} />
}

答案 1 :(得分:10)

是的,你有! 使用muiThemeable ..

import muiThemeable from 'material-ui/styles/muiThemeable';
class LockIcon extends React.Component {
    render() {
        return(
            <ActionLock color={this.props.muiTheme.palette.primary1Color} />
        )
    }
}
        export default muiThemeable()(LockIcon)

from material-ui docs

答案 2 :(得分:10)

添加如何访问material-ui v1.0.0(目前为测试版)中的主题颜色 withTheme 组件。
另请查看以下Example

import React, {Component} from 'react';
import { withTheme } from 'material-ui/styles';

class WithThemeExample extends Component {
    render() {
        const { theme } = props;
        const {primary, secondary} = theme.palette.text;

        return (
            <div>
                <div style={{color: primary}}>Hi in Primary color</div>
                <div style={{color: secondary}}>Bye in Secondary color</div>
            </div>
        );
    }
}

export default withTheme()(WithThemeExample);

答案 3 :(得分:5)

好吧,如果您使用的Material-ui版本大于4,则上述解决方案可能对您不起作用。请遵循以下代码

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

function DeepChildRaw(props) {
  return <span>{`spacing ${props.theme.spacing}`}</span>;
}

const DeepChild = withTheme(DeepChildRaw);

参考:https://material-ui.com/styles/advanced/

答案 4 :(得分:2)

有了react钩子,现在您不需要在 withTheme 中扭曲组件,只需使用 useTheme

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

export default function MyComponent() {
    const theme = useTheme();
    
    return <span>{`spacing ${theme.spacing}`}</span>;
}