如何在样式化组件中访问Material-ui的主题

时间:2019-07-19 08:38:44

标签: reactjs material-ui styled-components

我将CRA与Material-ui和Styled Components类型的样式一起使用。 构建CSS时,我想访问Material-ui的默认主题。

package.json的一部分:

  "dependencies": {
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-scripts": "3.0.1",
    "@material-ui/core": "^4.2.1",
    "@material-ui/icons": "^4.2.1",
    "@material-ui/styles": "^4.2.1",
    "styled-components": "^4.3.2"
  }

当我尝试以下操作时,theme上的props存在,但为空对象。

StyledApp.js:

import styled from "styled-components";
import Button from "@material-ui/core/Button";

export const StyledButtonUsingTheme = styled(Button)`
  //Below will give "Cannot read property 'error' of undefined" 
  background-color: ${props => props.theme.palette.error.light};
`;

App.js:

import React from "react";
import "./App.css";

import { StylesProvider, ThemeProvider } from "@material-ui/styles";
import { createMuiTheme } from "@material-ui/core/styles";

import { StyledButtonUsingTheme } from "./StyledApp";

function App() {
  const defaultTheme = createMuiTheme();

  window.console.log("Default theme passing to ThemeProvider", defaultTheme);

  return (
    <StylesProvider injectFirst>
      <ThemeProvider theme={defaultTheme}>
        <div className="App">
          <StyledButtonUsingTheme variant="outlined">
            Styled Button Using Theme
          </StyledButtonUsingTheme>
        </div>
      </ThemeProvider>
    </StylesProvider>
  );
}

export default App;

App.js中的console.log显示了整个主题对象,这就是我传递给ThemesProvider的内容。有趣的是props.theme在那里!但可惜没有价值。

6 个答案:

答案 0 :(得分:2)

问题已解决!

解决方案是使用: 在App.js中import { ThemeProvider } from "styled-components";,然后theme就在props对象上,所有值都在其中。

我在App.js中使用了“ @ material-ui / styles”中的ThemeProvider import { StylesProvider, ThemeProvider } from "@material-ui/styles"; 这与StyledApp.js中的“从“样式组件”中导入样式的导入方式”效果不佳

两个工作文件:

App.js

import React from "react";
import "./App.css";

import { StylesProvider } from "@material-ui/styles";
import { ThemeProvider } from "styled-components";
import { createMuiTheme } from "@material-ui/core/styles";

import { StyledButtonUsingTheme } from "./StyledApp";

function App() {
  const defaultTheme = createMuiTheme();

  window.console.log("Default theme passing to ThemeProvider", defaultTheme);

  return (
    <StylesProvider injectFirst>
      <ThemeProvider theme={defaultTheme}>
        <div className="App">
          <StyledButtonUsingTheme variant="outlined">
            Styled Button Using Theme
          </StyledButtonUsingTheme>
        </div>
      </ThemeProvider>
    </StylesProvider>
  );
}

export default App;

StyledApp.js

import styled from "styled-components";
import Button from "@material-ui/core/Button";

export const StyledButtonUsingTheme = styled(Button)`
  //Below will work now!
  background-color: ${props => props.theme.palette.error.light};
`;

答案 1 :(得分:2)

与主题一起使用的答案几乎是完整的。最后一部分对我不起作用,所以我更改为:

import styled from 'styled-components'
import { withTheme } from "@material-ui/core/styles"

const StyledButton = withTheme(styled('h1')`
    background-color: ${props => props.theme.palette.error.light};
  `
)

答案 2 :(得分:1)

如评论中的Horyd所述,使用ThemeProvider中的Styled-Components将使您可以访问样式化组件内的主题属性。 但是Material-UI不再将该主题应用于其自己的组件。

我发现的解决方法非常简单,很难看:同时使用两个Themeproviders 。因此Material-UI将主题应用于其组件,您可以在样式化的组件中访问主题。

import { ThemeProvider } from "styled-components";
import { MuiThemeProvider,StylesProvider } from "@material-ui/core/styles";

ReactDOM.render(

  //Make sure the Material stylesheet is placed above your own 
  //styles so you can overwrite them
  <StylesProvider injectFirst> 

    //Use the theme in the ThemeProvider for Material-UI so
    //styles are applied to the Material-UI components
    <MuiThemeProvider theme={theme}>

      //Use also the ThemeProvider for Styled-Components so 
      //you can access the theme in your own css
      <ThemeProvider theme={theme}>

        //Include your app and you have acces to everything 
        <App />

      </ThemeProvider>

    </MuiThemeProvider>

  </StylesProvider>,

document.getElementById("app"));

答案 3 :(得分:0)

您可以使用 withTheme

App.js

import React from "react"
import { ThemeProvider, createMuiTheme } from "@material-ui/core/styles"
import { StyledButton } from "./StyledButton"

const App = () => {

    const theme = createMuiTheme();

    return (
        <ThemeProvider theme={theme}>
            <StyledButton />
        </ThemeProvider>
    )
}

export default App

StyledButton.js

import { styled, withTheme } from "@material-ui/core/styles"
import Button from "@material-ui/core/Button"

export const StyledButton= styled(withTheme(Button))(props => ({
  background: props.theme.palette.background.paper,
}))

答案 4 :(得分:0)

如果您想同时使用两个 ThemeProviders,首先来自 styled-components,其次来自 material-ui,您可以为其中一个使用 alias

import { ThemeProvider as StyledThemeProvider} from 'styled-components';
import { ThemeProvider } from '@material-ui/core/styles';

 function App() {
  return (
    <Router>
      <ThemeProvider theme={theme}>
      <StyledThemeProvider theme={theme}>
        <Wrapper>
          <TheHeader />
          <TheContent />
          <TheFooter />
        </Wrapper>
      </StyledThemeProvider>
      </ThemeProvider>
    </Router >
  );
}

export default App;

答案 5 :(得分:0)

要访问 Material ui 主题,请使用“useTheme”钩子

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

要访问样式化组件主题,请使用“useTheme”钩子

import { useTheme } from 'styled-components';