我有一个带有旋转幻灯片的 仪表板 ,每个幻灯片在 Bldgs中 都有一个对应的选项卡。两者Dashboard.js并Bldgs.js有孩子我App.js。
Dashboard.js
Bldgs.js
App.js
当用户单击特定的幻灯片A时Dashboard.js,Dashboard需要告诉App.js以便App告诉它在路由到时显示Bldgs.js选项卡。A``Bldgs
A
Dashboard
App
A``Bldgs
我 相信 ,我从传递正确的索引值Dashboard达到App和向下Bldgs。但是,我的App.js文件中抛出一个错误,指出:
Bldgs
Uncaught TypeError: Cannot read property 'push' of undefined
在我开始将handleClick()函数传递给Dashboard组件之前,我的代码运行良好。
handleClick()
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') );
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;
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;
... 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;
为了history在App组件中使用,请与配合使用withRouter。withRouter仅当您的组件未收到时,才需要使用Router props,
history
withRouter
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