Replies: 7 comments 2 replies
-
I'd like to propose an alternative syntax for when order doesn't matter: <Route path="dogs" searchParams={{ x: new Set(["b", "a"]) }} element={<X />} /> I think it makes a lot of sense to make this logic declarative. It makes understanding this flow a lot more intuitive and less roundabout. |
Beta Was this translation helpful? Give feedback.
-
Oh, that's very nice. I do wonder if maybe defaulting to |
Beta Was this translation helpful? Give feedback.
-
Just a thought: what if it were all encoded in the <Route path="products?type=shoes" element={<Shoes />} /> If you're not looking for a particular value, but merely the existence of a query param, you could just leave off the value: <Route path="posts?date" element={<PostsSearchByDate />} /> Obviously not quite as powerful as the functional matching you're proposing above @AHBruns, but I do like how simple it is to read. And I think we can probably nail 90% of the use cases too. |
Beta Was this translation helpful? Give feedback.
-
@mjackson, I think that'd be a good complexity to power solution. It'd also be possible to build libraries on top of that API for more complex usage. E.g. accept a function to generate a description of param requirements then compile the result to a single which gets passed to the underlying route component. |
Beta Was this translation helpful? Give feedback.
-
I again propose a |
Beta Was this translation helpful? Give feedback.
-
I'm guessing a suitable place for this as of 6.4 would be in the // ...
{
path: 'search-params-validation',
loader: ({ request }) => {
const url = new URL(request.url);
const query = Object.fromEntries(url.searchParams.entries());
const validator = object({ sort: oneOf('ASC', 'DESC') }, { allowUnknown: true });
if (!okay(query, validator)) {
return redirect('/');
}
return json({ query });
},
element: <>Success</>
}
// ... However, at least in Remix, I presume this would result in a server request, even though technically it could be executed just fine client side? |
Beta Was this translation helpful? Give feedback.
-
Is this blocked because of remix file based routing? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
What is the new or updated feature that you are suggesting?
I think we should be able to route based on search params in a similar manner to how we route based on pathname. This idea was spawned from this tweet.
For instance, in an app I'm currently working on we have a list of charts that can be hidden, and we need to support link sharing so the hidden/showing state needs to be in the URL somewhere. The list is hidden when either the
page
search param is missing or thehideList
search param is set to true. Right now I start the<ChartsList />
component by checking the current search params viauseSearchParams
and returning null if the conditions for hiding it are met.In my mind, I'd like to do:
Notice that this lets me not only pattern match based on search params, but it gives me a way to express a requirement that a param exists with any value vs exists with a specific value. I imagine
searchParam.exists
would be a symbol. Ideally this would also allow exclusion viasearchParam.doesNotExist
. Also, anexactSearchParams
prop to enforce no extra searchParams would be nice though I almost think that's an anti-pattern we should prevent people from falling into.An open question would be how to support params with a list of available values. I'd lean toward allowing predicate functions as values. Then we can dynamically determined which values are okay. Seems cool though, maybe a bit overkill. Something like:
Additionally, just like
createSearchParams
this API should allow the same shorthand syntax that createSearchParams does.Only thing that I'm not sure about is ordering. A pathname is inherently ordered, but search params aren't, but also kind of are. Like, if I don't duplicate any keys I generally just treat them as a set, but if I duplicate some keys, I have to think about order. So, the question becomes should the following match
dogs?x=b&x=a
?If yes, then how would we implement the following?
Obviously, the intent is to choose one or the other based on order, but if order doesn't matter the first would always be matched. With this in mind, I think we need to say order matters, then for situations where we don't care about order we'd use a predicate function like the following.
This seems pretty verbose for the default case though. So, maybe invert it and say order doesn't matter then when you need to care about ordering you have to use a predicate function?
Anyways, I'm sure I've only thought about a fraction of what's need to do this well. I'd love to hear other people's thoughts!
Why should this feature be included?
The value here is pretty clear. With this API you'd no longer need to access search params via useSearchParams to make what amounts to a routing decision. You'd be able to make all routing decisions declaratively.
Beta Was this translation helpful? Give feedback.
All reactions