Skip to content

Commit

Permalink
chore(react): use updater callback function
Browse files Browse the repository at this point in the history
callback takes the previous state as the first argument.

> setState() does not always immediately update the component. It may batch or defer the update until later. This makes reading this.state right after calling setState() a potential pitfall. Instead, use componentDidUpdate or a setState callback (setState(updater, callback)), either of which are guaranteed to fire after the update has been applied. If you need to set the state based on the previous state, read about the updater argument below.

see:
- https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-access-state-in-setstate.md
- https://reactjs.org/docs/react-component.html#setstate
  • Loading branch information
virtuoushub committed Sep 21, 2022
1 parent 7b3665d commit f327737
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions packages/auth/src/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,14 @@ export class AuthProvider extends React.Component<
? null
: await this.getCurrentUser()

this.setState({
...this.state,
userMetadata,
currentUser,
isAuthenticated: true,
loading: false,
this.setState((prevState) => {
return {
...prevState,
userMetadata,
currentUser,
isAuthenticated: true,
loading: false,
}
})
}
} catch (e: any) {
Expand Down

0 comments on commit f327737

Please sign in to comment.