Skip to content

Commit f64d4f9

Browse files
authored
fix: update access token management with useEffect
Recommended setup was violating React rule of never accessing ref during render
1 parent 8fe2c47 commit f64d4f9

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

npm-packages/docs/docs/auth/authkit/index.mdx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ Next.js app with Convex. If not follow the
438438
```tsx title="components/ConvexClientProvider.tsx"
439439
'use client';
440440

441-
import { ReactNode, useCallback, useRef } from 'react';
441+
import { ReactNode, useCallback, useEffect, useRef } from 'react';
442442
import { ConvexReactClient } from 'convex/react';
443443
import { ConvexProviderWithAuth } from 'convex/react';
444444
import { AuthKitProvider, useAuth, useAccessToken } from '@workos-inc/authkit-nextjs/components';
@@ -462,9 +462,11 @@ Next.js app with Convex. If not follow the
462462
const authenticated = !!user && !!accessToken && !loading;
463463

464464
const stableAccessToken = useRef<string | null>(null);
465-
if (accessToken && !tokenError) {
466-
stableAccessToken.current = accessToken;
467-
}
465+
useEffect(() => {
466+
if (accessToken && !tokenError) {
467+
stableAccessToken.current = accessToken;
468+
}
469+
}, [accessToken, tokenError]);
468470

469471
const fetchAccessToken = useCallback(async () => {
470472
if (stableAccessToken.current && !tokenError) {

0 commit comments

Comments
 (0)