react-router 0.0.0-experimental-af76d50e → 0.0.0-experimental-dc8656c8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/LICENSE.md +3 -2
- package/README.md +2 -2
- package/dist/index.js +72 -37
- package/dist/index.js.map +1 -1
- package/dist/lib/context.d.ts +1 -0
- package/dist/main.js +1 -1
- package/dist/react-router.development.js +72 -38
- package/dist/react-router.development.js.map +1 -1
- package/dist/react-router.production.min.js +2 -2
- package/dist/react-router.production.min.js.map +1 -1
- package/dist/umd/react-router.development.js +76 -41
- package/dist/umd/react-router.development.js.map +1 -1
- package/dist/umd/react-router.production.min.js +2 -2
- package/dist/umd/react-router.production.min.js.map +1 -1
- package/package.json +2 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* React Router v0.0.0-experimental-
|
|
2
|
+
* React Router v0.0.0-experimental-dc8656c8
|
|
3
3
|
*
|
|
4
4
|
* Copyright (c) Remix Software Inc.
|
|
5
5
|
*
|
|
@@ -44,7 +44,8 @@ const LocationContext = /*#__PURE__*/React.createContext(null);
|
|
|
44
44
|
|
|
45
45
|
const RouteContext = /*#__PURE__*/React.createContext({
|
|
46
46
|
outlet: null,
|
|
47
|
-
matches: []
|
|
47
|
+
matches: [],
|
|
48
|
+
isDataRoute: false
|
|
48
49
|
});
|
|
49
50
|
|
|
50
51
|
{
|
|
@@ -149,6 +150,18 @@ function useMatch(pattern) {
|
|
|
149
150
|
} = useLocation();
|
|
150
151
|
return React.useMemo(() => matchPath(pattern, pathname), [pathname, pattern]);
|
|
151
152
|
}
|
|
153
|
+
const navigateEffectWarning = `You should call navigate() in a React.useEffect(), not when ` + `your component is first rendered.`; // Mute warnings for calls to useNavigate in SSR environments
|
|
154
|
+
|
|
155
|
+
function useIsomorphicLayoutEffect(cb) {
|
|
156
|
+
let isStatic = React.useContext(NavigationContext).static;
|
|
157
|
+
|
|
158
|
+
if (!isStatic) {
|
|
159
|
+
// We should be able to get rid of this once react 18.3 is released
|
|
160
|
+
// See: https://github.com/facebook/react/pull/26395
|
|
161
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
162
|
+
React.useLayoutEffect(cb);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
152
165
|
/**
|
|
153
166
|
* Returns an imperative method for changing the location. Used by <Link>s, but
|
|
154
167
|
* may also be used by other elements to change the location.
|
|
@@ -156,17 +169,21 @@ function useMatch(pattern) {
|
|
|
156
169
|
* @see https://reactrouter.com/hooks/use-navigate
|
|
157
170
|
*/
|
|
158
171
|
|
|
172
|
+
|
|
159
173
|
function useNavigate() {
|
|
160
|
-
let
|
|
174
|
+
let {
|
|
175
|
+
isDataRoute
|
|
176
|
+
} = React.useContext(RouteContext); // Conditional usage is OK here because the usage of a data router is static
|
|
161
177
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
162
178
|
|
|
163
|
-
return
|
|
179
|
+
return isDataRoute ? useNavigateStable() : useNavigateUnstable();
|
|
164
180
|
}
|
|
165
181
|
|
|
166
182
|
function useNavigateUnstable() {
|
|
167
183
|
!useInRouterContext() ? UNSAFE_invariant(false, // TODO: This error is probably because they somehow have 2 versions of the
|
|
168
184
|
// router loaded. We can help them understand how to avoid that.
|
|
169
185
|
`useNavigate() may be used only in the context of a <Router> component.`) : void 0;
|
|
186
|
+
let dataRouterContext = React.useContext(DataRouterContext);
|
|
170
187
|
let {
|
|
171
188
|
basename,
|
|
172
189
|
navigator
|
|
@@ -179,11 +196,13 @@ function useNavigateUnstable() {
|
|
|
179
196
|
} = useLocation();
|
|
180
197
|
let routePathnamesJson = JSON.stringify(UNSAFE_getPathContributingMatches(matches).map(match => match.pathnameBase));
|
|
181
198
|
let activeRef = React.useRef(false);
|
|
182
|
-
|
|
199
|
+
useIsomorphicLayoutEffect(() => {
|
|
183
200
|
activeRef.current = true;
|
|
184
201
|
});
|
|
185
202
|
let navigate = React.useCallback((to, options = {}) => {
|
|
186
|
-
UNSAFE_warning(activeRef.current,
|
|
203
|
+
UNSAFE_warning(activeRef.current, navigateEffectWarning) ; // Short circuit here since if this happens on first render the navigate
|
|
204
|
+
// is useless because we haven't wired up our history listener yet
|
|
205
|
+
|
|
187
206
|
if (!activeRef.current) return;
|
|
188
207
|
|
|
189
208
|
if (typeof to === "number") {
|
|
@@ -192,16 +211,18 @@ function useNavigateUnstable() {
|
|
|
192
211
|
}
|
|
193
212
|
|
|
194
213
|
let path = resolveTo(to, JSON.parse(routePathnamesJson), locationPathname, options.relative === "path"); // If we're operating within a basename, prepend it to the pathname prior
|
|
195
|
-
// to handing off to history
|
|
196
|
-
//
|
|
197
|
-
//
|
|
214
|
+
// to handing off to history (but only if we're not in a data router,
|
|
215
|
+
// otherwise it'll prepend the basename inside of the router).
|
|
216
|
+
// If this is a root navigation, then we navigate to the raw basename
|
|
217
|
+
// which allows the basename to have full control over the presence of a
|
|
218
|
+
// trailing slash on root links
|
|
198
219
|
|
|
199
|
-
if (basename !== "/") {
|
|
220
|
+
if (dataRouterContext == null && basename !== "/") {
|
|
200
221
|
path.pathname = path.pathname === "/" ? basename : joinPaths([basename, path.pathname]);
|
|
201
222
|
}
|
|
202
223
|
|
|
203
224
|
(!!options.replace ? navigator.replace : navigator.push)(path, options.state, options);
|
|
204
|
-
}, [basename, navigator, routePathnamesJson, locationPathname]);
|
|
225
|
+
}, [basename, navigator, routePathnamesJson, locationPathname, dataRouterContext]);
|
|
205
226
|
return navigate;
|
|
206
227
|
}
|
|
207
228
|
|
|
@@ -518,6 +539,14 @@ function _renderMatches(matches, parentMatches = [], dataRouterState = null) {
|
|
|
518
539
|
|
|
519
540
|
if (error) {
|
|
520
541
|
children = errorElement;
|
|
542
|
+
} else if (match.route.Component) {
|
|
543
|
+
// Note: This is a de-optimized path since React won't re-use the
|
|
544
|
+
// ReactElement since it's identity changes with each new
|
|
545
|
+
// React.createElement call. We keep this so folks can use
|
|
546
|
+
// `<Route Component={...}>` in `<Routes>` but generally `Component`
|
|
547
|
+
// usage is only advised in `RouterProvider` when we can convert it to
|
|
548
|
+
// `element` ahead of time.
|
|
549
|
+
children = /*#__PURE__*/React.createElement(match.route.Component, null);
|
|
521
550
|
} else if (match.route.element) {
|
|
522
551
|
children = match.route.element;
|
|
523
552
|
} else {
|
|
@@ -528,7 +557,8 @@ function _renderMatches(matches, parentMatches = [], dataRouterState = null) {
|
|
|
528
557
|
match: match,
|
|
529
558
|
routeContext: {
|
|
530
559
|
outlet,
|
|
531
|
-
matches
|
|
560
|
+
matches,
|
|
561
|
+
isDataRoute: dataRouterState != null
|
|
532
562
|
},
|
|
533
563
|
children: children
|
|
534
564
|
});
|
|
@@ -545,7 +575,8 @@ function _renderMatches(matches, parentMatches = [], dataRouterState = null) {
|
|
|
545
575
|
children: getChildren(),
|
|
546
576
|
routeContext: {
|
|
547
577
|
outlet: null,
|
|
548
|
-
matches
|
|
578
|
+
matches,
|
|
579
|
+
isDataRoute: true
|
|
549
580
|
}
|
|
550
581
|
}) : getChildren();
|
|
551
582
|
}, null);
|
|
@@ -761,7 +792,16 @@ function useNavigateStable() {
|
|
|
761
792
|
router
|
|
762
793
|
} = useDataRouterContext(DataRouterHook.UseNavigateStable);
|
|
763
794
|
let id = useCurrentRouteId(DataRouterStateHook.UseNavigateStable);
|
|
795
|
+
let activeRef = React.useRef(false);
|
|
796
|
+
useIsomorphicLayoutEffect(() => {
|
|
797
|
+
activeRef.current = true;
|
|
798
|
+
});
|
|
764
799
|
let navigate = React.useCallback((to, options = {}) => {
|
|
800
|
+
UNSAFE_warning(activeRef.current, navigateEffectWarning) ; // Short circuit here since if this happens on first render the navigate
|
|
801
|
+
// is useless because we haven't wired up our router subscriber yet
|
|
802
|
+
|
|
803
|
+
if (!activeRef.current) return;
|
|
804
|
+
|
|
765
805
|
if (typeof to === "number") {
|
|
766
806
|
router.navigate(to);
|
|
767
807
|
} else {
|
|
@@ -791,16 +831,10 @@ function RouterProvider({
|
|
|
791
831
|
fallbackElement,
|
|
792
832
|
router
|
|
793
833
|
}) {
|
|
794
|
-
|
|
834
|
+
// Need to use a layout effect here so we are subscribed early enough to
|
|
795
835
|
// pick up on any render-driven redirects/navigations (useEffect/<Navigate>)
|
|
796
|
-
|
|
797
|
-
React.useLayoutEffect(() =>
|
|
798
|
-
return router.subscribe(newState => {
|
|
799
|
-
if (newState !== state) {
|
|
800
|
-
setState(newState);
|
|
801
|
-
}
|
|
802
|
-
});
|
|
803
|
-
}, [router, state]);
|
|
836
|
+
let [state, setState] = React.useState(router.state);
|
|
837
|
+
React.useLayoutEffect(() => router.subscribe(setState), [router, setState]);
|
|
804
838
|
let navigator = React.useMemo(() => {
|
|
805
839
|
return {
|
|
806
840
|
createHref: router.createHref,
|
|
@@ -908,22 +942,22 @@ function Navigate({
|
|
|
908
942
|
// the router loaded. We can help them understand how to avoid that.
|
|
909
943
|
`<Navigate> may be used only in the context of a <Router> component.`) : void 0;
|
|
910
944
|
UNSAFE_warning(!React.useContext(NavigationContext).static, `<Navigate> must not be used on the initial render in a <StaticRouter>. ` + `This is a no-op, but you should modify your code so the <Navigate> is ` + `only ever rendered in response to some user interaction or state change.`) ;
|
|
911
|
-
let
|
|
912
|
-
|
|
913
|
-
React.
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
});
|
|
945
|
+
let {
|
|
946
|
+
matches
|
|
947
|
+
} = React.useContext(RouteContext);
|
|
948
|
+
let {
|
|
949
|
+
pathname: locationPathname
|
|
950
|
+
} = useLocation();
|
|
951
|
+
let navigate = useNavigate(); // Resolve the path outside of the effect so that when effects run twice in
|
|
952
|
+
// StrictMode they navigate to the same place
|
|
953
|
+
|
|
954
|
+
let path = resolveTo(to, UNSAFE_getPathContributingMatches(matches).map(match => match.pathnameBase), locationPathname, relative === "path");
|
|
955
|
+
let jsonPath = JSON.stringify(path);
|
|
956
|
+
React.useEffect(() => navigate(JSON.parse(jsonPath), {
|
|
957
|
+
replace,
|
|
958
|
+
state,
|
|
959
|
+
relative
|
|
960
|
+
}), [navigate, jsonPath, relative, replace, state]);
|
|
927
961
|
return null;
|
|
928
962
|
}
|
|
929
963
|
/**
|