You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Suppose you have two pages A and B with <PageAComponent> and <PageBComponent>, then when you navigate from A to B, AnimatePresence will hold onto <PageAComponent> while it does an exit animation, and then switch over to <PageBComponent>.
This all works pretty well with Next.js but crucially, useRouter() within <PageAComponent> will return unexpected results. In this case, while PageAComponent is doing an exit animation, useRouter().asPath inside PageA will return /pageB. (Which follows logically from the code as there is one global RouterContext)
Describe the solution you'd like
I would like it if RouterContext were more tightly applied to <Component>, so that calling useRouter() within PageAComponent returned the last router that was used to show PageA. Then in that case, useRouter().asPath would return /pageA.
I took a quick look at next/client/index.tsx and couldn't find an easy way to do this since Component is passed as a class rather than rendered instance (which of course, has other benefits). You could maybe wrap it in a component that rendered a RouteContext.Provider but that would break anyone using displayName or any other custom properties they might set.
Describe alternatives you've considered
Our workaround for this is a bit hacky but our _app.tsx is structured like:
This discussion was converted from issue #28597 on August 31, 2021 07:16.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the feature you'd like to request
Right now I am using AnimatePresence from Framer's motion library to do full page transitions, which works pretty well by doing something like:
Suppose you have two pages A and B with
<PageAComponent>and<PageBComponent>, then when you navigate from A to B, AnimatePresence will hold onto<PageAComponent>while it does an exit animation, and then switch over to<PageBComponent>.This all works pretty well with Next.js but crucially, useRouter() within
<PageAComponent>will return unexpected results. In this case, while PageAComponent is doing an exit animation, useRouter().asPath inside PageA will return/pageB. (Which follows logically from the code as there is one global RouterContext)Describe the solution you'd like
I would like it if
RouterContextwere more tightly applied to<Component>, so that callinguseRouter()withinPageAComponentreturned the last router that was used to show PageA. Then in that case,useRouter().asPathwould return/pageA.I took a quick look at
next/client/index.tsxand couldn't find an easy way to do this sinceComponentis passed as a class rather than rendered instance (which of course, has other benefits). You could maybe wrap it in a component that rendered aRouteContext.Providerbut that would break anyone usingdisplayNameor any other custom properties they might set.Describe alternatives you've considered
Our workaround for this is a bit hacky but our _app.tsx is structured like:
and then within the PageAComponent, we will cache the router path by doing:
Overall this is not great as we only get the first value of
asPathrather than the last.Another workaround which I am planning to try is:
which though it makes sense to me, I haven't tested to see if it actually solves the problem.
Beta Was this translation helpful? Give feedback.
All reactions