Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
fix: hydration errors on conditional rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
daphnesmit committed Mar 7, 2023
1 parent 2fc456f commit ae1a1c2
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/OidcJwtProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { createContext, useContext, useEffect, useRef } from 'react';
import React, { createContext, useContext, useEffect, useRef, useState } from 'react';
import { useIsomorphicLayoutEffect } from 'react-use';
import { StoreApi, UseBoundStore, useStore } from 'zustand';

import { createOidcJwtClientStore } from './store';
Expand Down Expand Up @@ -32,6 +33,7 @@ const OidcJwtInitializer: React.FC<React.PropsWithChildren<OidcJwtProviderProps>
const authService = useOidcJwtStore(state => state.service);
const isLoggedIn = useOidcJwtStore(state => state.authState.isLoggedIn);
const setState = useOidcJwtStore(state => state.setState);
const [isInitializing, setIsInitializing] = useState(false)

useEffect(() => {
authService?.loadInitialData().then(() => setState(authService.state));
Expand All @@ -54,11 +56,15 @@ const OidcJwtInitializer: React.FC<React.PropsWithChildren<OidcJwtProviderProps>
authService.authorize({ prompt: 'none' });
}, [authService, isLoggedIn, shouldAttemptLogin]);

const isInitializing =
!authService?.getCsrfToken().csrfToken &&
shouldAttemptLogin &&
!isLoggedIn &&
typeof window !== 'undefined';
useIsomorphicLayoutEffect(() => {
const isInitializing =
!authService?.getCsrfToken().csrfToken &&
shouldAttemptLogin &&
!isLoggedIn &&
typeof window !== 'undefined';

setIsInitializing(isInitializing);
}, [authService, isLoggedIn, shouldAttemptLogin]);

if (isInitializing) {
return null;
Expand Down

0 comments on commit ae1a1c2

Please sign in to comment.