How to Properly Handle Logout page Followed by Login page in React without Causing Page Re-renders?

I’m working on a React application where I need to handle user inactivity by logging them out and then redirect them back to login page. However, I’m encountering an issue where the page renders twice, causing a noticeable flicker or delay.

The component like this:

export const LogoutInactivityTimeout = () => {
  const { search, pathname } = useLocation()
  const { loginWithInactiveTimeout, logout } = useAuthenticationContext()
  const redirect = new URLSearchParams(search).get('redirect') ?? ''
  const isReturningFromLogout =
    new URLSearchParams(search).get('isReturningFromLogout') === 'true' || false

  useEffect(() => {
    if (!isReturningFromLogout) {
      logout({
        returnTo: `${window.location.origin}${pathname}?redirect=${redirect}&isReturningFromLogout=true`,
      })
    } else {
      loginWithInactiveTimeout(redirect)
    }
  }, [
    logout,
    loginWithInactiveTimeout,
    isReturningFromLogout,
    pathname,
    redirect,
  ])

  return (
    <AuthLoadingPage>
...
    </AuthLoadingPage>
  )
}

When isReturningFromLogout is false, the logout function is triggered, which logs the user out and redirects them back to the same component with the isReturningFromLogout parameter set to true. At this point, the loginWithInactiveTimeout function is called to direct the user to login page.

This approach causes the page to render twice—once during the logout process and again during the login process. This creates a flicker in the user experience that I’d like to avoid.

How can I handle the logout and login process in a way that makes sure the login is called after the logout finished to prevent the page from rendering twice?

I’ve tried wrapping the logout function in a Promise, and use .then() but since the logout function from Auth0 doesn’t return a Promise, this approach didn’t work as expected.

Is there a more efficient way to handle this process in React, or should I be considering a different approach altogether?