让我们说我希望Button
中的每个material-ui
组件都有默认的variant="contained" color="secondary"
道具吗?
答案 0 :(得分:3)
有关此文档,请参见:https://material-ui.com/customization/globals/#default-props
以下是如何执行此操作的示例:
import React from "react";
import ReactDOM from "react-dom";
import {createMuiTheme, MuiThemeProvider, Button} from "@material-ui/core";
const theme = createMuiTheme({
props: {
MuiButton: {
variant: "contained",
color: "secondary"
}
}
});
function App() {
return (
<MuiThemeProvider theme={theme}>
<Button>Props defaulted</Button>
</MuiThemeProvider>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
答案 1 :(得分:1)
创建自己的组件<MyButton />
,该组件将渲染<Button>
中的material-ui
组件。根据需要对组件进行样式设置,并在代码中使用MyButton
。
答案 2 :(得分:1)
如果我正确理解了这个问题,则必须在实例化主题时进行设置:
const theme = createMuiTheme({
overrides: {
MuiButton: {
root: {
fontSize: '1rem',
},
},
},
});