Replies: 1 comment 1 reply
-
I actually had an identical use case, and I solved it by using If your not using useEffect(() => { // or layoutEffect if you need before paint
if (navigation.location?.key) {
console.log("ON ENTER");
} else {
console.log("AFTER ENTER");
}
}, [navigation.location?.key]);
// or using prev-state pattern in render:
const [prevLocationKey, setPrevLocationKey] = React.useState(
navigation.location?.key
);
if (prevLocationKey !== navigation.location?.key) {
if (!navigation.location?.key) {
console.log("AFTER ENTER");
} else {
console.log("ON ENTER");
setPrevLocationKey(navigation.location?.key);
}
} If you are using const navigation = useNavigation();
const { key: locationKey } = useLocation();
const isLoading = navigation.state === 'loading';
// key is used to reset the state of the progress when location changes.
// Cannot use navigation.location.key directly because navigation.location
// will be undefined when navigation is not in a loading state, causing the animation
// to cancel early because the key will change at the same time as isLoading.
// Once navigation has finished, locationKey will be the same as previous (but now undefined)
// navigation.location.key
const resetKey = navigation.location?.key || locationKey;
return <RouteProgressBar key={resetKey} isLoading={isLoading} />; |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Are there any way react router can let us tap into the callback of before enter and after enter for when the route changes?
A use case of mine is that I want to display NProgress bar when i change route, doing it in useEffect is not accurate. I am using a data browser router, it would be great if react router can expose this functionality similar to what Vue has on their navigation guard
Beta Was this translation helpful? Give feedback.
All reactions