【发布时间】:2020-04-10 07:23:38
【问题描述】:
我是 React 和 Material UI 的新手,我正在尝试使用 createMuiTheme 向 MUI 按钮添加一些自定义样式。
我已经按照文档并几乎复制了该示例,但它没有任何效果,并且控制台中没有抛出任何错误。
我一直在努力解决这个问题,但我看不出问题出在哪里,我错过了什么?
import React from 'react';
import { createMuiTheme } from '@material-ui/core/styles';
import { ThemeProvider } from '@material-ui/styles';
import Button from '@material-ui/core/Button';
const mytheme = createMuiTheme({
palette: {
primary: { main: '#1565C0'},
secondary: { main: '#11cb5f' },
},
});
export const PrimaryButton = (props) => {
return (
<ThemeProvider theme={mytheme}>
<a href={props.buttonLink}>
<Button
style={{ ...props.styles}}
onClick={props.handleClick}
variant='contained' color='primary' size='large'
>
{props.buttonText}
</Button>
</a>
</ThemeProvider>
);
};
export const SecondaryButton = (props) => {
return (
<ThemeProvider theme={mytheme}>
<Button
style={{...props.styles }}
value={props.value || ''}
onClick={props.handleClick}
variant='outlined' color='secondary' size='large'
>
{props.buttonText}
</Button>
</ThemeProvider>
)
}
【问题讨论】:
-
效果很好,我试过codesandbox.io/s/flamboyant-wu-b8kqg。我改变了原色,它可以工作
-
嘿,谢谢你的设置。我可以看到它在那里工作。问题是默认样式仍在应用中,并且在我的自定义样式之后应用,因此过度编写它们,我不确定为什么会发生这种情况..
-
您在控制台中是否有任何关于
@material-ui/styles多个实例的警告? -
没有控制台警告,下面的答案已经解决了,我需要将整个应用程序包装在 ThemeProvider 中,并全局设置样式而不是每个组件。
标签: reactjs material-ui