-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
useParam is better to return undefined when the param is missing #616
Comments
@isoppp yeah, you have a great point. We should change |
To pull from some of the more back end minded techs: I always loved the: mixedType (number OR undefined) That way you could inject the typed return value rather than hardcoding it. |
We've talked about the implications of type checking URL parameters before, but now that nextjs supports rewrites, we could revisit the idea of defining a type mapping for parameters and lettings blitz generate the corresponding rewrites to enforce it. That would make it impossible for the param to have an invalid value in the first place. |
@MrLeebo regardless if we add that, there will still be the case of |
I did a PR above, but still remaining issue in regards to |
However, @isoppp, it's not great / strictly correct to do ... in this case For reference - this is the fun number parsing I use in my project for parsing query params: export function isNumeric(value: string) {
return /^[+-]?\d+(\.\d+)?$/.test(value);
}
export function parseFloatOrNull(value?: unknown): number | null {
if (typeof value === 'string' && isNumeric(value)) {
const float = parseFloat(value);
if (!Number.isNaN(float)) {
return float;
}
}
return null;
} |
What do you want and why?
if(param)
butNaN
will betrue
. It might not be useful.useParams
andparam
is not consistent, I'm a bit confusing.number | undefined
, but never returned undefined I guesshttps://github.com/blitz-js/blitz/blob/canary/packages/core/src/use-params.ts#L92
Possible implementation(s)
Want to change these test results to be
undefined
.useParam("doesnt-exist", TYPE)
in https://github.com/blitz-js/blitz/blob/canary/packages/core/test/use-params.test.tsor
change type definition such as
number | undefined
to benumber
if it truly never returnedundefined
Additional context
When first render,
useRouter().query
is always return empty object {} as mentioned in #962#1334, this modification might be helpful.The text was updated successfully, but these errors were encountered: