1
0
Fork 0

fix(org): Avoid recreating noAccount object on each render

This commit is contained in:
joostdecock 2023-03-12 18:49:09 +01:00
parent 0dbf76c9bf
commit 731c246e88
2 changed files with 11 additions and 2 deletions

View file

@ -95,6 +95,9 @@ const toastMethods = (t) => ({
},
})
// Make it possible to always check for app.account.username
const noAccount = { username: false }
/*
* The actual hook
*/
@ -104,7 +107,7 @@ export function useApp({ bugsnag }) {
const { t } = useTranslation(['toast'])
// Persistent state
const [account, setAccount, accountReady] = useLocalStorage('account', { username: false })
const [account, setAccount, accountReady] = useLocalStorage('account', noAccount)
const [token, setToken] = useLocalStorage('token', null)
const [theme, setTheme] = useTheme()
@ -155,6 +158,11 @@ export function useApp({ bugsnag }) {
return id
}
// Clear user data (when loggin in as a different user, this gets called)
const clear = () => {
setAccount({ username: false })
}
return {
// Static vars
site: 'org',
@ -188,6 +196,7 @@ export function useApp({ bugsnag }) {
stopLoading: () => setLoading(false),
updateNavigation,
setModal,
clear,
// State handlers
togglePrimaryMenu,

View file

@ -32,7 +32,7 @@ export function useLocalStorage(key, initialValue) {
} catch (error) {
console.log(error)
}
}, []) // The linter will hate this, but this was cleared to stop a render loop
}, [setReady, setValue, key, initialValue])
return [storedValue, setValue, ready]
}