Material-ui adding Link component from react-router

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

    So my work-around for this solution has been quite reliable, though it may be more manual of a solution than what you're looking to do.

    The strategy that I've been using is to actually not even use the Link Component. Instead, you'll utilize the Tabs onChange property as a callback that can respond to Tab clicks, and track location manually with Props on the Parent.

    You can import a utility called History from react-router that will allow you to manually push locations. While using React-Router, your component tree will have access to Location prop that has a pathname key with the string of your current location.

    We will manually parse this string into the components that make up your current URL, then use a Switch statement to decide both which tab is currently selected and also where to link to when a tab is clicked. (This gives you a fair amount of control over navigation)

    ( e.g. ['', 'latest'] )

    Here is a mock up of what your component MAY look like after integrating this solution.

    import React from 'react';
    import {History} from 'react-router';
    
    function parseLocation(location) {
        if (String(location)) {
            var locationArray = location.split('/');
            return locationArray;
        } else {
            return false;
        }
    };
    function filterPath(path) {
        let locationArray = parseLocation(path);
        return locationArray[locationArray.length - 1];
    };
    var Navigation = React.createClass({
          mixins: [History],
          getPage() {
            if (this.props.location.pathname) {
              let pathname = this.props.location.pathname;
              let pageName = filterPath(pathname);
              return pageName;
            } else {
              return false;
            } 
          },
          decideContent() {
            let page = this.getPage();
            let content;
            switch(page) {
               case 'popular':
                  content = 0;
               case 'latest':
                  content = 1;
               case 'myideas':
                  content = 2;
               default:
                  content = 0;
            }
            return content;
          },
          handleTabChange(value) {
            let location = false;
            switch (value) {
               case 0:
                 location = 'popular';
                 break;
               case 1:
                 location = 'latest';
                 break;
               case 2:
                 location = 'myideas';
                 break;
            }
            if (location && location !== this.getPage()) {
              this.history.pushState(null, '/'+location);
            }
          },
          render() {
             var styles = {
              appBar: {
               flexWrap: 'wrap'
              },
              tabs: {
               width: '100%'
              }
             };
             let content = this.decideContent();
             let tabs = 
                      
                      
                      
                    ;
            return (
             
               {tabs}
             
            );
          }
    });
    

提交回复
热议问题