【问题标题】:How to use Material-UI withMobileDialog HOC in TypeScript?如何在 TypeScript 中使用 Material-UI with MobileDialog HOC?
【发布时间】:2019-04-06 08:50:36
【问题描述】:

我在 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)

我在这里做错了什么?提前致谢!

【问题讨论】:

    标签: reactjs typescript material-ui


    【解决方案1】:

    这是@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);
    

    【讨论】:

    • 感谢您的回复,我已经尝试过了,但它不起作用,我得到了与问题中相同的错误。
    • 我需要查看您正在使用的版本的完整示例。它对我有用。
    猜你喜欢
    • 2020-04-09
    • 2019-03-27
    • 2018-11-20
    • 2017-04-30
    • 2019-01-29
    • 1970-01-01
    • 2021-10-06
    • 2020-07-20
    • 2020-07-22
    相关资源
    最近更新 更多