【问题标题】:How to pass prop to custom root component in material-ui@next when using TypeScript?使用 TypeScript 时如何将 prop 传递给 material-ui@next 中的自定义根组件?
【发布时间】:2018-09-21 19:47:54
【问题描述】:

我想用 react-router Link 渲染一个 ListItem 组件作为根组件,如下所示:

        <ListItem
          to="/profile"
          component={Link}
        >
          Profile
        </ListItem>

但是,我收到 TypeScript 错误:

(50,15): error TS2339: Property 'to' does not exist on type 'IntrinsicAttributes & ListItemProps & { children?: ReactNode; }'.

显然,Link 组件使用的to 属性是ListItem 属性接口的一部分...

如何解决?

【问题讨论】:

    标签: reactjs typescript react-router material-ui


    【解决方案1】:

    TypeScript 不会拒绝使用 prop-spread 语法传递的额外 props,因此这是可行的:

        <ListItem {...{to: "/profile"}} component={Link}>
          Profile
        </ListItem>
    

    【讨论】:

      猜你喜欢
      • 2021-05-19
      • 2020-09-07
      • 2021-09-11
      • 2021-12-07
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      • 2018-09-25
      • 2020-10-07
      相关资源
      最近更新 更多