diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx index 0b0cea3a0..6b9d65c01 100644 --- a/apps/client/src/widgets/react/hooks.tsx +++ b/apps/client/src/widgets/react/hooks.tsx @@ -1,4 +1,4 @@ -import { useCallback, useContext, useDebugValue, useEffect, useMemo, useRef, useState } from "preact/hooks"; +import { useCallback, useContext, useDebugValue, useEffect, useLayoutEffect, useMemo, useRef, useState } from "preact/hooks"; import { EventData, EventNames } from "../../components/app_context"; import { ParentComponent } from "./react_utils"; import SpacedUpdate from "../../services/spaced_update"; @@ -17,7 +17,7 @@ import { CSSProperties } from "preact/compat"; export function useTriliumEvent(eventName: T, handler: (data: EventData) => void) { const parentComponent = useContext(ParentComponent); - useEffect(() => { + useLayoutEffect(() => { parentComponent?.registerHandler(eventName, handler); return (() => parentComponent?.removeHandler(eventName, handler)); }, [ eventName, handler ]); @@ -27,7 +27,7 @@ export function useTriliumEvent(eventName: T, handler: (da export function useTriliumEvents(eventNames: T[], handler: (data: EventData, eventName: T) => void) { const parentComponent = useContext(ParentComponent); - useEffect(() => { + useLayoutEffect(() => { const handlers: ({ eventName: T, callback: (data: EventData) => void })[] = []; for (const eventName of eventNames) { handlers.push({ eventName, callback: (data) => { @@ -193,7 +193,6 @@ export function useUniqueName(prefix?: string) { } export function useNoteContext() { - const [ noteContext, setNoteContext ] = useState(); const [ notePath, setNotePath ] = useState(); const [ note, setNote ] = useState(); @@ -211,12 +210,7 @@ export function useNoteContext() { }); const parentComponent = useContext(ParentComponent) as ReactWrappedWidget; - (parentComponent as ReactWrappedWidget & { setNoteContextEvent: (data: EventData<"setNoteContext">) => void }).setNoteContextEvent = ({ noteContext }: EventData<"setNoteContext">) => { - setNoteContext(noteContext); - } - useDebugValue(() => `notePath=${notePath}, ntxId=${noteContext?.ntxId}`); - return { note: note,