The new Router doesn't return a Promise #49810
-
|
With the old The new router doesn't return a Promise. Is there a way to achieve a similar behavior without using my own state? Or is there a plan to implement this behavior? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 12 replies
-
|
While using userouter, it only works on Client Components. Add 'use client' on the very top of the code. |
Beta Was this translation helpful? Give feedback.
-
|
in my case, I don't know why export default function useAsyncRouter() {
const path = usePathname();
const router = useRouter();
const resolvePath = useRef<Function | null>(null);
const pushCallback = (href: string, callback?: Function) => {
router.push(href);
if (callback) {
resolvePath.current = callback;
}
};
const push = (href: string) => {
return new Promise((resolve) => pushCallback(href, resolve));
};
useEffect(() => {
resolvePath.current?.();
}, [path]);
return {
...router,
push
};
}
// usage
await router.push('somewhere') |
Beta Was this translation helpful? Give feedback.
-
|
I managed to solve it using a custom hook, which even has the advantage that you can run application code not only after the page has loaded, but in response to certain events (in my case, once a websocket connection has been established). First, I defined a in Now, in a special component which is a part of my layout, I add a My event (e.g. button-click) triggers a function that looks like: where Now, all that rests is to add a which is a bit more verbose than other solutions but super flexible and very useful !!! Hope this helps. |
Beta Was this translation helpful? Give feedback.
Take for instance:
So as the route transition is batched, you start a pending transiti…