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

Update to react-router 7 #1382

Merged
merged 7 commits into from
Dec 29, 2024
Merged

Update to react-router 7 #1382

merged 7 commits into from
Dec 29, 2024

Conversation

jtojnar
Copy link
Member

@jtojnar jtojnar commented Oct 15, 2022

Unfortunately, regular expressions in routes are no longer supported so URLs like /newest/source-2/143 or /unread/tag-news/259 will no longer work. We will need to switch them to something like /sources/2?filter=newest#143 and that will collide with the server API, so it will need to be moved to /api/ subdirectory. I managed to work around remix-run/react-router#8254 with intermediary EntriesFilter component with a custom validation logic.

Though I do not really like the direction of the library towards frameworkization.

@netlify
Copy link

netlify bot commented Oct 15, 2022

Deploy Preview for selfoss canceled.

Name Link
🔨 Latest commit d341be2
🔍 Latest deploy log https://app.netlify.com/sites/selfoss/deploys/634b2c9de36d5400085b8c2a

In react-router 5, when a function is passed to `children` prop,
it is rendered even when the `Route`’s `path` does not match.
We did that with `EntriesPage`.

https://v5.reactrouter.com/web/api/Route/route-render-methods

Since we do not need to animate `EntriesPage` or anything,
rendering it when not on `/entries` page is pointless.
Let’s switch it to `render` prop.

The upcoming react-router 6 migration will require all routes to use
the `element` prop so, while at it, let’s also switch the other routes
to make the future diff cleaner.
These are apparently soft deprecated in react-router 5.1 and will be removed in 6:
https://reacttraining.com/blog/react-router-v5-1#meet-react-router-51
https://reactrouter.com/6.28.1/upgrading/v5#upgrade-to-react-router-v51

Since our `EntriesPage` is currently a class component, we need to wrap it
in an functional one so that we can use `useParams` there.

We will be able to drop the `forwardRef` once we migrate to React 19:
https://react.dev/blog/2024/12/05/react-19#ref-as-a-prop
We are pushing an `id` for each entry in `this.state.entries` so
filtering the array based on `ids` would just return an empty array.
Less global state the better.
https://remix.run/blog/react-router-v6
https://reactrouter.com/6.28.1/upgrading/v5

- Explicit `History` object manipulation was replaced with `navigate` function.
- Similarly `Redirect` component was replaced with `Navigate` component.
  Also it now defaults to `push` so had to switch to `replace`.
- Regex path had to be replaced with a custom validation logic (see `EntriesFilter` component):
  https://reactrouter.com/6.28.1/start/faq#what-happened-to-regexp-routes-paths
- `Route` `path`s need to be relative.
- `Link`’s `to` prop no longer accepts callback or state.
- `Prompt` component was replaced with an unstable hook.
  Like before, it still does not handle `beforeunload` event.
  https://reactrouter.com/6.28.1/hooks/use-prompt#unstable_useprompt
  But it only works with data routers:
  https://reactrouter.com/6.28.1/routers/picking-a-router
  Yuck, I do not want framework, I just want a routing library.
  Dropping the feature for now.
Not much relevant to us. Just the renaming of the package,
and the features behind the `future` flags being enabled by default.
https://github.com/remix-run/react-router/blob/08e4f2fd399543cab776f4be8a29181093a3702c/CHANGELOG.md#v700
https://reactrouter.com/upgrading/v6
@jtojnar
Copy link
Member Author

jtojnar commented Dec 29, 2024

Dropped 18e99da in favour of 029abf5.

Copy link

netlify bot commented Dec 29, 2024

Deploy Preview for selfoss canceled.

Name Link
🔨 Latest commit 168e0e7
🔍 Latest deploy log https://app.netlify.com/sites/selfoss/deploys/6770d677c9d70a00084d9afe

@jtojnar jtojnar added this to the 2.20 milestone Dec 29, 2024
@jtojnar jtojnar added enhancement dependencies Pull requests that update a dependency file labels Dec 29, 2024
@jtojnar jtojnar marked this pull request as ready for review December 29, 2024 04:58
@jtojnar jtojnar changed the title Update to react-router 6 Update to react-router 7 Dec 29, 2024
@jtojnar jtojnar merged commit 168e0e7 into master Dec 29, 2024
15 checks passed
@jtojnar jtojnar deleted the react-router-6 branch December 29, 2024 04:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant