-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more fixes in Upgrade_20230121_ReactRouter6
- Loading branch information
1 parent
094fab5
commit 76755e7
Showing
1 changed file
with
3 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76755e7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Upgrade to
react-router
6.7.0One of the dependencies that has been lagging behind for 5 months is
react-router
because this lirary has a tendency to make big breaking changes and 6 was not an exception.Changes in
react-router
6This version deprecates most of the API using props (
RouteComponentProps
) in favour for a new hooks API.Also the new API encourages a global routes configuration and relative links and routes configuration that helps with Server Side Rendering.
Here some links of the changes in react-router 6:
https://reactrouter.com/en/main/upgrading/v5
https://blog.saeloun.com/2021/12/02/new-features-in-react-router-6.html
https://www.youtube.com/watch?v=Ul3y1LXxzdU
Changes in
react-router
6.4Also in version 6.4 another wave of important changes came, encouraging the use of javascript objects and
createBrowserRouter()
instead of React Elements and<BrowserRouter>
.This new version also introduces the concept of loaders, to parallelize the loading of data with the loading of javascript modules, but only when using the new internal router instead of the old
history
dependeny.https://reactrouter.com/en/main/start/overview
https://www.youtube.com/watch?v=6xqh2f-sV6E
In general, I don't think that most of the changes are very important for the typical LOB Application, but fundational stones for https://remix.run/, framework for building front-facing websites like a catalog.
Problems with
~/
and other braking changes.Traditionally ASP.Net applications are often deployed in IIS using sub directories urls like
http://localhost/MyApp/
. In order to make the application portable to othe sub directory, or no sub directory at all, the~
is used. https://learn.microsoft.com/en-us/previous-versions/aspnet/ms178116(v=vs.100)This tradition is not common in other web frameworks, where applications in development are typically deployed in another port, like
http://localhost:8081
.In order to make app-relative urls unambiguous and simplify IIS deployment, I monkey-pached some
react-router
classes to support~/
syntax. Here is how the hack worked.Unfortunatly,
react-router
6 is based on hooks and functional componets, so is not easy to monkey patch.This means that
~/
is gone... and doesn't work in<Link/>
, innavigate
( previouslypush
/replace
), and for consistency also inajaxGet
andajaxPost
. Instead just use/
to refer to the beginning of the application.is
~
really gone? ...ok, not quite. It's there for backward compatibility, but should not be used anymore.Old Behaviour in
toAbsolutUrl
and<Link/>
(monkey-pached)New Behaviour in
toAbsolutUrl
, andAppContext.navigate
New Behaviour in
<Link/>
oruseNavigate
Unfortunatly monkey-paching
react-router
is no longer possible, so there are some differences in behaviour, but as long as you use app relative urls starting with/
everithing will be ok.URL Strategy
The rule of thumb is, only use URLS like
/myPage
and at the very end, when the url is going to be used outside ofreact-router
usetoAbsoluteUrl
. Examples:window.open(AppContext.toAbsoluteUrl("/myPage")}
<a href={AppContext.toAbsoluteUrl("/myPage")}/>
<img src={AppContext.toAbsoluteUrl("/myLogo.png")}/>
But don't call
toAbsoluteUrl
for react-router stuff:<Link to="/myLogo.png" />
How to Upgrade
Fortunatly
Upgrade_20230121_ReactRouter6
should fix 99% of the braking changes. It's a complex upgrade script but should work if your code is idiomatic Signum React code.This is an example of the changes in Southwind: signumsoftware/southwind@be65dc9