如何在TypeScript中将Material-UI与MobileDialog HOC一起使用?

时间:2019-04-06 08:50:36

标签: reactjs typescript material-ui

我在TypeScript中使用withMobileDialog遇到麻烦。

这是我的代码,基于代码示例in the docs

import withMobileDialog, { InjectedProps } from "@material-ui/core/withMobileDialog";

class MyComponent extends React.Component<
  MyComponentProps & InjectedProps, MyComponentState> { 
    ... 
}

export default withMobileDialog()(MyComponent);

我收到此错误:

Argument of type 'typeof MyComponent' is not assignable to parameter of type 'ComponentType<InjectedProps & Partial<WithWidth>>'.
  Type 'typeof MyComponent' is not assignable to type 'ComponentClass<InjectedProps & Partial<WithWidth>, any>'.
    Types of parameters 'props' and 'props' are incompatible. ts(2345)

我在这里做错了什么?预先感谢!

1 个答案:

答案 0 :(得分:0)

这是@material-ui/core类型的问题。我们还注入了width道具。以下代码应通过:

import { WithWidth } from "@material-ui/core/withWidth";
import withMobileDialog, { InjectedProps } from "@material-ui/core/withMobileDialog";

class MyComponent extends React.Component<
  MyComponentProps & InjectedProps & Partial<WithWidth>, MyComponentState> { 
    ... 
}

export default withMobileDialog()(MyComponent);