react - router withRouter does not inject router

蹲街弑〆低调 提交于 2019-12-01 20:20:11

That is happening because you are injecting the router after you render the react app.

const App = React.createClass({
  render() {
      return (
          <div>
              <AppBar
                title="Democratizer"
                onTitleTouchTap={()=>this.props.router.push('/')}
              >
              </AppBar>
              <br/>

            {this.props.children}
          </div>
      );
  }
});

App = withRouter(App);


render((
  <MuiThemeProvider muiTheme={getMuiTheme()}>
    <Router history={hashHistory}>
      <Route path="/" component={App}>
          <Route path="voteview/:baselineId" component={VoteView}/>
          <IndexRoute component={OverView}/>
      </Route>
    </Router>
  </MuiThemeProvider>
  ), document.getElementById('app'));

A better solution would be creating a file for App like you have done with the other components.

const App = React.createClass({
  render() {
      return (
          <div>
              <AppBar
                title="Democratizer"
                onTitleTouchTap={()=>this.props.router.push('/')}
              >
              </AppBar>
              <br/>

            {this.props.children}
          </div>
      );
  }
});

module.exports = withRouter(App);

and import it in your index.js.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!