【问题标题】:How to properly implement a sidebar with react router?如何正确实现带有反应路由器的侧边栏?
【发布时间】:2019-06-12 04:03:04
【问题描述】:

我正在尝试使用 react 和 react 路由器构建一个主从视图,此尝试是试图从创意 tim 中模仿 material-dashboard-react,这是我的实际代码:

/* eslint-disable */
import React from "react";
import PropTypes from "prop-types";
import { Switch, Route, Redirect } from "react-router-dom";
import withStyles from "@material-ui/core/styles/withStyles";

import dashboardRoutes from "./routes/test.js";

import dashboardStyle from "./jss/styles";
import Sidebar from "./components/sidebar.jsx"

import image from "./image/sidebar-2.jpg";
import logo from "./logo.svg";

const switchRoutes = (
  <Switch>
    {dashboardRoutes.map((prop, key) => {
      return <Route path={prop.path} component={prop.component} key={key} />;
    })}
  </Switch>
);

class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      mobileOpen: false
    };    
  }

  handleDrawerToggle = () => {
    this.setState({ mobileOpen: !this.state.mobileOpen });
  };  

  render() {
    const { classes, ...rest } = this.props;
    return (
        <div>
          <Sidebar
            routes={dashboardRoutes}
            logoText={"Davidsito"}
            logo={'https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/1000px-React-icon.svg.png'}
            image={image}
            handleDrawerToggle={this.handleDrawerToggle}
            open={this.state.mobileOpen}
            color="blue"
            {...rest}
          />
          <div className={classes.mainPanel} ref="mainPanel">
            {console.log(dashboardRoutes[3])}
            <div className={classes.content}>
              <div className={classes.container}>
                <Route 
                  path={dashboardRoutes[3].path} 
                  component={dashboardRoutes[3].component} 
                  key={1} 
                />;
              </div>
            </div>            
          </div>          
        </div>
    );
  }
}

App.propTypes = {
  classes: PropTypes.object.isRequired
};

export default withStyles(dashboardStyle)(App);

所以行为应该是我选择一些东西并在侧边栏旁边呈现如下:

but when select doesn't render properly, What should I do?

【问题讨论】:

    标签: reactjs react-router material-ui


    【解决方案1】:

    确保您在 index.js 中使用 BrowserRouter 包装了 App 组件,例如:

    ReactDOM.render(<BrowserRouter><App/></BrowserRouter>, document.getElementById('root'));
    

    【讨论】:

    • 是的,我确定我有它,我不明白为什么我会出现这种行为。感谢您的评论
    猜你喜欢
    • 2013-03-13
    • 2020-07-23
    • 2023-03-03
    • 2022-01-06
    • 2020-07-29
    • 2019-04-04
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多