Skip to content

[v6] concerning optional URL params #8566

Closed Answered by chaance
Jazzmanpw asked this question in Q&A
Discussion options

You must be logged in to vote

@Jazzmanpw You answered your own question, I believe 😄

it's a guard from putting a hook under a route that doesn't have this parameter

Hooks can be called from anywhere, and the library can't be smart enough to know for sure that your params will be defined. We chose to optimize for real type safety here, and that means acknowledging that a parameter might be undefined.

it forces us to write some guards with undefined checks that should never run

Not exactly. If you are certain your parameter will be defined, you can use type casting or the non-null assertion operator to avoid a runtime check.

let { param } = useParams() as { param: "definitely defined" }
// or
let { param } = useParams<

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@ericbf
Comment options

@Jazzmanpw
Comment options

@ericbf
Comment options

Answer selected by Jazzmanpw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants