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

[Bug]: useNavigate, <Link> and <Navigate> 'to' types #11267

Closed
KKaldis opened this issue Feb 11, 2024 · 3 comments
Closed

[Bug]: useNavigate, <Link> and <Navigate> 'to' types #11267

KKaldis opened this issue Feb 11, 2024 · 3 comments
Labels

Comments

@KKaldis
Copy link

KKaldis commented Feb 11, 2024

What version of React Router are you using?

6.3.0

Steps to Reproduce

i want to navigate to previous hisotry url (-1)

onClick={() => navigate(-1 as string | number)}
onClick={() => navigate(-1 as string)}
onClick={() => navigate(-1 as number)}

it does work even with type error

Expected Behavior

accept both number | string

Actual Behavior

on useNavigate

No overload matches this call.
Overload 1 of 2, '(to: To, options?: NavigateOptions | undefined): void', gave the following error.
Argument of type 'string | number' is not assignable to parameter of type 'To'.
Type 'number' is not assignable to type 'To'.
Overload 2 of 2, '(delta: number): void', gave the following error.
Argument of type 'string | number' is not assignable to parameter of type 'number'.
Type 'string' is not assignable to type 'number'.ts(2769)

on <Navigate>

Type 'number' is not assignable to type 'To'.ts(2322)
index.d.ts(53, 5): The expected type comes from property 'to' which is declared here on type 'IntrinsicAttributes & LinkProps & RefAttributes'

@KKaldis KKaldis added the bug label Feb 11, 2024
@brophdawg11
Copy link
Contributor

If you want to go back - useNavigate is the proper API which accepts a number:

interface NavigateFunction {
  (to: To, options?: NavigateOptions): void;
  (delta: number): void;
}

It doesn't work if you explicitly cast it to string | number since that union doesn't fit either of the overloads, but you can use To objects, strings, or numbers individually without issue:

Screenshot 2024-02-12 at 11 45 11 AM

It doesn't really make sense for <Navigate> to accept a number IMO because going back should only be in response to a user-action - whereas Navigate is intended for redirects. It wouldn't make sense to land somewhere jut to be routed backwards without taking any user action.

<Link> does not accept a number because when going back in the history stack there is no way to determine what URL you are going back to, so we cannot render a <a href>.

@brophdawg11 brophdawg11 closed this as not planned Won't fix, can't repro, duplicate, stale Feb 12, 2024
@PauloBarrantes
Copy link

PauloBarrantes commented Mar 11, 2024

@brophdawg11 Why is not possible to have the option to go back on the Link Component, something like this: <Link to={-1}

@brophdawg11
Copy link
Contributor

<Link> does not accept a number because when going back in the history stack there is no way to determine what URL you are going back to, so we cannot render a <a href>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants