Replies: 2 comments 2 replies
-
I'd use the .with({ name: 'UserDetail' }, ({params: {userId}}) => !Number.isNaN(parseInt(userId)), (p) => <User id={parseInt(p.params.userId)} />) Having
|
Beta Was this translation helpful? Give feedback.
1 reply
-
Note that you could create a custom, reusable const intPattern = P.when(
(x) => typeof x === "string" && Number.isFinite(Number.parseInt(x)),
);
const Example = () => {
const route = Router.useRoute(["UserDetail"]);
return (
match(route)
.with({ name: "UserDetail", params: { userId: intPattern } }, (p) => (
<User id={Number.parseInt(p.params.userId)} /> // it's safe to parse the userId here
))
.otherwise(() => <div>404 Not found</div>)
);
}; |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
How does everybody handle params that need to be numbers (well integers)? Currently I am having to do use
parseInt
such as:However it's possible that
userId
isNaN
, and therefore unexpectedly breaks when used.As chicane already has array and union types, would it work to be able to also describe basic types such as integers or numbers?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions