Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nesting Switches #6199

Closed
Iggyfufu opened this issue Jun 8, 2018 · 1 comment
Closed

Nesting Switches #6199

Iggyfufu opened this issue Jun 8, 2018 · 1 comment

Comments

@Iggyfufu
Copy link

Iggyfufu commented Jun 8, 2018

Version

react-router-dom 4.1.1

Hello,

Ran into an issue when trying to nest Switches for these routes.
Going to a route that is contained inside the inner Switch it will redirect to /products if I was using Redirect. To try to work around that I replaced Redirect with a fallback route, but this solution only works if isLoggedIn is false so to fix that I also added a fallback route inside the inner Switch.

But this doesn't seem right. Is there a better way to implement this? This is my first time submitting an issue please excuse my lack of formatting.

  <Switch>
    {/* Routes placed here are available to all visitors */}
    <Route exact path="/login" component={Login} />
    <Route exact path="/register" component={RegisterForm} />
    <Route exact path="/product/:id" component={ProductDetails} />
    <Route exact path="/products" component={Products} />
    <Route exact path="/cart" component={Cart} />
    {/* Routes placed here are only available after logging in */}
    {isLoggedIn && (
      <Switch>
        <Route exact path="/user" render={history => (
            <Account history={history} user={this.props.user} />
          )}
        />
        <Route exact path="/home" component={UserHome} />
        <Route exact path="/checkout/address"
          component={CheckoutContainer}
        />
        <Route exact path="/checkout/credit"
          component={CheckoutContainer}
        />
        <Route component={Products} />
      </Switch>
    )}
    {/* Displays our Login component as a fallback */}
    <Route component={Products} />
    {/* <Redirect to="/products" /> */}
  </Switch>
@pshrmn
Copy link
Contributor

pshrmn commented Jun 8, 2018

You cannot nest <Switch>es. A <Switch> treats all of its direct children as <Route> element, so a <Switch> in a <Switch> will act as a pathless <Route>.

One alternative approach would be to have two <Switch>es and render one or the other based on if the user is logged in. This isn't perfect because it requires you to duplicate some of your routes.

{isLoggedIn
  ? <Switch>
      {/* all routes */}
     </Switch>
  : <Switch>
      {/* public routes */
    </Switch>
}

If it were me, I would probably write a custom <Switch> component that is better suited for this use case. It is really just a loop over props.children, so implementing your own isn't very difficult.

@pshrmn pshrmn closed this as completed Jun 8, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Aug 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants