未捕获的TypeError:无法读取未定义的属性'push'(React-Router-Dom) [英] Uncaught TypeError: Cannot read property 'push' of undefined (React-Router-Dom)
问题描述
我有一个带有旋转幻灯片的信息中心,每个幻灯片都在 Bldgs 中有相应的标签。 Dashboard.js
和 Bldgs.js
都是我的 App.js $的子项c $ c>。
I have a Dashboard with rotating slides, each of which has a corresponding tab in Bldgs. Both Dashboard.js
and Bldgs.js
are children to my App.js
.
当用户点击仪表板中的特定幻灯片
, A
时.js 仪表板
需要告诉 App.js
以便 App
可以告诉 Bldgs.js
在路由到<时显示标签 A
code> Bldgs 。
When a user clicks on a specific slide A
in Dashboard.js
, Dashboard
needs to tell App.js
so that App
can tell Bldgs.js
to have tab A
displayed when it routes to Bldgs
.
我相信我从<$ c传递了正确的索引值$ c>信息中心最高应用
,最低为 Bldgs
。但是,在我的 App.js
文件中引发错误,说明:
I believe that I am passing the correct index value from Dashboard
up to App
and down to Bldgs
. However, an error is being thrown in my App.js
file stating:
Uncaught TypeError:无法读取未定义的属性'push'
在我开始传递 handleClick之前,我的代码工作正常)
函数到我的仪表板
组件。
My code was working fine before I started passing my handleClick()
function to my Dashboard
component.
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
一起使用。只有当你的组件没有收到路径
,
In order to make use of history
in the App
component use it with withRouter
. You need to make use of withRouter
only when your component is not receiving the Router props
,
如果您的组件是由路由器呈现的组件的嵌套子项,或者您没有通过路由器道具,那么可能会发生这种情况或当组件未完全链接到路由器时,将其作为路径中的单独组件呈现。
This may happen in cases when your component is a nested child of a component rendered by the Router or you haven't passed the Router props to it or when the component is not linked to the Router at all and is rendered as a separate component from the 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);
文档 on withRouter
Documentation on withRouter
这篇关于未捕获的TypeError:无法读取未定义的属性'push'(React-Router-Dom)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!