【问题标题】:How does one use the component prop with custom components?如何将 component 属性与自定义组件一起使用?
【发布时间】:2020-05-14 20:49:54
【问题描述】:

我正在尝试将 Link 组件(来自 react-router)与我的自定义按钮组件一起使用。 我不确定我做错了什么:

<Button component={Link} to="/">
"This works"
</Button>

<CustomButton component={Link} to="/">
"And this doesn't"
</CustomButton>

CustomButton 是一个简单的 withStyles - 组件:

const CustomButton = withStyles((theme: Theme) => ({
    root: {
        marginLeft: '0.5rem'
        /* ...etc ... */
    },
}))((props: ButtonProps) => <Button color="default" {...props} variant="contained" />);

export default CustomButton;

我收到类型错误:

“IntrinsicAttributes & Pick”、“button”>、“children”类型上不存在属性“component” | "参考" | “表格” | “插槽” | ...

沙盒:https://codesandbox.io/s/fervent-borg-bqize

它本身确实有效,但是来自 linter 的类型错误不允许我编译它(不抑制 linter)

【问题讨论】:

标签: reactjs typescript material-ui


【解决方案1】:

试试

const CustomButton = withStyles({
  root: {
    marginLeft: '0.5rem'
  }
})(Button);

【讨论】:

    猜你喜欢
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 2018-08-07
    • 2011-07-23
    • 1970-01-01
    • 2021-04-07
    • 1970-01-01
    相关资源
    最近更新 更多