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

fix: convert types in @remix-run/router to be framework-agnostic #9141

Merged
merged 7 commits into from
Aug 11, 2022

Conversation

brophdawg11
Copy link
Contributor

Removes the last two React.ReactNode type references from @remix-run/router. It doesn't need to know or care about the element/errorElement in the end, it simply needs to know what routes have error boundaries so it can bubble errors accordingly.

Changes:

  • element/errorElement removed from @remix-run/router types in favor of hasErrorBoundary?: boolean
  • Types exported from @remix-run/router renamed to AgnosticRouteObject, AgnosticDataRouteObject, AgnosticRouteMatch, and AgnosticDataRouteMatch for clarity
  • react-router extends these types with react-specific element/errorElement fields and exports the current public-API types of RouteObject, DataRouteObject, RouteMatch, and DataRouteMatch

I'm not crazy about the Agnostic* prefixes but it makes it very explicit that these are not the things you should be using in your application code, and you should always be using the non-agnostic types exported from react-router (or eventually @remix-run/preact, @remix-run/vue, etc.).

@changeset-bot
Copy link

changeset-bot bot commented Aug 9, 2022

⚠️ No Changeset found

Latest commit: cf3d5c9

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@@ -27,7 +32,6 @@ import type {
HashHistory,
History,
HydrationState,
RouteObject,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

react-router-dom now re-exports from react-router instead of @remix-run-/router

@@ -631,6 +635,7 @@ export function createRoutesFromChildren(
loader: element.props.loader,
action: element.props.action,
errorElement: element.props.errorElement,
hasErrorBoundary: element.props.errorElement != null,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createRoutesFromChildren fills this field in for you automatically based on errorElement. This means the user-facing JSX API doesn't change at all.

If a user decides to create their own routes array manually and do <DataBrowserRouter routes={routes} />, then they are responsible for including hasErrorBoundary accordingly. This feels sort of annoying but seems necessary unless we want to walk the tree and add it on our own prior to handing off to createRouter - which means we need to create copies of the routes to avoiding mutating in place, etc.

@ryanflorence WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'd like to add it before handing it off to createRouter. That shouldn't be a detail React Router users should have to care about.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok cool - added that in 11b9bb6

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and renamed to enhanceManualRouteObjects in b45f4f5 😄

RouteObjectType extends RouteObject = RouteObject
> extends AgnosticRouteMatch<ParamKey, RouteObjectType> {}

export interface DataRouteMatch extends RouteMatch<string, DataRouteObject> {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Export react-specific versions of these from react-router

@@ -559,7 +560,7 @@ export function _renderMatches(
if (dataRouterState?.errors) {
// Don't bail if we have data router errors so we can render them in the
// boundary. Use the pre-matched (or shimmed) matches
matches = dataRouterState.matches;
matches = dataRouterState.matches as DataRouteMatch[];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're inside a data router initialized with react-specific routes, we can safely cast these back here when we pull them from the agnostic router state.

@brophdawg11 brophdawg11 force-pushed the brophdawg11/agnostic-router-types branch from c8d1289 to e519220 Compare August 10, 2022 14:22
@brophdawg11 brophdawg11 merged commit 6df7745 into dev Aug 11, 2022
@brophdawg11 brophdawg11 deleted the brophdawg11/agnostic-router-types branch August 11, 2022 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants