Skip to content

Commit 803e116

Browse files
authored
chore(react): use updater callback function (#6424)
1 parent 7b3665d commit 803e116

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

packages/auth/src/AuthProvider.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,14 @@ export class AuthProvider extends React.Component<
264264
? null
265265
: await this.getCurrentUser()
266266

267-
this.setState({
268-
...this.state,
269-
userMetadata,
270-
currentUser,
271-
isAuthenticated: true,
272-
loading: false,
267+
this.setState((prevState) => {
268+
return {
269+
...prevState,
270+
userMetadata,
271+
currentUser,
272+
isAuthenticated: true,
273+
loading: false,
274+
}
273275
})
274276
}
275277
} catch (e: any) {

packages/router/src/__tests__/router.test.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,18 @@ const mockUseAuth =
8787
useState(isAuthenticated)
8888

8989
useEffect(() => {
90+
let timer: NodeJS.Timeout | undefined
9091
if (loadingTimeMs) {
91-
setTimeout(() => {
92+
timer = setTimeout(() => {
9293
setAuthLoading(false)
9394
setAuthIsAuthenticated(true)
9495
}, loadingTimeMs)
9596
}
97+
return () => {
98+
if (timer) {
99+
clearTimeout(timer)
100+
}
101+
}
96102
}, [])
97103

98104
return createDummyAuthContextValues({
@@ -763,7 +769,8 @@ test('can display a loading screen with a hook', async () => {
763769
const [showStill, setShowStill] = useState(false)
764770

765771
useEffect(() => {
766-
setTimeout(() => setShowStill(true), 100)
772+
const timer = setTimeout(() => setShowStill(true), 100)
773+
return () => clearTimeout(timer)
767774
}, [])
768775

769776
return <>{showStill ? 'Still authenticating...' : 'Authenticating...'}</>

0 commit comments

Comments
 (0)