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.

When redirecting to a route under a container, the child components won't get rendered.

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
THPubscommented, Aug 22, 2017

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 withRouter first and then add the redux connect like this:

export default withRouter(connect(mapStateToProps)(Container));

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.

0reactions
jjwilliams42commented, Aug 18, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

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