私はBldgsに対応するタブをそれぞれ有する回転スライドとダッシュボードを、持っています。 Dashboard.js
とBldgs.js
はともにApp.js
の子です。ユーザーはApp
がBldgs
にするとき、それはルートタブA
が表示持っているBldgs.js
を伝えることができるようにApp.js
を伝えるために必要Dashboard
Dashboard.js
内の特定のスライドA
、クリックキャッチされない例外TypeError:プロパティを読み取ることができません未定義の「プッシュ」(リアクト - ルータ - ドム)
。
私はApp
までとBldgs
までDashboard
から正しいインデックス値を渡していを信じ。私は私のDashboard
コンポーネントに私のhandleClick()
機能を渡す開始する前に私のコードは正常に動作していた
Uncaught TypeError: Cannot read property 'push' of undefined
:しかし、エラーが述べ私App.js
ファイルでスローされています。
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 withRouter
で使用App
成分でhistory
を利用するために
...
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;
https://stackoverflow.com/questions/42123261/programmatically-navigate-using-react-router-v4/42124328#42124328これが答えを持っていますあなたが必要です。 –