|
23 | 23 | - [Session Expiry from Upstream IdP (IPSIE)](#session-expiry-from-upstream-idp-ipsie) |
24 | 24 | - [Use Suspense for loading state (React 19+)](#use-suspense-for-loading-state-react-19) |
25 | 25 |
|
26 | | -## Use Suspense for loading state (React 19+) |
27 | | - |
28 | | -On React 19 and later, `useAuth0Suspense` lets a `<Suspense>` boundary handle the |
29 | | -auth loading state and an Error Boundary handle initialization errors, so your |
30 | | -component code stays focused on rendering. Unlike `useAuth0`, it does not return |
31 | | -`isLoading` (the component suspends until auth is ready) or `error` (it is thrown |
32 | | -to the nearest Error Boundary). |
33 | | - |
34 | | -```jsx |
35 | | -import { Suspense } from 'react'; |
36 | | -import { Auth0Provider, useAuth0Suspense } from '@auth0/auth0-react'; |
37 | | - |
38 | | -function App() { |
39 | | - return ( |
40 | | - <Auth0Provider |
41 | | - domain="YOUR_DOMAIN" |
42 | | - clientId="YOUR_CLIENT_ID" |
43 | | - authorizationParams={{ redirect_uri: window.location.origin }} |
44 | | - > |
45 | | - <MyErrorBoundary fallback={<p>Could not sign you in.</p>}> |
46 | | - <Suspense fallback={<p>Loading...</p>}> |
47 | | - <UserGreeting /> |
48 | | - </Suspense> |
49 | | - </MyErrorBoundary> |
50 | | - </Auth0Provider> |
51 | | - ); |
52 | | -} |
53 | | - |
54 | | -function UserGreeting() { |
55 | | - const { user, isAuthenticated } = useAuth0Suspense(); |
56 | | - return isAuthenticated ? <p>Hello, {user?.name}!</p> : <p>Please log in</p>; |
57 | | -} |
58 | | -``` |
59 | | -
|
60 | | -`useAuth0Suspense` requires React 19; calling it on an earlier version throws a |
61 | | -clear error. All the auth methods available on `useAuth0` (`loginWithRedirect`, |
62 | | -`logout`, `getAccessTokenSilently`, etc.) are also available here. |
63 | 26 |
|
64 | 27 | ## Use with a Class Component |
65 | 28 |
|
@@ -1910,3 +1873,41 @@ function CallApi() { |
1910 | 1873 | ``` |
1911 | 1874 |
|
1912 | 1875 | Using `withAuthenticationRequired` on protected routes is the simpler alternative — the redirect happens automatically without the null check. |
| 1876 | +
|
| 1877 | +
|
| 1878 | +## Use Suspense for loading state (React 19+) |
| 1879 | +
|
| 1880 | +On React 19 and later, `useAuth0Suspense` lets a `<Suspense>` boundary handle the |
| 1881 | +auth loading state and an Error Boundary handle initialization errors, so your |
| 1882 | +component code stays focused on rendering. Unlike `useAuth0`, it does not return |
| 1883 | +`isLoading` — the component suspends until auth is ready. |
| 1884 | +
|
| 1885 | +```jsx |
| 1886 | +import { Suspense } from 'react'; |
| 1887 | +import { Auth0Provider, useAuth0Suspense } from '@auth0/auth0-react'; |
| 1888 | + |
| 1889 | +function App() { |
| 1890 | + return ( |
| 1891 | + <Auth0Provider |
| 1892 | + domain="YOUR_DOMAIN" |
| 1893 | + clientId="YOUR_CLIENT_ID" |
| 1894 | + authorizationParams={{ redirect_uri: window.location.origin }} |
| 1895 | + > |
| 1896 | + <MyErrorBoundary fallback={<p>Could not sign you in.</p>}> |
| 1897 | + <Suspense fallback={<p>Loading...</p>}> |
| 1898 | + <UserGreeting /> |
| 1899 | + </Suspense> |
| 1900 | + </MyErrorBoundary> |
| 1901 | + </Auth0Provider> |
| 1902 | + ); |
| 1903 | +} |
| 1904 | + |
| 1905 | +function UserGreeting() { |
| 1906 | + const { user, isAuthenticated } = useAuth0Suspense(); |
| 1907 | + return isAuthenticated ? <p>Hello, {user?.name}!</p> : <p>Please log in</p>; |
| 1908 | +} |
| 1909 | +``` |
| 1910 | +
|
| 1911 | +`useAuth0Suspense` requires React 19; calling it on an earlier version throws a |
| 1912 | +clear error. All the auth methods available on `useAuth0` (`loginWithRedirect`, |
| 1913 | +`logout`, `getAccessTokenSilently`, etc.) are also available here. |
0 commit comments