Skip to content

Commit a3232c7

Browse files
authored
fix(clerk-react): Initialize isomorphic clerk with useRef (#6024)
1 parent 94be195 commit a3232c7

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

.changeset/shaky-ants-raise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-react': patch
3+
---
4+
5+
Initialize isomorphic clerk with `useRef`. Avoid memoizing the singleton, instead use a reference to store it, and then destroy it.

packages/react/src/contexts/ClerkContextProvider.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,27 +99,26 @@ export function ClerkContextProvider(props: ClerkContextProvider) {
9999
}
100100

101101
const useLoadedIsomorphicClerk = (options: IsomorphicClerkOptions) => {
102-
const isomorphicClerk = React.useMemo(() => IsomorphicClerk.getOrCreateInstance(options), []);
103-
const [clerkStatus, setStatus] = React.useState(isomorphicClerk.status);
102+
const isomorphicClerkRef = React.useRef(IsomorphicClerk.getOrCreateInstance(options));
103+
const [clerkStatus, setClerkStatus] = React.useState(isomorphicClerkRef.current.status);
104104

105105
React.useEffect(() => {
106-
void isomorphicClerk.__unstable__updateProps({ appearance: options.appearance });
106+
void isomorphicClerkRef.current.__unstable__updateProps({ appearance: options.appearance });
107107
}, [options.appearance]);
108108

109109
React.useEffect(() => {
110-
void isomorphicClerk.__unstable__updateProps({ options });
110+
void isomorphicClerkRef.current.__unstable__updateProps({ options });
111111
}, [options.localization]);
112112

113113
React.useEffect(() => {
114-
isomorphicClerk.on('status', setStatus);
115-
return () => isomorphicClerk.off('status', setStatus);
116-
}, [isomorphicClerk]);
117-
118-
React.useEffect(() => {
114+
isomorphicClerkRef.current.on('status', setClerkStatus);
119115
return () => {
116+
if (isomorphicClerkRef.current) {
117+
isomorphicClerkRef.current.off('status', setClerkStatus);
118+
}
120119
IsomorphicClerk.clearInstance();
121120
};
122121
}, []);
123122

124-
return { isomorphicClerk, clerkStatus };
123+
return { isomorphicClerk: isomorphicClerkRef.current, clerkStatus };
125124
};

0 commit comments

Comments
 (0)