#redirect function fail when refresh page

17 messages · Page 1 of 1 (latest)

rare oriole
#

Hi, I'm facing a problem with redirect function from next/navigation. When the profile object is null and I try to access the route /my-profle, an error occurs "react-dom.development.js:11003 Uncaught Error: Rendered more hooks than during the previous render".
It worked fine when soft navigation. The error only occurs when I try hard navigation. Can anyone help me ?

/my-profle
const page = () => {

  const { profile } = useAppSelector((state) => state.authReducer);

  if (!profile?.accessToken) {
    return redirect("/");
  }

  return (
    <p className="mt-[10px]">
        hello
    </p>
  );
};
vast tartanBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

daring plank
rare oriole
#

I have tried that way but I dont want the content rendered before I make sure the user logged in

rare oriole
#

I have another component uses redirect function same as the code above but the error did not occur when I accessed the route without logging in

rare oriole
daring plank
daring plank
#
const router = useRouter()
const { isLoading, profile } = useAppSelector((state) => state.authReducer);

useEffect(() => {
  if (!profile?.accessToken) {
    router.push("/")
  }
}, [profile])

if (isLoading) return <div>loading...</div>

...
rare oriole
#

How can I define it true or false? For example, When I not log in and I try to access '/my-profile' then it still render the content below right?

daring plank
#

true before you load the profile data
false after you load the profile data

rare oriole
#

I mean the profile data is only loaded when I try to login. But the case I mention is I do not login and try to access the route by modifying the url. Sorry, If I misunderstand your way

daring plank
#

then just return null if profile is not there

rare oriole
rare oriole