React-router: Switch wrapped in a component not updating on route change

Created on 22 Aug 2017  路  4Comments  路  Source: ReactTraining/react-router

Version

4.1.2

Steps to reproduce

Routes.js

<HashRouter history={history}>
   <App>
      <Switch>
         <Route exact path="/" component={SignIn} />
         <Route path="/signin" component={SignIn} />
         <Route path="/signup/" component={SignUp} />
         <Route path="/signup" component={SignUp} />
      </Switch>
   </App>
</HashRouter>

App.js

<div className="site">
   <NotificationContainer />
   <Header router={this.props.router} session={this.props.session} actions={this.props.actions} exchange={this.props.exchange} />
   {this.props.children}
   <Footer />
</div>

Expected Behavior

On route change the respective component is shown

Actual Behavior

Path changes but previous component is shown until the page is refreshed.

Most helpful comment

I ended up, dumping my App component and wrapping everything in a Router component instead

<Router>
  <div className="site">
    <NotificationContainer />
    <Header session={this.props.session} actions={this.props.actions} />
    <Route
      render={({ location }) => (
        <ReactCSSTransitionGroup
          transitionAppear={true}
          transitionAppearTimeout={450}
          transitionEnterTimeout={450}
          transitionLeaveTimeout={200}
          transitionName={this.getPageAnimation(location)}
        >
          <Switch key={location.key} location={location}>
            {/* ... routes in here ... */}
            <Route component={NotFound} />
          </Switch>
        </ReactCSSTransitionGroup>
      )}
    />
    <Footer />
  </div>
</Router>

All 4 comments

@jebzaki did you solve this issue? I am experiencing something similar.

I ended up, dumping my App component and wrapping everything in a Router component instead

<Router>
  <div className="site">
    <NotificationContainer />
    <Header session={this.props.session} actions={this.props.actions} />
    <Route
      render={({ location }) => (
        <ReactCSSTransitionGroup
          transitionAppear={true}
          transitionAppearTimeout={450}
          transitionEnterTimeout={450}
          transitionLeaveTimeout={200}
          transitionName={this.getPageAnimation(location)}
        >
          <Switch key={location.key} location={location}>
            {/* ... routes in here ... */}
            <Route component={NotFound} />
          </Switch>
        </ReactCSSTransitionGroup>
      )}
    />
    <Footer />
  </div>
</Router>

Ah, a <Route> with a controlled <Switch>, cool. Thanks for sharing!

@jebzaki you provided the only working solution, which I have found. My component was connected to redux, tried wrapping it with withRouter and passing pure: false to the connect options, nothing worked.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ArthurRougier picture ArthurRougier  路  3Comments

wzup picture wzup  路  3Comments

davetgreen picture davetgreen  路  3Comments

tomatau picture tomatau  路  3Comments

stnwk picture stnwk  路  3Comments