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

Params constraints #4019

Closed
ingro opened this issue Oct 9, 2016 · 4 comments
Closed

Params constraints #4019

ingro opened this issue Oct 9, 2016 · 4 comments
Labels
Milestone

Comments

@ingro
Copy link

ingro commented Oct 9, 2016

Hello! While playing with v4's alpha I found a little awkward how ambiguous matches are handled, especially when using a centralized route config.

So I thought about adding optional params constraint to <Match> to better handle ambiguous matches while iterating a route config.

Use case

If I have these routes in my config (I know they are silly, they're just an example):

const routes = [
  {
    pattern: 'posts/:id',
    component: SinglePost,
  },
  {
    pattern: 'posts/comments',
    component: Comments
  }
];

And I render them like that:

{routes.map((route, i) => (
  <Match key={i} {...route}/>
))}

When I visit /posts/comments both routes will trigger.

Proposal

So I propose to add constraints prop to <Match> (I'm very bad at naming, couldn't figure a better one) that will also validate the params in order to match the route.

Something like that:

<Match pattern="/posts/:id" component={Post} constraints={{ id: /\d+/ }} />

In this way when I visit /posts/comments I will not have a double match anymore!

Hopefully this makes sense, my english it's not very good :(

If you find this proposal somewhat interesting I could work on a PR too!

Thanks for your time and your work on this library!

@timdorr timdorr changed the title [PROPOSAL][v4] Add optional params constraint Params constraints Oct 10, 2016
@timdorr timdorr added this to the v4.0.0 milestone Oct 10, 2016
@timdorr
Copy link
Member

timdorr commented Oct 10, 2016

This was being discussed for possibly adding on 3.0 in #2286. Also, a WIP version was in #3098.

@pshrmn
Copy link
Contributor

pshrmn commented Oct 10, 2016

Isn't this already doable using path-to-regexp's custom match parameters?

<Match pattern='/posts/:id(\d+)' component={Post} />

Note: One gotcha with this is that with Babel, JSX strings are automatically escaped, so while in the path-to-regexp documentation they note that you need to use two backslashes (\\d+), in JSX you should only include one (\d+).

<Match pattern='/posts/:id(\d+)' component={PostID} />
// becomes
React.createElement(Match, { pattern: '/posts/:id(\\d+)', component: PostID })

@timdorr
Copy link
Member

timdorr commented Oct 10, 2016

I wasn't familiar with that, but it sounds like that fits the bill. I think the other idea was if you want to do more business logic on your params, but that's best left for your child function.

@timdorr timdorr closed this as completed Oct 10, 2016
@ingro
Copy link
Author

ingro commented Oct 10, 2016

Thanks @timdorr and @pshrmn for the infos, I will look into that!

@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.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants