Skip to content

Commit

Permalink
Fix dynamic tabs infinite loop (#2677)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge authored Jan 7, 2025
1 parent deb8c54 commit cbe6139
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-oranges-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'gitbook': patch
---

Fix dynamic tabs infinite loop
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
}
Expand Down

0 comments on commit cbe6139

Please sign in to comment.