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

react-router-config does not match on later paths even if there is an exact path match later #5429

Open
terencechow opened this issue Aug 11, 2017 · 4 comments

Comments

4 participants
@terencechow
Copy link

commented Aug 11, 2017

Version

4.1.1

Test Case

None

Steps to reproduce

Create a react-router-config that has the following:

    [{ component: someAuthorizedLayout
        routes: [
            {component: AuthorizedDashboard, path: '/home, exact: true}
       ]
     },
     { component: UnauthorizedLayout
         routes: [
            {component: Login, path: '/', exact: true}
        ]
     }]

Try to go to '/' and you are unable to. Why? Because the route matches the first route and "consumes" the request before it realizes that there is a '/' exact match

Expected Behavior

I expect it to go to the Login page.

Actual Behavior

It goes to no route because it traverses the routes tree and when it hits someAuthorizedLayout it finds no nested routes that match.

Is this a bug? or is this by design?

@timdorr

This comment has been minimized.

Copy link
Collaborator

commented Aug 11, 2017

This does appear to be a bug in rrc. We're not doing any testing of that form of routes, so the first step would be to add a failing test. Then we can go about fixing for it.

Edit: Why are people thumbs-downing me? 😭

@terencechow terencechow changed the title Match does not work without paths react-router-config does not match on later paths even if there is an exact path match later Aug 11, 2017

@terencechow

This comment has been minimized.

Copy link
Author

commented Aug 14, 2017

@timdorr renderRoutes uses Switch internally and on documentation it says Switch is supposed to match exclusively.

If you are saying this is a bug, that means renderRoutes should not use Switch internally. Is that correct? Are there any requirements for renderRoutes to use Switch internally? I need this context before looking into possible solutions... If there is a requirement for renderRoutes to use Switch internally then I don't see a possible solution since by definition Switch is supposed to match exclusively.

Please advise. I can see if I can help contribute to fixing this bug but need more info. Thanks

@Evgenyx82

This comment has been minimized.

Copy link

commented Sep 6, 2017

You can workaround this... set flag (variable) as UnauthorizedLayout/someAuthorizedLayout into routes config. Then use matchRoutes, to find matched route, check flag, then do redirect with some status if fail, or just render if it success )...

here is example:

const routes = [
  { component: Root,
    routes: [
      { path: '/',
        exact: true,
        component: App,
        loadData: (dispatch) => new Promise((resolve, reject) => {
          dispatch(setCounter(44));
          resolve('loadData set counter');
        })
      },
      { path: '/courses',
        component: Wrapper,
        routes: [
          { path: '/courses',
            exact: true,
            component: Courses,
            loadData: (dispatch) => new Promise((resolve, reject) => {
              dispatch(fetchCourses(() => { // pass promise callback 
                resolve('loadData set courses');
              }));
            })
          },
          { path: '/courses/public',
            component: CoursesPublic,
            auth: false
          },
          { path: '/courses/draft',
            component: CoursesDraft,
            auth: true
          }
        ]
      },
    ]
  }
]
@mjackson

This comment has been minimized.

Copy link
Member

commented Oct 10, 2018

I believe renderRoutes should not be using a <Switch> internally, but rather should return an array of <Route> elements.

@mjackson mjackson added this to the 4.4 milestone Oct 10, 2018

@mjackson mjackson modified the milestones: 4.4, 4.5 Mar 15, 2019

@mjackson mjackson removed this from the 5.1 milestone Mar 26, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.