question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React Router 4 (beta 8) won't render components if using redux connect

See original GitHub issue

Version

4.0.0-beta.8

If a component containing <Route> components is exported with a Redux connect wrapper non of the components mapped to those routes components are rendered. Let’s say you have

<BrowserRouter>
 <App />
</BrowserRouter>

and App looks like:

class App extends Component {

  render() {
    return (
      <div>
          <Route exact path="/" component={Home} />
          <Route path="/Test1" component={Test1}/>
          <Route path="/Test2" component={Test2} />
      </div>
    );
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(App);

then none of the components get rendered on route changes. If you remove the connect wrapper all works as expected.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:104
  • Comments:40 (14 by maintainers)

github_iconTop GitHub Comments

146reactions
hihuzcommented, Mar 9, 2017

Hey klase, I may be mistaken but I think this is exactly the problem I had these previous days. I was used to the v4 alpha API where doing what you shown above worked fine.

In the latest beta API however they kind of went back to their previous way of doing things (that is using react context exclusively to communicate changes), connect() prevents that from working by ways of shouldComponentUpdate.

They have provided again in the beta the withRouter() HOC that solves this nicely, just import it and do

export default withRouter(connect(mapStateToProps, mapDispatchToProps)(App));

This should work just fine after that. Note that you would also need to wrap any redux connected component that has router components inside it with a withRouter(). That means if you have router Links in your redux connected Nav component for example, you would need to wrap it with withRouter() as well.

Hope this helps.

Here is a helpful discussion over on the react-redux repo

81reactions
thedukecommented, Mar 27, 2017

This MUST be mentioned very prominently in the docs, it’s an issue many will run into.

I spent a long time scratching my head, trying to figure out why my routes aren’t working.

@timdorr

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Router 4 (beta 8) won't render components if using ...
Version 4.0.0-beta.8 If a component containing components is exported with a Redux connect wrapper non of the components mapped to those ...
Read more >
reactjs - Router renders nothing - Stack Overflow
I copied store/Router solution from my previous project where it was working fine. Then updated redux/router/dom dependencies, ...
Read more >
connected-react-router - npm
A Redux binding for React Router v4 and v5. ... Start using connected-react-router in your project by running `npm i connected-react-router` ...
Read more >
React Router DOM: How to handle routing in web apps
React Router DOM contains the DOM bindings for React Router. Learn about its essential components and how to build routes with parameters.
Read more >
Suspense for Data Fetching (Experimental) - React
We call this approach “fetch-on-render” because it doesn't start fetching until after the component has rendered on the screen. This leads to a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found