【问题标题】:Apply theme to material UI menu component将主题应用于材质 UI 菜单组件
【发布时间】:2020-02-14 14:30:46
【问题描述】:

我有下一个代码,用于应用自定义主题:

import Menu from '@material-ui/core/Menu';
import { createStyles, withStyles, Theme } from '@material-ui/core/styles';

const myMenu = withStyles( ( theme: Theme ) =>
    createStyles( {
        root: {
            backgroundColor: theme.palette.primary.main,
        },
    } ),
)( Menu );

但我收到下一个错误:

Argument of type 'ComponentType<MenuProps>' is not assignable to parameter of type 'ComponentType<ConsistentWith<MenuProps, { classes: Record<"root", string>; }> | ConsistentWith<PropsWithChildren<MenuProps>, { ...; }>>'.
  Type 'ComponentClass<MenuProps, any>' is not assignable to type 'ComponentType<ConsistentWith<MenuProps, { classes: Record<"root", string>; }> | ConsistentWith<PropsWithChildren<MenuProps>, { ...; }>>'.
    Type 'ComponentClass<MenuProps, any>' is not assignable to type 'ComponentClass<ConsistentWith<MenuProps, { classes: Record<"root", string>; }> | ConsistentWith<PropsWithChildren<MenuProps>, { ...; }>, any>'.
      Types of property 'propTypes' are incompatible.
        Type 'WeakValidationMap<MenuProps> | undefined' is not assignable to type 

【问题讨论】:

    标签: typescript material-ui


    【解决方案1】:

    您必须将(theme: Theme) =&gt; 移动到createStyles 函数中。像这样:

    import Menu from '@material-ui/core/Menu';
    import { createStyles, withStyles, Theme } from '@material-ui/core/styles';
    
    const myMenu = withStyles(createStyles((theme: Theme) => {
            root: {
                backgroundColor: theme.palette.primary.main,
            },
        }),
    )( Menu );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 2015-11-13
      • 1970-01-01
      • 2017-09-20
      相关资源
      最近更新 更多