fix: use hook with first render (#2481)

This commit is contained in:
Himself65
2023-05-22 15:58:13 +08:00
committed by GitHub
parent 5fbfabb3b2
commit 2eaaeef4a7

View File

@@ -4,18 +4,16 @@ import { memo, useRef } from 'react';
const themes = ['dark', 'light'];
// a workaround to sync theme to electron
let firstRender = true;
const DesktopThemeSync = memo(function DesktopThemeSync() {
const { theme } = useTheme();
const lastThemeRef = useRef(theme);
if (lastThemeRef.current !== theme || firstRender) {
const onceRef = useRef(false);
if (lastThemeRef.current !== theme || !onceRef.current) {
if (environment.isDesktop && theme) {
window.apis?.ui.handleThemeChange(theme as 'dark' | 'light' | 'system');
}
lastThemeRef.current = theme;
firstRender = false;
onceRef.current = true;
}
return null;
});