热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

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

如何解决《如何在样式化组件中访问Material-ui的主题》经验,请帮忙看看怎么搞?

我正在将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 (
    
        
); } export default App;

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


推荐阅读
author-avatar
仙走壹步
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有