# Error: Invalid state when calling handleRedirectCallback on React App

**URL:** <https://community.auth0.com/t/error-invalid-state-when-calling-handleredirectcallback-on-react-app/95329>\
**Category:** Get Help\
**Tags:** classic-universal-login-experience, login-experience\
**Created:** [November 29, 2022, 10:43pm UTC](https://community.auth0.com/t/error-invalid-state-when-calling-handleredirectcallback-on-react-app/95329 "2022-11-29T22:43:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabrielferraz](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/gabrielferraz/32/11278_2.png) [@gabrielferraz](https://community.auth0.com/u/gabrielferraz)\
**Post date:** [November 29, 2022, 10:43pm UTC](https://community.auth0.com/t/error-invalid-state-when-calling-handleredirectcallback-on-react-app/95329/1 "2022-11-29T22:43:03Z")

</div>

**Platform** : “react”: “^18.2.0”

**Routing Library** : “react-router-dom”: “6.4.2”

**Auth0 library** : “@auth0/auth0-spa-js”: “^2.0.0”

Issue: getting “Error: Invalid state” when calling `handleRedirectCallback`. Not sure what’s going on on the background but the login completes successfully after the error.

 ![Screen Shot 2022-11-29 at 5.39.31 PM](https://global.discourse-cdn.com/auth0/original/3X/4/5/459c0fd080f9a00117001fbb2b38d5d51eba9666.png)

Setup:

Auth.js

```auto
const auth0 = new Auth0Client({
	authorizationParams: {
		audience: <my-audience>,
		redirect_uri: <my-url>/callback,
	},
	cacheLocation: 'localstorage',
	clientId: <my-clientId>,
	domain: <my-domain>,
	useRefreshTokens: true,
});

export const login = async () => {
	await auth0.loginWithRedirect();
};

export const isAuthenticated = () => {
      return auth0.isAuthenticated();
};

export const handleRedirectCallback = () => {
	return auth0.handleRedirectCallback();
};

export const login = async () => {
	await auth0.loginWithRedirect();
};

```

App.js

```auto
<Routes>
	<Route path="/login" element={<Login />} />
	<Route path="/callback" element={<Auth0Callback />} />
	<Route path="/" element={<AuthenticatedRoute component={AppLayout} />}>
                // routes children of AuthenticatedRoute
    </Route>
</Routes>

```

Auth0Callback.js

```auto
export default function Auth0Callback() {
	const navigate = useNavigate();

	useEffect(() => {
		(async () => {
			try {
				await handleRedirectCallback();
				navigate(localStorage.getItem('redirectTo') || '/');
			} catch (e) {
				console.log('e', e); // ERROR IS LOGGED HERE
			}
		})();
	}, [navigate]);

	return (
		<SpinnerContainer>
			<Spinner />
		</SpinnerContainer>
	);
}

```

AuthenticatedRoute.js

```auto
export const AuthenticatedRoute = ({ component: Component }) => {
	const location = useLocation();
	const [authenticated, setAuthenticated] = useState(false);

	useEffect(() => {
		(async () => {
			const authenticated = await isAuthenticated();
			setAuthenticated(authenticated);

			if (!authenticated) {
				localStorage.setItem('redirectTo', location.pathname);
				login();
			}
		})();

		return;
	}, [location.pathname]);

	return authenticated ? (
		<Component />
	) : (
		<SpinnerContainer>
			<Spinner />
		</SpinnerContainer>
	);
};

export default AuthenticatedRoute;

```

---

<div class="post-metadata">

**Author:** ![gabrielferraz](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/gabrielferraz/32/11278_2.png) [@gabrielferraz](https://community.auth0.com/u/gabrielferraz)\
**Post date:** [November 30, 2022, 4:59pm UTC](https://community.auth0.com/t/error-invalid-state-when-calling-handleredirectcallback-on-react-app/95329/2 "2022-11-30T16:59:38Z")

</div>

The issue is that in React 18 the `useEffect` hook is called twice (thus `handleRedirectCallback` is called twice as well) because the component mounts, unmounts, then mounts again. I removed `<React.StrictMode>` to get the old behavior of `useEffect` and the bug is not happening anymore. This is not the final solution, but at least it shows what is causing the bug.

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [November 30, 2022, 5:16pm UTC](https://community.auth0.com/t/error-invalid-state-when-calling-handleredirectcallback-on-react-app/95329/3 "2022-11-30T17:16:05Z")

</div>

I’ll mark it temporarily as a solution but can I also ask you to raise it as aGitHub issue so we can discuss it directly with the SDK maintainers? Thank you!

---

<div class="post-metadata">

**Author:** ![gabrielferraz](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/gabrielferraz/32/11278_2.png) [@gabrielferraz](https://community.auth0.com/u/gabrielferraz)\
**Post date:** [December 1, 2022, 1:26am UTC](https://community.auth0.com/t/error-invalid-state-when-calling-handleredirectcallback-on-react-app/95329/4 "2022-12-01T01:26:46Z")

</div>

This is my final solution. I am not sure how to raise as a github issue.

```auto
export default function Auth0Callback() {
	const navigate = useNavigate();
	const shouldRedirect = useRef(true);

	useEffect(() => {
		if (shouldRedirect.current) {
			shouldRedirect.current = false;

			(async () => {
				try {
					await handleRedirectCallback();
					navigate(localStorage.getItem('redirectTo') || '/');
				} catch (e) {
					navigate('/500');
				}
			})();
		}
	}, [navigate]);

	return (
		<SpinnerContainer>
			<Spinner />
		</SpinnerContainer>
	);
}

```

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [December 2, 2022, 11:18am UTC](https://community.auth0.com/t/error-invalid-state-when-calling-handleredirectcallback-on-react-app/95329/5 "2022-12-02T11:18:11Z")

</div>

Thanks for sharing it with the rest of community!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [December 16, 2022, 11:18am UTC](https://community.auth0.com/t/error-invalid-state-when-calling-handleredirectcallback-on-react-app/95329/6 "2022-12-16T11:18:49Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.

---

<div class="post-metadata">

**Author:** ![dan-auth0](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@dan-auth0](https://community.auth0.com/u/dan-auth0)\
**Post date:** [January 6, 2023, 1:35am UTC](https://community.auth0.com/t/error-invalid-state-when-calling-handleredirectcallback-on-react-app/95329/7 "2023-01-06T01:35:27Z")

</div>

Hello, I recommend using the Auth0 React SDK instead of the Auth0 SPA SDK for your React application.

You can also consult our newly released [React Authentication By Example: Using React Router 6](https://developer.auth0.com/resources/guides/spa/react/basic-authentication) 🎉 for guidance on how to integrate Auth0 with a React Router `v6` application. While the guide uses React `v18`, the same architecture would apply to an app that uses React `v17`.
