Next.js Discord

Discord Forum

router.push() in UseEffect vs onSubmit

Answered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
I have these code snippets. When I use router.push() inside useEffect, it loads the loading.tsx. But when I use it inside an onSubmit(), it doesn't load the loading.tsx.

Any suggestions? What's the best option? Thanks in advance!

function onSubmit(storeParams) {
    router.push(
      `${pathname}?${createQueryString({
        store_ids: encrypt(storeParams),
      })}`,
      {
        scroll: false,
      },
    );
  }


React.useEffect(() => {
  startTransition(() => {
    router.push(
      `${pathname}?${createQueryString({
          store_ids: storeParams ? encrypt(storeParams) : null,
        })}`,
    );
  });
  // eslint-disable-next-line react-hooks/exhaustive-deps
}, [storeParams]);
Answered by riský
well this is what vercel does: [website](https://demo.vercel.store/search?q=a), [github](https://github.com/vercel/commerce/blob/main/components/layout/navbar/search.tsx) and it doesn't have loading, so i am inclined to think it isn't possible in a simple manner
View full answer

8 Replies

well for starters your useeffect has startTransition, and onsubmit doesn't
Asiatic LionOP
Yes, but it's irrelevant for this case
so if you put transition for both, that doesn't change it?
Asiatic LionOP
ey @riský sorry for the delay. I added the startTransition() to the onSubmit() function, but there were no changes.
function onSubmit(storeParams) {
    startTransition(() => {
      router.push(
        `${pathname}?${createQueryString({
          store_ids: encrypt(storeParams),
        })}`,
        {
          scroll: false,
        },
      );
    });
  }
Asiatic LionOP
Hello @riský any suggestions?
well this is what vercel does: [website](https://demo.vercel.store/search?q=a), [github](https://github.com/vercel/commerce/blob/main/components/layout/navbar/search.tsx) and it doesn't have loading, so i am inclined to think it isn't possible in a simple manner
Answer
Asiatic LionOP
Ok thanks :thinq:
yeah, so you can mark as solution if you want to.... but it isn't that helpful of an answer 😭