Material-ui adding Link component from react-router

后端 未结 9 2015
一个人的身影
一个人的身影 2020-12-01 01:51

I\'m struggling to add component to my material-ui AppBar

This is my navigation class:

class Navigation extends Component          


        
9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 02:46

    Since we are using TypeScript I could not use @hazardous solutions. This is how we implemented routing for material-ui v1.0.0-beta.16 and react-router 4.2.0. The reason why we are splitting this.props.history.location.pathname is because we need to access /renewals/123 for example. If we did not do this we would get the following warning and no tab would be displayed as active: Warning: Material-UI: the value provided '/renewals/123' is invalid

    Complete code with imports:

    import * as React from "react";
    import * as ReactDOM from "react-dom";
    import * as ReactRouter from "react-router";
    import * as PropTypes from "prop-types";
    import { Switch, Route, Redirect, Link  } from "react-router-dom";
    import { Cases } from './../Cases';
    import { SidePane } from './../SidePane';
    import { withStyles, WithStyles } from 'material-ui/styles';
    import Paper from 'material-ui/Paper';
    import Tabs, { Tab } from 'material-ui/Tabs';
    import { withRouter } from "react-router-dom";
    import Badge from 'material-ui/Badge';
    import Grid from 'material-ui/Grid';
    import { Theme } from 'material-ui/styles';
    import SimpleLineIcons from '../../Shared/SimpleLineIcons'
    
    interface IState {
        userName: string;
    }
    
    interface IProps {
        history?: any
    }
    
    const styles = (theme: Theme) => ({
        root: theme.typography.display1,
        badge: {
            right: '-28px',
            color: theme.palette.common.white,
        },
        imageStyle:{
            float: 'left',
            height: '40px',
            paddingTop: '10px'
        },
        myAccount: {
            float: 'right'
        },
        topMenuAccount: {
            marginLeft: '0.5em',
            cursor: 'pointer'
        }
    });
    
    type WithStyleProps = 'root' | 'badge' | 'imageStyle' | 'myAccount' | 'topMenuAccount';
    
    class Menu extends React.Component, IState> {
        constructor(props: IProps & WithStyles) {
            super(props);
            this.state = {
                userName: localStorage.userName ? 'userName ' + localStorage.userName : ""
            }
        }
        componentDidMount() {
            this.setState({ userName: localStorage.userName ? localStorage.userName : "" })
        }
        logout(event: any) {
            localStorage.removeItem('token');
            window.location.href = "/"
        }
    
        handleChange = (event: any, value: any) => {
            this.props.history.push(value);
        };
    
        render() {
            const classes = this.props.classes;
            let route = '/' + this.props.history.location.pathname.split('/')[1];
            return (
                
    Logo
    MY ACCOUNT
    Renewals } value="/renewals" />
    ); } } export default withStyles(styles)(withRouter(Menu))

提交回复
热议问题