@@ -86,13 +86,15 @@ const RouteContext = React.createContext<RouteContextObject>({
86
86
outlet : null ,
87
87
params : readOnly < Params > ( { } ) ,
88
88
pathname : "" ,
89
+ basename : "" ,
89
90
route : null
90
91
} ) ;
91
92
92
93
interface RouteContextObject {
93
94
outlet : React . ReactElement | null ;
94
95
params : Params ;
95
96
pathname : string ;
97
+ basename : string ;
96
98
route : RouteObject | null ;
97
99
}
98
100
@@ -414,7 +416,7 @@ export function useNavigate(): NavigateFunction {
414
416
) ;
415
417
416
418
let navigator = React . useContext ( NavigatorContext ) ;
417
- let { pathname } = React . useContext ( RouteContext ) ;
419
+ let { pathname, basename } = React . useContext ( RouteContext ) ;
418
420
419
421
let activeRef = React . useRef ( false ) ;
420
422
React . useEffect ( ( ) => {
@@ -427,7 +429,7 @@ export function useNavigate(): NavigateFunction {
427
429
if ( typeof to === "number" ) {
428
430
navigator . go ( to ) ;
429
431
} else {
430
- let path = resolvePath ( to , pathname ) ;
432
+ let path = resolvePath ( to , pathname , basename ) ;
431
433
( ! ! options . replace ? navigator . replace : navigator . push ) (
432
434
path ,
433
435
options . state
@@ -441,7 +443,7 @@ export function useNavigate(): NavigateFunction {
441
443
) ;
442
444
}
443
445
} ,
444
- [ navigator , pathname ]
446
+ [ basename , navigator , pathname ]
445
447
) ;
446
448
447
449
return navigate ;
@@ -473,8 +475,11 @@ export function useParams(): Params {
473
475
* @see https://reactrouter.com/api/useResolvedPath
474
476
*/
475
477
export function useResolvedPath ( to : To ) : Path {
476
- let { pathname } = React . useContext ( RouteContext ) ;
477
- return React . useMemo ( ( ) => resolvePath ( to , pathname ) , [ to , pathname ] ) ;
478
+ let { pathname, basename } = React . useContext ( RouteContext ) ;
479
+ return React . useMemo (
480
+ ( ) => resolvePath ( to , pathname , basename ) ,
481
+ [ to , pathname , basename ]
482
+ ) ;
478
483
}
479
484
480
485
/**
@@ -561,6 +566,7 @@ function useRoutes_(
561
566
outlet,
562
567
params : readOnly < Params > ( { ...parentParams , ...params } ) ,
563
568
pathname : joinPaths ( [ basename , pathname ] ) ,
569
+ basename,
564
570
route
565
571
} }
566
572
/>
@@ -971,7 +977,7 @@ function safelyDecodeURIComponent(value: string, paramName: string) {
971
977
*
972
978
* @see https://reactrouter.com/api/resolvePath
973
979
*/
974
- export function resolvePath ( to : To , fromPathname = "/" ) : Path {
980
+ export function resolvePath ( to : To , fromPathname = "/" , basename = "" ) : Path {
975
981
let {
976
982
pathname : toPathname ,
977
983
search = "" ,
@@ -981,7 +987,11 @@ export function resolvePath(to: To, fromPathname = "/"): Path {
981
987
let pathname = toPathname
982
988
? resolvePathname (
983
989
toPathname ,
984
- toPathname . startsWith ( "/" ) ? "/" : fromPathname
990
+ toPathname . startsWith ( "/" )
991
+ ? basename
992
+ ? normalizeSlashes ( `/${ basename } ` )
993
+ : "/"
994
+ : fromPathname
985
995
)
986
996
: fromPathname ;
987
997
0 commit comments