Error using unstable_HistoryRouter with react-router-dom "^6.3.0", #8
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.
Problem Description
We encountered a TypeScript error related to the history package version incompatibility with the React Router. Specifically:
Type 'BrowserHistory' is missing the following properties from type 'History': createURL, encodeLocation
This error occurs because we are using an old version of history (v5) with a newer version of React Router (v6.3+), which has an unstable API and uses a different internal history implementation.
Solution
Current Workaround:
To address this issue, we added // @ts-expect-error to suppress the TypeScript error for the unstable API:
Alternative Solutions:
a. Use the New History Implementation:
We can leverage the new history implementation compatible with React Router v6.3+, though it has some differences from v5 (e.g., no blocking, no back/forward - just go). Example usage:
b. Upgrade to the New Router Version:
Another option is to upgrade to the latest version of React Router. However, this would require significant changes to the codebase, as nearly all instances of the old router would need to be replaced with the new implementation.
Link: remix-run/react-router#9630