Material-UI's Tabs integration with react router 4?

后端 未结 9 2054
一个人的身影
一个人的身影 2020-12-24 10:58

The new react-router syntax uses the Link component to move around the routes. But how could this be integrated with material-ui?

In my cas

相关标签:
9条回答
  • 2020-12-24 11:28
     <BrowserRouter>
    <div className={classes.root}>
      <AppBar position="static" color="default">
        <Tabs
          value={this.state.value}
          onChange={this.handleChange}
          indicatorColor="primary"
          textColor="primary"
          fullWidth
        >
          <Tab label="Item One" component={Link} to="/one" />
          <Tab label="Item Two" component={Link} to="/two" />
        </Tabs>
      </AppBar>
    
      <Switch>
        <Route path="/one" component={PageShell(ItemOne)} />
        <Route path="/two" component={PageShell(ItemTwo)} />
      </Switch>
    </div>
    
    0 讨论(0)
  • 2020-12-24 11:29

    The error you are seeing from material-ui is because it expects to have a <Tab> component rendered as direct child of <Tabs> component.

    Now, here is a way that I've found to integrate the link into the <Tabs> component without loosing the styles:

    import React, {Component} from 'react';
    import {Tabs, Tab} from 'material-ui/Tabs';
    import {Link} from 'react-router-dom';
    
    export default class MyComponent extends Component {
        render() {
            const {location} = this.props;
            const {pathname} = location;
    
            return (
                <Tabs value={pathname}>
                    <Tab label="First tab" containerElement={<Link to="/my-firs-tab-view" />} value="/my-firs-tab-view">
                        {/* insert your component to be rendered inside the tab here */}
                    </Tab>
                    <Tab label="Second tab" containerElement={<Link to="/my-second-tab-view" />} value="/my-second-tab-view">
                        {/* insert your component to be rendered inside the tab here */}
                    </Tab>
                </Tabs>
            );
        }
    }
    

    To manage the 'active' property for the tabs, you can use the value property in the <Tabs> component and you also need to have a value property for each tab, so when both of the properties match, it will apply the active style to that tab.

    0 讨论(0)
  • 2020-12-24 11:29

    You can use browserHistory instead of React-Router Link component

    import { browserHistory } from 'react-router'
    
    // Go to /some/path.
    onClick(label) {
      browserHistory.push('/${label}');
    }
    
    // Example for Go back
    //browserHistory.goBack()
    
    <Tabs>
      <Tab
        label={label}
        onActive={() => onClick(label)}
      />
    </Tabs>
    

    As you see you can simply push() your target to the browserHistory

    0 讨论(0)
提交回复
热议问题