【发布时间】:2018-09-12 21:40:10
【问题描述】:
我正在尝试在 Material UI 选项卡中实现最新版本的 React Router (v.4.3.1),但收到以下错误:TypeError: Cannot read property 'location' of undefined。我相信我需要使用某种位置历史记录,但由于 React Router 的更新,我不确定实现。
更新
我已设法修复未定义问题的位置并使用BrowseRouter as Router 显示导航栏和内容,但现在导航栏链接未显示在导航栏中。没有任何逻辑可以显示或隐藏链接,所以不确定是什么导致它们不显示。
App.js
import React, { Component } from "react";
import Main from './Main';
import { Router, Route, Switch, Link } from 'react-router-dom';
class App extends Component {
constructor() {
super();
this.state = {
appTitle: "App"
};
}
componentDidMount() {
document.title = this.state.appTitle;
}
render() {
return (
<ThemeProvider theme={lightTheme}>
<Router>
<Switch>
<Route component={Main} exact path="/" />
</Switch>
</Router>
</ThemeProvider>
);
}
}
export default App;
Main.js
import React from 'react';
import PropTypes from 'prop-types';
import { NavBar } from "./Components/Navbar/";
const GetNavBar = props => {
return (<NavBar appTitle={props.appTitle} />);
}
const Main = props => {
return (
<div className={props.classes.root}>
<GetNavBar appTitle={props.appTitle} { ...data.appHeader } />
<GetPageComponents {...props} data={data}/>
</div>
)};
Main.propTypes = {
onClose: PropTypes.func
};
export default withStyles(styles)(Main);
Navbar.js
import React from "react";
import { Tabs, Tab } from 'tabs';
import PropTypes from 'prop-types';
import { Router } from 'react-router-dom';
const TabLink = linkProps => (
<a {...linkProps}>{linkProps.linklabel}</a>
);
function renderTab({ getTabProps }) {
const { className, label, ...rest } = getTabProps();
return (
<Tab
className={`${className}`}
component={TabLink}
linklabel={label}
to={TabLink}
{...rest}
/>
);
}
renderTab.propTypes = {
getTabProps: PropTypes.func
};
const NavBar = ({onChange, onDeselect, classes}, props) => {
return (
<div className={styles.headerContainer}>
<AppHeader
data-testid="app-header-default-example"
position="static"
className={styles.root}
getTabProps={({ getTabProps }) => (
<div {...getTabProps({})}>
<Router>
<Tabs>
<Tab label="Home" component={renderTab} exact to="/" />
<p className={styles.headerAppTitle}>{props.appTitle} .
</p>
</Tabs>
</Router>
</div>
)}
/>
</div>
)};
NavBar.propTypes = {
onChange: PropTypes.func,
onDeselect: PropTypes.func
};
export default withStyles(styles)(NavBar);
【问题讨论】:
-
此代码块中的任何内容均未引用位置。我们可以看到
Main组件吗?
标签: reactjs react-router