-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[Bug]: useNavigate's NavigateFunction is mixing async and non-async return types #12348
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
Comments
use this:
|
Hey @timdorr! Would you mind sharing the rationale behind closing? Not saying it's wrong in any way, just interested to know what you consider to be the solution. Is it one of the suggestions mentioned above? Or was there a code update? |
Is this actually the way we're supposed to invoke It seems more ideomatic to make this return a |
@timdorr can you reopen this issue? |
@brookslybrand Is the team aware of this type issue? This issue was closed but I don't think a good solution was presented |
The tricky part about The data router version returns the promise from Changing the return type to I reopened this so we can figure out the best recommendation for usage with function useNavigatePromise(...args) {
return useNavigate(...args) as (args: Parameters<NavigateFunction>) => Promise<void>
} |
FTR that happens here. Why are they called "stable" and "unstable"? One solution could be to expose both of those functions separately, for users who know what they're getting. More concretely, have
Is it thinkable that this becomes a Promise in the future too, when the non-data-router version also supports data loading or for other reasons? (I have no idea whether this question even makes sense to ask.) Would doing an extra
I guess it would be the same amount of breaking as the change to |
Because the architecture of |
I see, thanks for the explanation! So the stable/unstable situation is conceptually unrelated to the void/promise situation, just in today's world it happens to be fully correlated. That naming is fine for those two internal functions, but perhaps not worth adopting when exposing two different functions (I guess?), if that's even a reasonable solution in the first place (if so, I proposed two other names above). |
What is the current recommendation from the react-router team? I guess we need a matrix of: React 18/19 and data router / non-data-router. The documentation at https://reactrouter.com/start/library/navigating#usenavigate seems inadequate now that it's a bit more complex of a situation in v7. |
Do we really expect everyone calling |
I'm just using a custom hook for now but yeah, this should be handled by the library itself. import { useNavigate } from 'react-router';
import type { NavigateOptions, To } from 'react-router';
type NavigateFunctionVoid = {
(to: To, options?: NavigateOptions): void;
(delta: number): void;
};
const useNavigateVoid = () => useNavigate() as NavigateFunctionVoid;
export default useNavigateVoid; |
To avoid floating-promises error, for now, we are using void when calling const navigate = useNavigate()
const takeMeToTheNextPage = () {
void navigate('nextPage');
} But would be awesome to see the recommended way from the react-router team. |
navigate('/'); triggers @typescript-eslint/no-floating-promises. void navigate('/'); triggers no-void. |
What version of React Router are you using?
7.0.1
Steps to Reproduce
Initialize navigate
Expected Behavior
The
navigate
function return type should be either:void
or
This would make it clear whether we need to handle a promise or not, making the behavior of
navigate
more predictable.Actual Behavior
navigate
return type isThe text was updated successfully, but these errors were encountered: