Skip to content

Commit 073a786

Browse files
authored
feat(react-router): Make navigate and setSearchParams consistently return a Promise<void>
1 parent e5bce7d commit 073a786

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

packages/react-router/lib/dom/lib.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2313,7 +2313,7 @@ export function useSearchParams(
23132313
: nextInit,
23142314
);
23152315
hasSetSearchParamsRef.current = true;
2316-
navigate("?" + newSearchParams, navigateOptions);
2316+
return navigate("?" + newSearchParams, navigateOptions);
23172317
},
23182318
[navigate, searchParams],
23192319
);

packages/react-router/lib/hooks.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ export function useMatch<
195195
* The interface for the `navigate` function returned from {@link useNavigate}.
196196
*/
197197
export interface NavigateFunction {
198-
(to: To, options?: NavigateOptions): void | Promise<void>;
199-
(delta: number): void | Promise<void>;
198+
(to: To, options?: NavigateOptions): Promise<void>;
199+
(delta: number): Promise<void>;
200200
}
201201

202202
const navigateEffectWarning =
@@ -355,11 +355,11 @@ function useNavigateUnstable(): NavigateFunction {
355355

356356
// Short circuit here since if this happens on first render the navigate
357357
// is useless because we haven't wired up our history listener yet
358-
if (!activeRef.current) return;
358+
if (!activeRef.current) return Promise.resolve();
359359

360360
if (typeof to === "number") {
361361
navigator.go(to);
362-
return;
362+
return Promise.resolve();
363363
}
364364

365365
let path = resolveTo(
@@ -387,6 +387,8 @@ function useNavigateUnstable(): NavigateFunction {
387387
options.state,
388388
options,
389389
);
390+
391+
return Promise.resolve();
390392
},
391393
[
392394
basename,
@@ -1757,9 +1759,9 @@ function useNavigateStable(): NavigateFunction {
17571759
if (!activeRef.current) return;
17581760

17591761
if (typeof to === "number") {
1760-
router.navigate(to);
1762+
return router.navigate(to);
17611763
} else {
1762-
await router.navigate(to, { fromRouteId: id, ...options });
1764+
return router.navigate(to, { fromRouteId: id, ...options });
17631765
}
17641766
},
17651767
[router, id],

packages/react-router/lib/router/router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1490,7 +1490,7 @@ export function createRouter(init: RouterInit): Router {
14901490
location: nextLocation,
14911491
});
14921492
// Send the same navigation through
1493-
navigate(to, opts);
1493+
return navigate(to, opts);
14941494
},
14951495
reset() {
14961496
let blockers = new Map(state.blockers);

0 commit comments

Comments
 (0)