Fix dynamic tabs infinite loop (#2677)

This commit is contained in:
Greg Bergé
2025-01-07 14:54:28 +01:00
committed by GitHub
parent deb8c54da3
commit cbe61397a3
2 changed files with 8 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'gitbook': patch
---
Fix dynamic tabs infinite loop
@@ -20,6 +20,7 @@ let globalTabsState: TabsState = (() => {
const stored = localStorage.getItem('@gitbook/tabsState');
return stored ? (JSON.parse(stored) as TabsState) : { activeIds: {}, activeTitles: [] };
})();
const listeners = new Set<() => void>();
function useTabsState() {
@@ -30,13 +31,13 @@ function useTabsState() {
const getSnapshot = useCallback(() => globalTabsState, []);
const setTabsState = (updater: (previous: TabsState) => TabsState) => {
const setTabsState = useCallback((updater: (previous: TabsState) => TabsState) => {
globalTabsState = updater(globalTabsState);
if (typeof localStorage !== 'undefined') {
localStorage.setItem('@gitbook/tabsState', JSON.stringify(globalTabsState));
}
listeners.forEach((listener) => listener());
};
}, []);
const state = React.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
return [state, setTabsState] as const;
}