【问题标题】:How to use Material UI custom theme in React with Typescript如何在 React with Typescript 中使用 Material UI 自定义主题
【发布时间】:2020-04-09 10:25:27
【问题描述】:

使用这个工具https://react-theming.github.io/create-mui-theme/ 我得到了一个 js 文件和一个 json 文件,如上述主题生成器页面所述,在各自的路径中:

// src/ui/theme/index.js
/* src/ui/theme/theme.json */

现在,当我将文件扩展名留给 js 时,它们可以正常工作。一旦我尝试使用 js 作为 tsx 文件,编辑器就会开始抱怨。我还通过 tsconfig 文件中的 CRA Typescript 完成了所有必要的设置。此页面还需要配置https://material-ui.com/guides/typescript/

当我尝试这个时它不起作用,如果我遗漏了什么,有什么想法吗?

// My amended index.tsx file

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

const palette = {
  primary: { main: '#3f51b5' },
  secondary: { main: '#f50057' }
};
const themeName = 'San Marino Razzmatazz Sugar Gliders';

export default createMuiTheme({ palette, themeName });

theme.json 也没有被我触及。 对不起,我还在学习,如果这是一个界面问题以及如何使用它,有什么想法吗? 谢谢!

【问题讨论】:

    标签: javascript reactjs typescript material-ui create-react-app


    【解决方案1】:

    Material-UI 已经定义了类型声明,因此您不能只为其添加额外的属性。您必须通过模块扩充来扩展接口:

    import { createMuiTheme } from '@material-ui/core/styles';
    
    declare module '@material-ui/core/styles/createMuiTheme' {
        interface ThemeOptions {    
            themeName?: string  // optional
        }
    }
    
    const palette = {
      primary: { main: '#3f51b5' },
      secondary: { main: '#f50057' }
    };
    
    const themeName = 'San Marino Razzmatazz Sugar Gliders';
    
    export default createMuiTheme({ palette, themeName });
    

    【讨论】:

    • 感谢您的快速回复。它工作得很好。我不会知道这一点,因为在带有 TS 的 MUI 文档上找不到自定义主题的示例。有很多使用这种语法的例子,但我不会在我的解决方案中实现这些例子。所以谢谢你!
    • 不客气!如果您认为它可以解决问题,您能否将其标记为已接受 :) 您还可以在typescriptlang.org/docs/handbook/typescript-in-5-minutes.html 学习大量 TS
    • 是的,但它是这样写的 :( 感谢您的反馈!声望低于 15 人的投票会被记录下来,但不要更改公开显示的帖子分数。
    【解决方案2】:

    @kasperoo 写的是什么,而是让它更通用,因为输入样式是很多工作的方式:

    // imported theme from separate file
    import { themeDetails } from './utils/theme';
    
    declare module '@material-ui/core/styles/createMuiTheme' {
            interface ThemeOptions {
              [key: string]: any; // 
            }
        }
    const theme = createMuiTheme({ themeDetails, 'theme name' });
    

    【讨论】:

    【解决方案3】:

    Material UI 自定义主题 V5

    React with Typescript v4->v5 迁移指南中的 Material UI 自定义主题。为声明创建单独的文件。

    ?theme.d.ts

    import { Theme, ThemeOptions } from '@mui/material/styles';
    
    declare module '@mui/material/styles' {
      interface CustomTheme extends Theme {
        status: {
          danger: string;
        };
      }
      // allow configuration using `createTheme`
      interface CustomThemeOptions extends ThemeOptions {
        status?: {
          danger?: string;
        };
      }
      export function createTheme(options?: CustomThemeOptions): CustomTheme;
    }
    

    它将使用自定义主题配置覆盖默认的createTheme 函数。现在您可以在主题中使用自定义配置,如下所述?。

    ?theme.ts

    import { createTheme } from '@mui/material/styles';
    import { orange, red } from '@mui/material/colors';
    
    const theme = createTheme({
      status: {
        danger: red[500],
      },
      palette: {
        primary: {
          main: orange[500],
        },
      },
    });
    
    export default theme;
    

    【讨论】:

      猜你喜欢
      • 2021-11-14
      • 2019-05-24
      • 2019-07-15
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      相关资源
      最近更新 更多