Uncaught TypeError:无法读取未定义的属性“ push”(React-Router-Dom)

发布于 2021-01-31 23:03:43

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

当用户单击特定的幻灯片ADashboard.jsDashboard需要告诉App.js以便App告诉它在路由到时显示Bldgs.js选项卡。A``Bldgs

相信 ,我从传递正确的索引值Dashboard达到App和向下Bldgs。但是,我的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
// 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;
关注者
0
被浏览
93
1 个回答
  • 面试哥
    面试哥 2021-01-31
    为面试而生,有面试问题,就找面试哥。

    为了historyApp组件中使用,请与配合使用withRouterwithRouter仅当您的组件未收到时,才需要使用Router props

    如果您的组件是 由路由器渲染的 组件的 嵌套子代, 或者 您尚未将路由器道具传递给它, 或者 该组件根本未链接到路由器
    ,并且作为独立于 该组件 的组件呈现,则 可能会发生这种情况路线。

    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



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看