Reactjs 参考主题';s原色,而不是材质UI中的特定颜色

Reactjs 参考主题';s原色,而不是材质UI中的特定颜色,reactjs,material-ui,Reactjs,Material Ui,使用ReactJS和Material UI,我有一个项目,其中我更改了主题颜色 const newTheme = getMuiTheme({ fontFamily: 'Roboto, sans-serif', palette: { primary1Color: cyan500, primary2Color: cyan700, primary3Color: grey400, accent1Color: amberA40

使用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(
        <ThemeProvider theme={newTheme}>
            <Project />
        </ThemeProvider>
    )
  }
const newTheme=getMuiTheme({
fontFamily:“机器人,无衬线”,
调色板:{
原色1:青色500,
原色2:青色700,
原色3:灰色400,
Accent1颜色:amberA400,
Accent2颜色:灰色100,
Accent3颜色:灰色500,
textColor:darkBlack,
alternateTextColor:白色,
画布颜色:白色,
边框颜色:灰色300,
禁用颜色:淡入淡出(黑色,0.3),
pickerHeaderColor:cyan500,
时钟圈颜色:褪色(黑色,0.07),
阴影颜色:全黑,
},
});
//应用程序类
render(){
返回(
)
}
一切正常。某些组件(如按钮)能够基于主道具设置颜色。但是,我有一个组件使用需要原色的图标。我可以导入颜色并直接设置:

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} />
        )
    }
}
从“React”导入React;
从“材质ui/svg图标/action/lock”导入ActionLock;
从“材质ui/样式/颜色”导入{cyan500};
导出默认类LockIcon扩展React.Component{
render(){
返回(
)
}
}
是否有某种方法可以引用主题的原色,而不是单独设置每个组件中的颜色?比如:

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} />
        )
    }
}
从“React”导入React;
从“材质ui/svg图标/action/lock”导入ActionLock;
从“某处”导入{primary1Color};
导出默认类LockIcon扩展React.Component{
render(){
返回(
)
}
}
是的,你有! 使用muitemeable

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)
从“材质ui/样式/muithemable”导入muithemable;
类LockIcon扩展了React.Component{
render(){
返回(

添加如何使用with theme组件访问(当前测试版)中的主题颜色。
同时检查以下各项

import React,{Component}来自'React';
从“材质ui/样式”导入{withTheme};
使用MeeXample扩展组件的类{
render(){
const{theme}=道具;
const{primary,secondary}=theme.palete.text;
返回(
Hi原色
二次色再见
);
}
}
使用Theme()导出默认值(使用ThemeExample);

确定如果您使用的是大于4的material ui版本,则上述解决方案可能不适用于您。请按照下面的代码进行操作

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

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

const DeepChild = withTheme(DeepChildRaw);
从'@materialui/core/styles'导入{withTheme};
功能(道具){
返回{`spating${props.theme.spating}`;
}
const DeepChild=withTheme(DeepChildRaw);

参考资料:

如果您使用的是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} />
}
从'@materialui/core/styles'导入{useTheme};
函数锁定图标=()=>{
const theme=useTheme();
返回(
}

使用react钩子,现在您不需要在中使用主题扭曲组件,只需使用使用主题即可:

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

export default function MyComponent() {
    const theme = useTheme();
    
    return <span>{`spacing ${theme.spacing}`}</span>;
}
从'@materialui/core/styles'导入{useTheme};
导出默认函数MyComponent(){
const theme=useTheme();
返回{`spating${theme.spating}`};
}

道具从何而来的可能副本?“withTheme”组件正在向“WithThemeExample”组件注入颜色
withTheme
需要一个函数,而不是一个
类。原始帖子暗示了类内部的实现。
import { useTheme } from '@material-ui/core/styles';

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