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

Closed
terencechow opened this issue Aug 11, 2017 · 5 comments
Labels

Comments

@terencechow
Copy link

terencechow 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
Copy link
Member

timdorr 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
Copy link
Author

@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
Copy link

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
Copy link
Member

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
@stale
Copy link

stale bot commented Sep 10, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Sep 10, 2019
@stale stale bot closed this as completed Sep 17, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Nov 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants