fix: fix infinity re-redner

This commit is contained in:
Yuren Ju
2023-10-13 12:37:28 +08:00
parent 971a9f148f
commit f3c6152d2c
3 changed files with 13 additions and 15 deletions

View File

@@ -4,9 +4,10 @@ import {
useCredentials,
} from '@tw-did/react-library';
import { useNavigate } from '@tanstack/react-router';
import { useCallback } from 'react';
export function CredentialView() {
const { logout } = useAuth();
const { logout, user } = useAuth();
const { credentials } = useCredentials();
const handleDownload = async (data: any) => {
@@ -34,9 +35,9 @@ export function CredentialView() {
};
/* TODO: get from AuthContext */
const checkLogin = () => {
return localStorage.getItem('user') ? true : false;
};
const checkLogin = useCallback(() => {
return !!user;
}, [user]);
return (
<div>
@@ -44,7 +45,7 @@ export function CredentialView() {
credentials={credentials}
actionLabels={['download']}
onAction={(index, label) => {
if (label == 'download')
if (label === 'download')
handleDownload(credentials[index].verifiableCredential);
else return;
}}

View File

@@ -11,10 +11,13 @@ export interface DialogProps {
export const useDialog = () => {
const [props, setProps] = useState<DialogProps | undefined>();
const open = useCallback((openProps: DialogProps) => {
openProps && setProps(openProps);
}, []);
const close = useCallback(() => setProps(undefined), []);
const open = useCallback(
(openProps: DialogProps) => {
openProps && setProps(openProps);
},
[setProps]
);
const close = useCallback(() => setProps(undefined), [setProps]);
const dialogController = useMemo(
() => ({ open, close, props }),

View File

@@ -143,12 +143,6 @@ export const CredentialScreen = ({
dialogController.open(errorDialog);
}
useEffect(() => {
if (!checkLogin()) {
dialogController.open(goHomeDialog);
}
}, [dialogController, checkLogin]);
return (
<Container>
<div className={styles.CredentialScreen}>