React-router: Nested Switch would lead to better composability

Created on 29 Oct 2019  路  2Comments  路  Source: ReactTraining/react-router

In pseudo code, what I propose is that a Switch does the following

    for routeOrSwitch in switch:
        component = routeOrSwitch.match(location)
        if component:
             return render(component)

This would not change the semantics of Switch and make it possible to compose (and therefore: reuse) Switches.

ps using React.Fragment to group Routes (something that has been proposed) has less clear semantics, so I would prefer to allow nesting Switches.

Most helpful comment

Hi Tim, what I meant is that a Switch should be able to contain not only Routes but also other Switches. It would not change the semantics of a Switch: it still picks the first child routeOrSwitch that matches the location. This would make Switches composable, which would be a good thing.

All 2 comments

I'm not sure I quite understand what you're getting at here, but they certainly do nest: https://reacttraining.com/react-router/web/example/nesting

Hi Tim, what I meant is that a Switch should be able to contain not only Routes but also other Switches. It would not change the semantics of a Switch: it still picks the first child routeOrSwitch that matches the location. This would make Switches composable, which would be a good thing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

imWildCat picture imWildCat  路  3Comments

davetgreen picture davetgreen  路  3Comments

wzup picture wzup  路  3Comments

misterwilliam picture misterwilliam  路  3Comments

jzimmek picture jzimmek  路  3Comments