javascript - 未捕获的类型错误 : Cannot read property 'push' of undefined (React-Router-Dom)

标签 javascript reactjs react-router react-router-dom

我有一个带有旋转幻灯片的仪表板,每个幻灯片在Bldgs中都有一个相应的选项卡。 Dashboard.jsBldgs.js 都是我的 App.js 的子级。

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

相信我将正确的索引值从Dashboard传递到App并向下传递到Bldgs 。但是,我的 App.js 文件中抛出了一个错误,指出:

未捕获类型错误:无法读取未定义的属性“push”

在我开始将 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
// http://stackoverflow.com/a/34015469/988941
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

当您的组件是 Router 渲染的组件的嵌套子组件或您尚未将 Router 属性传递给它时,可能会发生这种情况>当组件根本没有链接到路由器并且被渲染为与路由分离的组件时。

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 上

关于javascript - 未捕获的类型错误 : Cannot read property 'push' of undefined (React-Router-Dom),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44009618/

相关文章:

reactjs - Next.js 在 router.push 时保持状态或发送 props

javascript - 如何在 Javascript 中使用 Moment js 将 ISO 8601 解析为日期和时间格式?

reactjs - 如何使用 __mocks__ 目录中的 jest 来模拟 React-router-dom

javascript - 如何从 React-Router-Dom 停止对 Link 元素的传播?

reactjs - React Router NavLink不触发history.listen()函数

javascript - Safari 返回值 "normal"为 "line-height"样式

javascript - $.get 调用外部的变量值尚未设置

javascript - ThreeJS动画几何体不更新但位置会更新?

javascript - 为什么 draggable 不适用于动态创建的元素?

javascript - 如何清除/重置react.js表单中的输入数据?