Using a Link component with ListItem and Typescript

谁说胖子不能爱 提交于 2019-12-12 12:13:29

问题


I'm using material-ui v3.5.1

I want to make ListItem use the Link component like so:

<ListItem component={Link} to="/some/path">
  <ListItemText primary="Text" />
</ListItem>

but Typescript greets me with a lengthy error message (the word "component" is highlighted in VSCode), at the bottom it says:

The type "typeof Link" cannot be assigned to the type "ComponentClass<ListItemProps, any>"

Property 'to' is missing in type 'ListItemProps' but required in type 'Readonly'. [2322]

Is there a workaround to get things like these to work with Typescript?

Thanks!


回答1:


That is currently a limitation of our type declarations (until we move to generic props). As a temporary workaround you can extract your link into another component e.g.

function SomePathLink(props: ButtonBaseProps) {
  return <Link to="/some/path" {...props} />
}

<ListItem component={SomePathLink}>
  <ListItemText primary="Text" />
</ListItem>

More detailed explanation in the docs: https://material-ui.com/demos/buttons/#third-party-routing-library



来源:https://stackoverflow.com/questions/53375964/using-a-link-component-with-listitem-and-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!