Material-ui adding Link component from react-router

后端 未结 9 1993
一个人的身影
一个人的身影 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:43

    here's how you can do it now:

    <Tabs onChange={this.changeTab} value={value}>
       <Tab value={0} label="first" containerElement={<Link to="/first"/>} />
       <Tab value={1} label="second" containerElement={<Link to="/second"/>}/>
       <Tab value={2} label="third" containerElement={<Link to="/third"/>} />
     </Tabs>
    
    0 讨论(0)
  • 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<IProps & WithStyles<WithStyleProps>, IState> {
        constructor(props: IProps & WithStyles<WithStyleProps>) {
            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 (
                <div>
                    <Grid container spacing={24}>
                        <Grid item xs={12} className={classes.root}>
                            <img src="/Features/Client/Menu/logo.png" alt="Logo" className={classes.imageStyle} />
                            <div className={this.props.classes.myAccount}>
                            <span><span className={this.props.classes.topMenuAccount}>MY ACCOUNT</span><span className={classes.topMenuAccount}><SimpleLineIcons iconName={'user'} />&#x25BE;</span></span>
                                <span onClick={this.logout} className={classes.topMenuAccount}><SimpleLineIcons iconName={'logout'} /></span>
                            </div>
                        </Grid>
                        <Grid item xs={12} >
                            <div className="route-list">
                                <Tabs
                                    value={route}
                                    onChange={this.handleChange}
                                    indicatorColor="primary"
                                    textColor="primary"
                                >
                                    <Tab label="Overview" value="/" />
                                    <Tab label={<Badge classes={{ badge: classes.badge }} badgeContent={this.props.caseRenewalCount} color="primary">
                                        Renewals
                                       </Badge>} value="/renewals" />
                                </Tabs>
                            </div>
                        </Grid>
                    </Grid>
                </div>
            );
        }
    }
    export default withStyles(styles)(withRouter(Menu))
    
    0 讨论(0)
  • 2020-12-01 02:46

    Here's another implementation of React with hooks, Material-UI with tabs, React Router with Link, and TypeScript.

    import * as React from "react";
    import { BrowserRouter as Router, Route, Redirect, Switch, Link, LinkProps } from 'react-router-dom';
    import AppBar from '@material-ui/core/AppBar';
    import Tabs from '@material-ui/core/Tabs';
    import { default as Tab, TabProps } from '@material-ui/core/Tab';
    
    import Home from './Home';
    import ProductManagement from './ProductManagement';
    import Development from './Development';
    import HomeIcon from '@material-ui/icons/Home';
    import CodeIcon from '@material-ui/icons/Code';
    import TimelineIcon from '@material-ui/icons/Timeline';
    
    const LinkTab: React.ComponentType<TabProps & LinkProps> = Tab as React.ComponentType<TabProps & LinkProps>;
    
    function NavBar() {
      const [value, setValue] = React.useState(0);
    
      const handleChange = (event: React.ChangeEvent<{}>, newValue: number) => {
        setValue(newValue);
      };
      return (
        <div >
          <AppBar position="static" >
            <Tabs value={value} onChange={handleChange} centered>
              <LinkTab label='Home' icon={ <HomeIcon />} component={Link} to="/" />
              <LinkTab label='Development' icon={<CodeIcon />} component={Link} to="/dev" />
              <LinkTab label='Product Management' icon={<TimelineIcon />} component={Link} to="/pm" />
            </Tabs>
          </AppBar>
        </div>
      )
    };
    
    export default function App() {
      return (
        <Router>
          <div>
            <NavBar />
            <Switch>
              <Route exact path="/" component={ Home } />
              <Route exact path="/dev" component={ Development } />
              <Route exact path="/pm" component={ ProductManagement } />
              <Redirect from="/" to="/" />
            </Switch>
          </div>
        </Router>
      )
    }
    
    0 讨论(0)
提交回复
热议问题