Preact-ISO: Adding TS-approved Route component #422
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Apologies in advance if this gets a bit long winded.
This PR adds a simple
Route
component that matches the one used inpreact-router
. The problem that it looks to solve is around the typings. The current support for using any component as a route brings on a few problems, namely, those route props have to be added to every single component globally and for typed components that expect route parameters, this method does not work.For example,
The form
<Example path=... />
results in the following TS error:Which is technically correct.
As far as I know we're the only ones still using this API.
reach-router
was, however, it's now being absorbed intoreact-router
and the the community driven types decided not to support that form due to the (pretty horrible) hack needed, applying the route props globally (not ragging on anyone, I'm the one who wrote the types that allowed that; not evenpreact-router
allows using that form in TS). Bothreact-router
andwouter
both go the way of using a<Route />
component instead, though they do so in their own ways when it comes to route parameters and the like.This PR gives TS users the helper form
<Route />
, taken frompreact-router
. It too can arguably be called a hack, as it's essentially just dodging the type checking. I suppose it depends on people's opinions as towards how this is documented; my opinions are very TS-centric so I'd lean towards promoting this usage even if it is more verbose, as otherwise some TS users will inevitably turn up having difficulty, but on the other hand, there probably are more JS users, so this does nothing to help them.I think it's worth exploring moving to this style of API entirely with a
preact-iso
v2 eventually, as TS and arbitrarily adding props/changing the types of components don't go well together. And seeing as howreach-router
was merged intoreact-router
, I'm not sure this will even remain a familiar API for users. I don't think we'd be losing out on anything, though that's a discussion for further down the road.