This misses event.shiftKey. All keyboard modifiers disqualify client-side routing.
As for <Link> in its entirety, where you have to use a special component which becomes a link and adds a click handler to it, I think this is generally the wrong solution: it’s better to instead put a single click handler on the entire document to intercept clicks on links. That way, you can just use normal links everywhere and it just works, rather than having to remember to use a whole separate component every time which may or may not be able to pass through the required properties or attributes (e.g. this one only supports class and href). Simpler, smaller and cheaper.
I think React tends to encourage local solutions (using a Link component) over global ones (inserting a global click handler) when possible. Global solutions are less composable and will cause issues if you have multiple independent teams working on the same page (eg maybe they need different click handlers)
Whenever I see someone describe how small something they've written as a replacement for a big library is, I wonder what they've discarded in order to get it so small.
Normally it's backwards compatibility, but in this case it appears to be "everything".
If I read this correctly, all the route components are evaluated (i.e. rendered to virtual DOM) and only then one of them is selected and shown (i.e. rendered to HTML DOM). This seems utterly inefficient, even for educational example.
You read that incorrectly. Only the active route component is returned by the Router function, therefore React's render() won't do anything with the others.
There's one thing, you should redirect all the pages to one single endpoint in server side order to use "pushState". Otherwise it will return 404 when you hit the refresh button. If you don't own a server, you can support routing with hashtag "#" and listen to "onhashchange" event instead of "popstate".
Also, if you would like to support nested and dynamic routes (it's not possible with that code snippet in the github repository since it just checks like `path===currentPath`), you might look at the following solution:
Please don’t encourage using a single route and the hash; people should just ensure that suitable document is served from all routable URLs—whether it be a consistent bootstrap file, or (generally preferably) that you do server-side rendering. Using the fragment for routing is only something to do when you can’t do it the proper way, not a tool to reach for from the start.
> There's one thing, you should redirect all the pages to one single endpoint in server side order to use "pushState". Otherwise it will return 404 when you hit the refresh button. If you don't own a server, you can support routing with hashtag "#" and listen to "onhashchange" event instead of "popstate".
Could you explain this one a bit more / maybe some example code I could borrow from? :)
I've used a lot of routers and my favorite is still page.js[1]. It hasn't been updated in years. But it's small, is Express-compatible (i.e. server/client routes can use the same code), and, more importantly, is hackable. I'll never use a router tied to a certain framework again (react, nextjs, etc.) because you trade flexibility for perceived convenience (e.g. using folder structure as route structure, or React component tree as route structure). But it's a terrible trade-off that paints you into a corner later, IMO. Routing can get really niche and site-dependent, so having it fully under your control is worth it.
1kb is likely a lot more than what is shown here; I made a "tiny" but very complete React Router package which is very complete and minified+gzip it's just 1.8kb https://crossroad.page/
The author's goal in this seems to be education, not really optimization or replacement of a bigger framework. And I guess this would probably be much less than 1k!
It’s strange that of the numerous frameworks React seems to have the most churn, it’s had multiple ways of “doing state”, there seems to be a new router every year or at the very least a new major (breaking) version of react router.
Why is routing, of all things, in such need of constant development? I say this as someone that’s made SPAs with Vue a lot and occasionally React.
Because making a simple router or simple state management library from scratch is quite simple. So a lot of people do them to learn how the internals work, like this case here. This makes people better programmers. Some of those folks will then decide to continue the development, put some special things here and there and release it to the world to see if anyone is interested in their version of it. Most of those are ignored, which is fine.
However this makes some people vocally angry, because in theory a developer should know every single packages in the world, but never the internals. Only people of a higher class or caste should be allowed to release stuff to the world. The rest are peasants. How dare they. /s
React isn't a framework. In order to build an app, you have to cobble together a bunch of other libraries to use with it to build something meaningful.
As someone coming from an Angular shop, I started a new job a few months ago that uses React on the frontend. My main gripe so far is there is no one "way" to do X in a react-based app. For an enterprise with lots of developers, Angular seems like a better fit - unless you want to spend months coming up with standards. Angular has its own issues of course - it's much more rigid and has a steeper learning curve.
React is a view library. It deliberately focused on doing one thing, and doing it well -- declaratively painting the view layer. It's not comparable to more general/monolithic frontend development solutions like Angular/Vue/etc. Those tools have far different scopes. So of course React doesn't prescribe routing, state management, project styling solutions, build bundling, or any other frequently lamented "missing" features. They're only omissions to folks misunderstanding React's use case.
Now there are solutions comparable to Vue/etc which use React -- Next.js, Create React App, etc. Comparisons, tradeoffs, and criticisms are totally fair there. Just don't blame React for churn in third party implementations of features outside of its purview.
As a side note, I love that React is so narrowly scoped. It lets the community build on, experiment with, and coalesce around amazing solutions to other big features outside of React's scope. That Darwinian approach has created broad front end solutions that to me are a joy to use compared to similarly featured frameworks like Angular.
> Why is routing, of all things, in such need of constant development?
Because requirements change. In the last few years there was a lot of churn in React due to the introduction of async rendering and server-side rendering, and React routers were strongly affected by both of those changes.
chrismorgan|3 years ago
This misses event.shiftKey. All keyboard modifiers disqualify client-side routing.
As for <Link> in its entirety, where you have to use a special component which becomes a link and adds a click handler to it, I think this is generally the wrong solution: it’s better to instead put a single click handler on the entire document to intercept clicks on links. That way, you can just use normal links everywhere and it just works, rather than having to remember to use a whole separate component every time which may or may not be able to pass through the required properties or attributes (e.g. this one only supports class and href). Simpler, smaller and cheaper.
woojoo666|3 years ago
wonderbore|3 years ago
Don't forget clicks other than "left". Middle click can open a new tab.
delaaxe|3 years ago
math-dev|3 years ago
lexicality|3 years ago
Normally it's backwards compatibility, but in this case it appears to be "everything".
jrvarela56|3 years ago
These short libs are apples to oranges unless we have a test suite or set of acceptance criteria. Something like https://todomvc.com/ and https://eugenkiss.github.io/7guis/tasks helps keep reqs constant.
delaaxe|3 years ago
janci|3 years ago
ratww|3 years ago
fatih-erikli|3 years ago
There's one thing, you should redirect all the pages to one single endpoint in server side order to use "pushState". Otherwise it will return 404 when you hit the refresh button. If you don't own a server, you can support routing with hashtag "#" and listen to "onhashchange" event instead of "popstate".
Also, if you would like to support nested and dynamic routes (it's not possible with that code snippet in the github repository since it just checks like `path===currentPath`), you might look at the following solution:
https://github.com/fatih-erikli/universal-router/blob/main/u...
I use that solution in server-side and client-side so it works like Nextjs.
chrismorgan|3 years ago
ReactNative22|3 years ago
Could you explain this one a bit more / maybe some example code I could borrow from? :)
math-dev|3 years ago
ReactNative22|3 years ago
deckard1|3 years ago
[1] https://github.com/visionmedia/page.js
ldd|3 years ago
I made one myself that was absolutely the simplest I could go for[0]
0: https://github.com/ldd/react-simplest-router
kretaceous|3 years ago
0: https://github.com/molefrog/wouter
aliswe|3 years ago
ratww|3 years ago
ThePaulAlek|3 years ago
ReactNative22|3 years ago
martini333|3 years ago
franciscop|3 years ago
KhalPanda|3 years ago
ratww|3 years ago
unknown|3 years ago
[deleted]
lloydatkinson|3 years ago
Why is routing, of all things, in such need of constant development? I say this as someone that’s made SPAs with Vue a lot and occasionally React.
ratww|3 years ago
However this makes some people vocally angry, because in theory a developer should know every single packages in the world, but never the internals. Only people of a higher class or caste should be allowed to release stuff to the world. The rest are peasants. How dare they. /s
GiorgioG|3 years ago
As someone coming from an Angular shop, I started a new job a few months ago that uses React on the frontend. My main gripe so far is there is no one "way" to do X in a react-based app. For an enterprise with lots of developers, Angular seems like a better fit - unless you want to spend months coming up with standards. Angular has its own issues of course - it's much more rigid and has a steeper learning curve.
a_wild_dandan|3 years ago
Now there are solutions comparable to Vue/etc which use React -- Next.js, Create React App, etc. Comparisons, tradeoffs, and criticisms are totally fair there. Just don't blame React for churn in third party implementations of features outside of its purview.
As a side note, I love that React is so narrowly scoped. It lets the community build on, experiment with, and coalesce around amazing solutions to other big features outside of React's scope. That Darwinian approach has created broad front end solutions that to me are a joy to use compared to similarly featured frameworks like Angular.
hobofan|3 years ago
Because requirements change. In the last few years there was a lot of churn in React due to the introduction of async rendering and server-side rendering, and React routers were strongly affected by both of those changes.
unknown|3 years ago
[deleted]
yanis_t|3 years ago
[deleted]