将主题应用于材质UI菜单组件

时间:2020-02-14 14:30:46

标签: typescript material-ui

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

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 

1 个答案:

答案 0 :(得分:0)

您必须在(theme: Theme) =>函数内部移动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 );