When redirecting to a route under a container, the child components won't get rendered.
See original GitHub issueI have several session routes like /login and /forgotpassword. I wrap these routes in the SessionContainer. The rest of the routes I wrap in the MainAppContainer. My app looks like this:
<Provider store={store}>
<Router>
<div className="App" >
<ProgressBar />
<MuiThemeProvider theme={theme}>
<div>
<div id="container">
<Switch>
<SessionContainer>
<Route exact path="/login" component={Login} />
<Route exact path="/forgotpassword" component={ForgotPassword} />
</SessionContainer>
<MainAppContainer>
<Route exact path="/" component={Home} />
</MainAppContainer>
</Switch>
</div>
</div>
</MuiThemeProvider>
</div>
</Router>
</Provider>
In the app container, I check whether the user is there in my redux state and redirect the user to the app if true. In the app container I reidrect to login if the user is not there. Example (My app container):
const MainAppContainer = ({ children, user }) => {
if (user) {
return (<div>
<Sidebar />
<MainContainer>
<UserMenu />
<InnerContainer>
{children}
</InnerContainer>
</MainContainer>
</div>);
}
return <Redirect to="/login" />;
};
The problem is, if I go to the app without logging in, the redirection happens as intended but, only the session container is loaded. The child components are not getting loaded. Any idea why this is happening?
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
When redirecting to a route under a container, the child ...
Back to the main app, the MainAppContainer is rendered again. ... Solution: Check the current pathname before deciding to redirect or not.
Read more >React Router 4: A Practical Introduction - Auth0
It serves as the container for every other route component. Furthermore, the Router component can only have one child element or component.
Read more >The Guide to Nested Routes with React Router - ui.dev
In this comprehensive, up-to-date guide, you'll learn everything you need to know about creating nested routes with React Router.
Read more >Navigation in React App using React Router (v6)
The component that needs to be rendered when the user is navigated to a particular path is defined by the element property in...
Read more >React Router 6 Tutorial - Robin Wieruch
We have seen once again how to create nested routes by nesting one Route component (or multiple Route components) in another Route component....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I just solved it! Everything works well until we add Redux. Redux prevents the container’s location from getting updated.
@jackjwilliams In your container, use the router’s location prop but when connecting redux, add
withRouterfirst and then add the reduxconnectlike this:Have a look at this stackoverflow question: https://stackoverflow.com/questions/45731078/when-redirecting-to-a-route-under-a-container-the-child-components-wont-get-re Make sure you read the comments.
Using withRouter did not work for me. Thank you for the guides though. I’ve even inlined my
<Layout><Route/></Layout>directly in my ReactDOM.render to no avail. I may just get back to the basics without react-router-redux.