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

[Router] improve comments and consolidate validation logic #3977

Closed
wants to merge 11 commits into from

Conversation

jtoar
Copy link
Contributor

@jtoar jtoar commented Dec 22, 2021

Note: tests pass locally, but there's a type error. I have to reconsider some of the changes I made there

As a follow-up to #3772 and to keep the work on the router going, I wanted to do some of the some of the easier refactors before I forgot how this thing works again. Namely by

  • comments all the things and/or improving existing comments
  • consolidating the validation logic
  • adding missing peerDependencies

Most of the changes are just comments. Even in router.tsx, although that's where I strived to consolidate the validation logic so a little more's going on there.

@Tobbe I've removed the custom isReactElement function in favor of React.isValidElement, which according to the React docs, does what the former sought to do. Was there any reason to prefer it?

I want to do more, but didn't want this to consume me. This is how far I got.

packages/router/src/router.tsx Outdated Show resolved Hide resolved
useAuth,
paramTypes,
pageLoadingDelay,
trailingSlashes = 'never',
children,
}) => (
}: React.PropsWithChildren<RouterProps>) => (
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've seen React.FC discouraged a few times, but don't understand the argument enough to have my own opinion:

Copy link
Member

Choose a reason for hiding this comment

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

I'm not as consistent in my usage as I should be. At work we always use React.FC or React.VFC (mostly VFC because most of our components don't take children, appart from our UI lib components). Redwood don't use FC/VFC. So I'm influenced by both and end up mixing :/

packages/router/src/router.tsx Outdated Show resolved Hide resolved
@jtoar
Copy link
Contributor Author

jtoar commented Dec 22, 2021

@Tobbe the analyzeRouterTree strategy is pretty slick I have to say!

@jtoar jtoar changed the title [Router] add comments and consolidate validation logic [Router] improve comments and consolidate validation logic Dec 22, 2021

// Check for issues with the path.
validatePath(path)
path as string
Copy link
Contributor Author

@jtoar jtoar Dec 22, 2021

Choose a reason for hiding this comment

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

I added this and other assertions to illustrate what I expect. At this point, path shouldn't be optional, since if a route isn't the not-found route, it has to either be a route or a redirect route (path is required for both).

Copy link
Member

Choose a reason for hiding this comment

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

Having a type assertion on a line of its own like that, does it actually do anything?

.filter((child) => isRoute(child) && !child.props.notfound)
.forEach((child) => {
const { path, redirect, page, name } = (
child as React.Component<RouteProps & RedirectRouteProps>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another type assertion to illustrate what should only be in the array at this point.

packages/router/src/router.tsx Outdated Show resolved Hide resolved
packages/router/src/router.tsx Outdated Show resolved Hide resolved
@Tobbe
Copy link
Member

Tobbe commented Dec 22, 2021

@Tobbe I've removed the custom isReactElement function in favor of React.isValidElement, which according to the React docs, does what the former sought to do. Was there any reason to prefer it?

I didn't know about isValidElement. Thanks for showing me :) Should totally use that instead!

}

/**
* Note that this technically makes the router impure.
Copy link
Member

Choose a reason for hiding this comment

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

Now that you've pointed out that we're deviating from best-practice I kind of feel like your comment needs to include some kind of justification for our choice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just added some more copy

Copy link
Member

Choose a reason for hiding this comment

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

Looking forward to reading it when you're ready to push it 🙂

@Tobbe
Copy link
Member

Tobbe commented Dec 22, 2021

Totally subjective, but I'd prefer to keep /** JSDoc */ style comments for actual JSDoc, and just use regular // style comments for everything else.

Any reason you prefer /* */ or even /** */ style comments?

Generally my rule-of-thumb is to use /** */ for anything I'd want to show up as a tooltip when hovering something in VSCode. So pretty much only use it for exported consts and functions.

* ```
*/
flatChildArray
.filter((child) => isRoute(child) && !child.props.notfound)
Copy link
Member

Choose a reason for hiding this comment

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

See if you can include something in the big comment above about why we're skipping notfound here

@jtoar
Copy link
Contributor Author

jtoar commented Jan 21, 2023

We'll take a closer look at the router soon after React 18 goes in and we'll address these kinds of thing then.

@Tobbe
Copy link
Member

Tobbe commented Jan 22, 2023

Lot's of good stuff in this PR. Definitely think we should revisit once #4992 has landed

@jtoar jtoar deleted the ds-refactor-router branch February 27, 2023 22:49
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.

3 participants