反应this.context.router未定义

时间:2019-04-22 14:35:16

标签: reactjs material-ui

在我的主页组件中,未定义this.context.router。我正在使用以下依赖项:

"@material-ui/core": "^3.9.3",
"@material-ui/icons": "^3.0.2",
"history": "^4.9.0",
"moment": "^2.24.0",
"prop-types": "^15.7.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.0",
"react-scripts": "2.1.8",
"react-swipeable-views": "^0.13.1"

我的index.js是:

import React from 'react';
import ReactDOM from 'react-dom';
import {Switch, Router, Route, BrowserRouter } from 'react-router-dom';
import {MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import * as serviceWorker from './serviceWorker';
import UserApp from 'components/App.js';
ReactDOM.render((
                  <MuiThemeProvider theme={createMuiTheme({typography: { useNextVariants: true, },})}>
                    <BrowserRouter>
                      <Switch>
                          <Route path='/' component={UserApp} />                    
                      </Switch>   
                    </BrowserRouter>
                  </MuiThemeProvider>
                ), document.getElementById('root'));

serviceWorker.register();

components / App.js的内容是:

import React, { Component } from 'react';
import Header from 'components/layout/Header.js';
import Footer from 'components/layout/Footer.js';
import Main from 'components/layout/Main.js';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      navOpen: false
    };
  }
  openLeftNav=()=>{}
  render() {
    return (
      <div className="site-app">
        <Header {...this.props} openLeftNav={this.openLeftNav} />
        <Main />
        <Footer />
      </div>
    );
  }
}

export default App;

主要是我们的内部应用程序路由文件。 Main.js的结构为:

import React from 'react';
import { Switch, Route } from 'react-router-dom';
import HomePage from 'components/app/account/Home.js';

const Main = () => (
  <main>
    <Switch>
      <Route exact path='/' component={HomePage}/>
   </Switch>
  </main>
);   
export default Main;

我的Home.js文件是:

import React from 'react';
import PropTypes from 'prop-types';
import classNames from "classnames";
import withStyles from "@material-ui/core/styles/withStyles";
import landingPageStyle from "assets/jss/landingPage.jsx";

    class Home extends React.Component {
          constructor(props, context) {
                super(props, context);
                this.state = {
                      expanded: null,
                }
          }
          componentDidMount() {}
          testFunction=()=>{
               console.log(this.context);
               this.context.router.history.push('/test-page');
          }

       render() {
        return (
               <div>
                <Button color="primary" onClick={this.testFunction}>Click Me</Button>
              </div>

            )
         }
    }

    Home.contextTypes = {
          router: PropTypes.object.isRequired
    };
    export default withStyles(landingPageStyle)(Home);

我想使用this.context.route有条件地进行导航,有人可以帮助我为什么this.context.router未定义吗? 此代码以前可以与早期版本的react [v16.3] react-router-dom [v4.2.2]和material-ui [v1.1.0]一起使用,但现在不起作用。

1 个答案:

答案 0 :(得分:1)

我也发生了同样的事情,我开始使用由react-router-dom提供的Link wich

import { Link } from 'react-router-dom';
import Button from '@material-ui/core/Button';

<Button component={Link} to="/test-page">
  About
</Button>

例如,另一个解决方案是注入历史记录

...
import { withRouter } from 'react-router-dom';
class Home extends React.Component {
    testFunction=()=> this.props.history.push('/test-page');
    ...
}
export default withStyles(landingPageStyle)(withRouter(Home));