开发者问题收集

未捕获的类型错误:无法读取未定义的属性“push”(React-Router-Dom)

2017-05-16
138983

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

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

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

未捕获的 TypeError:无法读取未定义的属性“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;
3个回答

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

当您的组件是 Router 渲染的组件的嵌套子组件 您未将 Router props 传递给它 组件未链接到 Router 并且作为与 Routes 分开的组件渲染时,可能会发生这种情况。

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);

文档 关于 withRouter

Shubham Khatri
2017-05-16

对于 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);
Parishilan Rayamajhi
2017-09-07

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

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

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

  let history = useHistory();

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

history.push('/asdfg')
Aaquib
2020-10-30