diff --git a/src/ui/markdown.tsx b/src/ui/markdown.tsx index 3d144ca1..2574f218 100644 --- a/src/ui/markdown.tsx +++ b/src/ui/markdown.tsx @@ -243,24 +243,13 @@ export function useIndexBackedState( initial: T, compute: () => Promise ): T { - let [initialized, setInitialized] = useState(false); let [state, updateState] = useState(initial); - let [lastReload, setLastReload] = useState(index.revision); - - // Initial setup to queue fetching the correct state. - if (!initialized) { - setLastReload(index.revision); - setInitialized(true); - - compute().then(updateState); - } // Updated on every container re-create; automatically updates state. useEffect(() => { const refreshOperation = () => { - if (lastReload != index.revision && container.isShown() && settings.refreshEnabled) { + if (container.isShown() && settings.refreshEnabled) { compute().then(updateState); - setLastReload(index.revision); } }; @@ -269,11 +258,14 @@ export function useIndexBackedState( // ...or when the DOM is shown (sidebar expands, tab selected, nodes scrolled into view). let nodeEvent = container.onNodeInserted(refreshOperation); + // Handle the initial refresh + compute().then(updateState); + return () => { app.workspace.offref(workEvent); nodeEvent(); }; - }, [container, lastReload]); + }, [container]); return state; }