Is it possible to override material-ui components default props?

不问归期 提交于 2019-12-23 12:38:30

问题


Lets say i want every Button component from material-ui to have a default props of variant="contained" color="secondary" is it possible ?


回答1:


The documentation for this is here: https://material-ui.com/customization/globals/#default-props

Here is an example of how to do this:

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);




回答2:


Create your own component <MyButton /> which renders a <Button> component from material-ui. Style the component as you wish and use MyButton in your code.




回答3:


If I understood the question correctly, you have to set that when you instantiate your theme :

const theme = createMuiTheme({
  overrides: {
    MuiButton: {
      root: {
        fontSize: '1rem',
      },
    },
  },
});


来源:https://stackoverflow.com/questions/57359439/is-it-possible-to-override-material-ui-components-default-props

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!