Cannot update a component (`Router`) while rendering a different component (`Page`).
Answered
White-tailed Tropicbird posted this in #help-forum
White-tailed TropicbirdOP
Cannot update a component (
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:
I've narrowed the problem down to the
If I don't use the
This must be some kind of a bug...
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-renderI 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"
1 Reply
White-tailed TropicbirdOP
Okay, nevermind, this was happening because
checkRegion()
was defined in a component that was marked with "use server"
Answer