Uncaught TypeError: Cannot read property 'push' of undefined (React-Router-Dom)

Uncaught TypeError: Cannot read property 'push' of undefined (React-Router-Dom)

我有一个带有旋转幻灯片的 Dashboard,每个幻灯片在 Bldgs 中都有一个对应的选项卡。 Dashboard.jsBldgs.js 都是 children 我的 App.js.

当用户点击 Dashboard.js 中的特定幻灯片 A 时,Dashboard 需要告诉 App.js 以便 App 可以告诉 Bldgs.js 在路由到 Bldgs.

时显示选项卡 A

相信 我正在传递从 DashboardAppBldgs 的正确索引值。但是,我的 App.js 文件中出现了一个错误,指出:

Uncaught TypeError: Cannot read property 'push' of undefined

在我开始将 handleClick() 函数传递给我的 Dashboard 组件之前,我的代码运行良好。

Index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.css';
import injectTapEventPlugin from 'react-tap-event-plugin';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import { BrowserRouter as Router } from 'react-router-dom';
import { hashHistory } from 'react-router';

// Needed for onTouchTap
// 
injectTapEventPlugin();

ReactDOM.render(
  <MuiThemeProvider>
    <Router history={hashHistory}>
      <App />
    </Router>
  </MuiThemeProvider>,
  document.getElementById('root')
);

App.js

import React from 'react';
import { Route } from 'react-router-dom';
import Dashboard from './Dashboard';
import Bldgs from './Bldgs';

var selectedTab;

class App extends React.Component {
  constructor(props) {
    super(props);
    this.handleClick = this.handleClick.bind(this);
    selectedTab = 0;
  }

  handleClick(value) {
    selectedTab = value;
    // console.log(selectedTab);
    this.props.history.push('/Bldgs');
    // console.log(this.props);
  }

  render() {
    var _this = this;

    return (
      <div>
        <Route exact path="/" render={(props) => <Dashboard {...props} handleClick={_this.handleClick} />} />
        <Route path="/Bldgs" component={Bldgs} curTab={selectedTab} />
      </div>
    );
  }
}

export default App;

Dashboard.js

import React, { Component } from 'react';
import './Dashboard.css';
import { AutoRotatingCarousel, Slide } from 'material-auto-rotating-carousel';
...

var curIndex;

class Dashboard extends Component {
  constructor(props) {
    super(props);
    this.handleEnter = this.handleEnter.bind(this);
    this.handleChange = this.handleChange.bind(this);
    curIndex = 0;
  }

  handleEnter(e) {
    // console.log(curIndex);
    this.props.handleClick(curIndex);
  }

  handleChange(value) {
    // console.log(value);
    curIndex = value;
  }

...
}

export default Dashboard;

Bldgs.js

...
var curTab;

class Bldgs extends Component {
  constructor(props) {
    super(props);
    this.handleChange = this.handleChange.bind(this);
    this.goHome = this.goHome.bind(this);
    curTab = 0;
  }

  handleChange(value) {
    this.setState({'selectedTab': value});
    console.log(this.state);
  }

  goHome(e) {
    this.props.history.push('/');
  }

...
}

export default Bldgs;

为了在 App 组件中使用 history,请将其与 withRouter 一起使用。仅当您的组件未收到 Router props

时,您才需要使用 withRouter

当您的组件是路由器呈现的组件的嵌套子组件时,可能会发生这种情况或者您还没有将路由器道具传递给它 当组件根本没有链接到路由器时 并且作为独立于路由的组件呈现。

import React from 'react';
import { Route , withRouter} from 'react-router-dom';
import Dashboard from './Dashboard';
import Bldgs from './Bldgs';

var selectedTab;

class App extends React.Component {
  constructor(props) {
    super(props);
    this.handleClick = this.handleClick.bind(this);
    selectedTab = 0;
  }

  handleClick(value) {
    selectedTab = value;
    // console.log(selectedTab);
    this.props.history.push('/Bldgs');
    // console.log(this.props);
  }

  render() {
    var _this = this;

    return (
      <div>
        <Route exact path="/" render={(props) => <Dashboard {...props} handleClick={_this.handleClick} />} />
        <Route path="/Bldgs" component={Bldgs} curTab={selectedTab} />
      </div>
    );
  }
}

export default withRouter(App);

Documentation 在 withRouter

对于 React-router V4 将函数更改为

onClick={this.fun.bind(this)}

fun() {
  this.props.history.push("/Home");
}

import { withRouter } from 'react-router-dom';

稍后导出为:

export default withRouter (comp_name);

您正试图在不将任务交给有效库的情况下进行推送。在 App.js

所以: 在 App.js

您正在使用默认处理页面历史记录的 BrowserRouter,使用此 react-router-dom 函数,您依赖 BrowserRouter 为您执行此操作。

使用 Router 而不是 BrowserRouter 来控制你的历史,使用历史来控制行为。

  1. 使用 npm 历史记录 "yarn add history@4.x.x" / "npm i history@4.x.x"
  2. 从'react-router-dom'导入路由; //不要使用 BrowserRouter
  3. 从 'createBrowserHistory' 导入 createBrowserHistory;

记得导出!! export const history = createBrowserHistory();

4.import 历史到 Dashboard.js 5.import 历史到 Bldgs.js

希望这对您有所帮助!!! @brunomiyamotto

请参阅下面我的 App.js 文件。我最终将 {...this.props} 传递给了我创建的 NavBar 组件。 NavBar 组件不是我的 Switch 路由的一部分。

import React, { Component } from 'react';
import { Route, Link, Redirect, Switch, withRouter } from 'react-router-dom'

import Navbar from './Navbar.js';
import Home from './Home/HomeCont';
import Login from './Register/Login';
import Dashboard from './Dashboard/DashboardCont';


import './App.css';

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

  }

  render() {
    return (
      <div className="App">
        <header className="App-header">

//SOLUTION!!!
          <Navbar {...this.props}/>


        </header>
        <Switch>
          <React.Fragment>
            <Route exact path="/" render={(props) => <Home {...props}/>}/>

            <Route exact path="/login" render={(props) => <Login {...props}/>}/>


          </React.Fragment>


        </Switch>
      </div>
    );
  }
}

export default withRouter(App);

在我的导航栏中。我使用的按钮具有以下代码。我必须 .bind(this) 才能查看历史记录并能够使用 this.props.history.push("/")

<Button
   color="inherit"
   onClick={this.handleLogout.bind(this)}>Logout</Button>

在使用功能组件时,我们可以使用 useHistory() 将历史推送到方法

import { useHistory } from "react-router-dom";

然后在函数中我们必须分配 useHistory()

  let history = useHistory();

现在我们可以使用history.push重新定位到想要的页面

history.push('/asdfg')

我的错误是与 BrowserRouter 相关的错误导入,即:

不正确:

import { useHistory } from 'react-router'

正确:

import { useHistory } from 'react-router-dom'

我通过将组件包装在 BrowserRouter 中解决了这个错误。
不要忘记这一点,这是一个很常见的错误。

import { BrowserRouter} from 'react-router-dom';

<BrowserRouter>
    <Menu/>
<BrowserRouter>    

只有路由器子节点接收历史挂钩。