-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cannot use react-router Link with ListItem in TypeScript #9106
Comments
It's something we have already been discussing in #8063 (comment). |
@oliviertassinari ops, I haven't searched well enough 😄 But just to be clear, I should be using: <ListItem component={props => <Link {...props} to="/about" />}>
// ...
</ListItem> Right? At least that's the only variation that works for me without throwing any TS compilation errors. |
@rolandjitsu Yes, I would encourage using this pattern. |
@oliviertassinari thanks, I've just started with React and at this point all is a little confusing 😆 |
I think @rolandjitsu 's solution is the only way it could work becouse the |
@rolandjitsu This soultion no longer work in @types/react 16.0.36 version. :( |
@activebiz I'll give that a try tomorrow and see if it still works for me. |
It seems when ListItem's innerRef is passed to Link, Link will not work properly. Try this:
|
I wrote my own component for that which seams to work fine and is reasonably type safe: import * as React from 'react'
import { ListItem } from '@material-ui/core';
import { Link } from 'react-router-dom';
import { ListItemProps } from '@material-ui/core/ListItem';
import { LocationDescriptor } from 'history';
interface Props extends ListItemProps {
// ListItemProps and LinkProps both define an 'innerRef' property
// which are incompatible. Therefore the props `to` and `replace` are
// simply duplicated here.
to: LocationDescriptor
replace?: boolean
}
function createLink({innerRef, ...props}: Props) {
// Remove `innerRef` from properties as the interface
// is incompatible. The property `innerRef` should not be
// needed as the `ListItem` component already provides that
// feature with a different interface.
return <Link {...props}/>
}
export class ListItemLink extends React.PureComponent<Props> {
render() {
return <ListItem {...this.props} component={createLink}/>
}
} I tried to write it as type safe as possible. That's the reason for a dedicated Usage: <ListItemLink to="/about">... </ListItemLink> This solution is more or less identical to the one suggested by @adimitris but also adds some extra type safety (due to |
The ripple effect seems to not be working when using @adimitris solution. Any idea why? I'm trying to figure out. |
@Nelrohd You need to create the link component outside of the render method. If you don't, the component reference will change at each render. React will consider it's a new component, unmount and remount it. You loose the ripple. |
Same thing with So far I found this: <Tab label="Local" {...{component: Link, to: `/local`} as any}/> |
Still an issue in 2020 |
Expected Behavior
I should be able to somehow use a
<ListItem>
with aLink
in TypeScript.Current Behavior
I've found a few examples that suggest using:
But I use TypeScript, and using
to
attr throws a compilation error sinceto
is not a property ofListItem
.Steps to Reproduce (for bugs)
create-react-app my-app --scripts-version=react-scripts-ts
to create a simple react app with TSreact-router
as dep and install itList
in the app template and try to add an item with<ListItem button component={Link} to="/somelink"></ListItem>
Context
I'm trying to setup Material UI with react-router.
Your Environment
The text was updated successfully, but these errors were encountered: