Skip to content

Exclusive matching #3994

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

Closed
aaronshaf opened this issue Oct 4, 2016 · 13 comments
Closed

Exclusive matching #3994

aaronshaf opened this issue Oct 4, 2016 · 13 comments

Comments

@aaronshaf
Copy link
Contributor

aaronshaf commented Oct 4, 2016

"React Router lets you match in multiple places on purpose" (https://react-router.now.sh/ambiguous-matches)

For simplicity it'd be nice to have an exclusive property:

<Match exclusive exactly pattern="/about" component={About}/>

This would simplify use of Match/Miss. It could be exclusive relative to its Match siblings.

@timdorr timdorr added this to the v4.0.0 milestone Oct 5, 2016
@Ciantic
Copy link

Ciantic commented Oct 8, 2016

Also, because there is no way to define exclusivity, one has to trick the MainApp using Miss:

const NoMatch = ({ location }) => (
    <div>
        <h2>Whoops</h2>
        <p>Sorry but {location} didn’t match any pages</p>
    </div>
)

const MainApp = () => (
    <div>
        <h2>Main App</h2>
        <Match exactly pattern="/About" component={About} />
        <Miss component={NoMatch} /> 
    </div>
)

class App extends React.Component<any, any> {
    render() {
        return <HashRouter>
            <div>
                <Match pattern="/OtherApp" component={OtherApp} />
                <Match pattern="/YetAnotherAppf" component={YetAnotherAppf} />
                <Miss component={MainApp} />
            </div>
            </HashRouter>;
    }
}

And even in my example, the NoMatch does not work because two Miss can't be inside each other, I think.

@aaronshaf
Copy link
Contributor Author

Here is a (fledging, incipient) proof of concept:

https://react-middle-router.now.sh/

@ryanflorence
Copy link
Member

@Ciantic you want <Match pattern="/" exactly component={MainApp}/> not <Miss>.

@kwelch
Copy link
Contributor

kwelch commented Oct 11, 2016

@ryanflorence That would breaking nesting within MainApp, if there was nesting there that is different from the Matches at the app level. I have a similar example trying to get it into text.

@ryanflorence
Copy link
Member

Let's keep @Ciantic's conversation over on #4016

@aaronshaf I'm not totally understanding what exclusive does, can you explain it some more?

@aaronshaf
Copy link
Contributor Author

@ryanflorence exclusive says, "if I match, then don't match any of my siblings." See "foo" at the prototype for https://react-middle-router.now.sh/foo

@ryanflorence
Copy link
Member

gotcha, that would make things order dependent, I have something I'm still playing with that would probably meet the use-cases you've got (<MatchGroup/>)

@Ciantic
Copy link

Ciantic commented Oct 11, 2016

@ryanflorence hmm, I thought exactly means basically ^/$ in regexp terms.

I mean why would exactly "/" match the "/about"?

That is why I think I need exclusive.

@nick
Copy link

nick commented Oct 24, 2016

I think something akin to 'exclusive' would solve my issue too, as I never want more than Match to apply at once. Like how routes in express stop matching after the first hit.

<Match pattern="/terms" component={Terms} />
<Match pattern="/product/:id" component={ProductDetail} />
<Match pattern="/:country?/:category?" component={ProductList} />

/terms => Terms
/product/prod-id => ProductDetail
/us => ProductList
/us/cats => ProductList
/cats => ProductList
/ => ProductList

Right now, /terms and /product/prod-id also displays the 'ProductList' component

ryanflorence added a commit that referenced this issue Oct 26, 2016
closes #3936, #3994, #4032

be sure to see commit messages on cf2555d
@timdorr timdorr closed this as completed Oct 26, 2016
@Ciantic
Copy link

Ciantic commented Jan 29, 2017

Is this now supported? Match was renamed Route, and MatchGroup does not exist anymore. Is exclusivity achieved some other way?

@Ciantic
Copy link

Ciantic commented Jan 29, 2017

Apologies, combing through these various docs, I found MatchGroup is renamed to "Switch"

@mjackson
Copy link
Member

Always best to check the docs before commenting on issues, @Ciantic :) Please submit a PR if something's missing! Beta next week! :D

@Ciantic
Copy link

Ciantic commented Jan 29, 2017

Yes I had bookmarked the old docs ( https://react-router.now.sh/ they are not updated anymore ) was scouring those, then the README.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants