Next.js Discord

Discord Forum

Cannot update a component (`Router`) while rendering a different component (`Page`).

Answered
White-tailed Tropicbird posted this in #help-forum
Open in Discord
Avatar
White-tailed TropicbirdOP
Cannot update a component (Router) while rendering a different component (Page). To locate the bad setState() call inside Page, follow the stack trace as described in https://reactjs.org/link/setstate-in-render

I looked at the [github comment](https://reactjs.org/link/setstate-in-render) linked in the error, but that has nothing to do with how my component works.
Here it is:
export default function Page() {
  const params = useSearchParams();
  const platformParams = params.get("r");

  let platform;
  if (platformParams && checkRegion(platformParams)) {
    platform = platformParams as Platform;
  }

  return // not important
};

I've narrowed the problem down to the checkRegion() function, which looks like this:
export const checkRegion = (platform: Platform | string): boolean => {
  switch (platform) {
    case "NA1":
    case "BR1":
    // (...)
    case "TW2":
    case "VN2":
      return true;
    default:
      return false;
  }
};

If I don't use the checkRegion() function the error doesn't appear.
This must be some kind of a bug...
Answered by White-tailed Tropicbird
Okay, nevermind, this was happening because checkRegion() was defined in a component that was marked with "use server"
View full answer

1 Reply

Avatar
White-tailed TropicbirdOP
Okay, nevermind, this was happening because checkRegion() was defined in a component that was marked with "use server"
Answer