With the old pages
router, I could await
router.push
. I used this to keep a button disabled while we are in the process of navigating to another page:
async function onSubmit(input: CreatePostFormData) {
try {
await BlogApi.createBlogPost(input);
await router.push("/blog/" + slug);
} catch (error) {
console.error(error);
alert(error);
}
}
The new router doesn't return a Promise. Is there a way to achieve a similar behavior without using my own state?
In the past, there was a capability where you could wait for a route transition to complete. However, due to some changes or limitations, this capability is no longer available.
As an alternative, the suggestion is to attempt wrapping the call to router.push (a method that triggers a route transition) with a startTransition hook. Here it is an example: