Convert ListItem compatible with react-router
See original GitHub issueI want to make each ListItem
in a List
to a Link
component.
I have a Drawer
component composed of a List
component,
That List is composed of 4 ListItem
s.
What I want to achieve is that convert each ListItem
to a React router Link
.
I tried to modify drawer’s onClick method, get Id of the clicked ListItem and then programmatically change location of window based on the id.
But I do think this approach is more jqueryish that the react concepts.
I also tried to add Link
to component
prop in ListItem
, but it resulted error.( I’ve seen you have used a in component
prop in docs).
What is the recommended way to achieve this requirement?
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles';
import Drawer from 'material-ui/Drawer';
import List, { ListItem, ListItemIcon, ListItemText } from 'material-ui/List';
import Face from 'material-ui-icons/Face';
import Person from 'material-ui-icons/Person';
import Assignment from 'material-ui-icons/Assignment';
import Link from 'react-router-dom/Link';
import { FormattedMessage } from 'react-intl';
const styles = {
list: {
width: 250,
flex: 'initial',
},
};
class UndockedDrawer extends Component {
constructor() {
super();
this.onClick = this.onClick.bind(this);
}
onClick(e, data) {
console.log(data);
this.props.onRequestClose();
}
render() {
const classes = this.props.classes;
const sidebarListItems = (
<div>
<ListItem button >
<ListItemIcon>
<Person />
</ListItemIcon>
<ListItemText primary={<FormattedMessage id="user" />} />
</ListItem>
<ListItem button>
<ListItemIcon>
<Assignment />
</ListItemIcon>
<ListItemText primary={<FormattedMessage id="consultant" />} />
</ListItem>
<ListItem button>
<ListItemIcon>
<Face />
</ListItemIcon>
<ListItemText primary={<FormattedMessage id="student" />} />
</ListItem>
</div>
);
const sidebarList = (
<div>
<List className={classes.list} >
{sidebarListItems}
</List>
</div>
);
return (
<div>
<Drawer
anchor="right"
open={this.props.open}
onRequestClose={this.props.onRequestClose}
onClick={this.onClick}
>
{sidebarList}
</Drawer>
</div>
);
}
}
UndockedDrawer.propTypes = {
classes: PropTypes.shape({}).isRequired,
open: PropTypes.bool.isRequired,
onRequestClose: PropTypes.func.isRequired,
};
export default withStyles(styles)(UndockedDrawer);
- Material-UI: 1.0.6-beta
- React: ^15.6.1
- Browser: Chrome: 60
- react-router-dom : ^4.1.2
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:21 (9 by maintainers)
Top Results From Across the Web
Convert ListItem compatible with react-router #7956 - GitHub
I want to make each ListItem in a List to a Link component. I have a Drawer component composed of a List component,...
Read more >react-router-dom v6 NavLink and MUI ListItem not working ...
Issue. The issue is that the className prop is for the ListItem , not the component you've specified to be rendered. It's not...
Read more >Upgrading from v5 v6.6.1 - React Router
In order to use v6, you'll need to convert all your <Switch> elements to <Routes> . If you already made the upgrade to...
Read more >Migrating to React Router v6: A complete guide
Migrate your React Router applications from v5 to v6 with this in-depth guide, including a review of additions and improvements from v5.
Read more >How To Handle Routing in React Apps with React Router
React Router lets you split out child routes directly in the component, which means you don't need to have the whole list in...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is covered in the documentation: https://material-ui.com/components/buttons/#third-party-routing-library.
Here is the solution that solved my issue
Thus just use ListItem as <ListItem key={index} {…{ to: menu.link }} component={Link} button={true} >