From 4a2d36e2fab9babb4a7fcada751756ecafc678f3 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 21 Sep 2026 22:37:27 -0400 Subject: [PATCH] chore: Remove useIsMounted guards that only avoid the unmounted setState warning (#13843) React 18+ no longer warns on setState after unmount, so the guards in useRequest, useIdle, ActionButton, useDocumentSave and MultiplayerEditor are removed. The Mentions guards protect an editor transaction against a destroyed node view, so they are replaced with an effect-scoped cancel flag. Both useIsMounted hook files are deleted. Closes #13839 --- app/components/ActionButton.tsx | 4 +- app/hooks/useIdle.ts | 16 ++---- app/hooks/useIsMounted.ts | 3 - app/hooks/useRequest.ts | 21 ++----- .../Document/components/MultiplayerEditor.tsx | 7 +-- app/scenes/Document/hooks/useDocumentSave.ts | 14 ++--- shared/editor/components/Mentions.tsx | 57 +++++++++++++------ shared/hooks/useIsMounted.ts | 19 ------- 8 files changed, 58 insertions(+), 83 deletions(-) delete mode 100644 app/hooks/useIsMounted.ts delete mode 100644 shared/hooks/useIsMounted.ts diff --git a/app/components/ActionButton.tsx b/app/components/ActionButton.tsx index f1650bbc1d..37884e206a 100644 --- a/app/components/ActionButton.tsx +++ b/app/components/ActionButton.tsx @@ -4,7 +4,6 @@ import * as React from "react"; import type { Props as TooltipProps } from "~/components/Tooltip"; import Tooltip from "~/components/Tooltip"; import { performAction, resolve } from "~/actions"; -import useIsMounted from "~/hooks/useIsMounted"; import useActionContext from "~/hooks/useActionContext"; import type { ActionVariant, ActionWithChildren } from "~/types"; @@ -32,7 +31,6 @@ function ActionButton({ const actionContext = useActionContext({ isButton: true, }); - const isMounted = useIsMounted(); const [executing, setExecuting] = React.useState(false); if (!actionContext || !action) { @@ -68,7 +66,7 @@ function ActionButton({ const response = performAction(action, actionContext); if (response?.finally) { setExecuting(true); - void response.finally(() => isMounted() && setExecuting(false)); + void response.finally(() => setExecuting(false)); } } : rest.onClick diff --git a/app/hooks/useIdle.ts b/app/hooks/useIdle.ts index 05c330db34..512f3bc65d 100644 --- a/app/hooks/useIdle.ts +++ b/app/hooks/useIdle.ts @@ -1,7 +1,6 @@ import { throttle } from "es-toolkit/compat"; import { useState, useRef, useCallback, useEffect } from "react"; import { Minute } from "@shared/utils/time"; -import useIsMounted from "./useIsMounted"; const activityEvents = [ "click", @@ -26,7 +25,6 @@ export default function useIdle( timeToIdle: number = 3 * Minute.ms, events = activityEvents ) { - const isMounted = useIsMounted(); const [isIdle, setIsIdle] = useState(false); const timeout = useRef | undefined>(undefined); @@ -36,18 +34,14 @@ export default function useIdle( } timeout.current = setTimeout(() => { - if (isMounted()) { - setIsIdle(true); - } + setIsIdle(true); }, timeToIdle); - }, [isMounted, timeToIdle]); + }, [timeToIdle]); useEffect(() => { const handleUserActivityEvent = throttle(() => { - if (isMounted()) { - setIsIdle(false); - onActivity(); - } + setIsIdle(false); + onActivity(); }, 1000); events.forEach((eventName) => @@ -67,7 +61,7 @@ export default function useIdle( window.removeEventListener(eventName, handleUserActivityEvent) ); }; - }, [events, isMounted, onActivity]); + }, [events, onActivity]); return isIdle; } diff --git a/app/hooks/useIsMounted.ts b/app/hooks/useIsMounted.ts deleted file mode 100644 index 3770b66a58..0000000000 --- a/app/hooks/useIsMounted.ts +++ /dev/null @@ -1,3 +0,0 @@ -import useIsMounted from "@shared/hooks/useIsMounted"; - -export default useIsMounted; diff --git a/app/hooks/useRequest.ts b/app/hooks/useRequest.ts index fa9fbd768a..8267aacc12 100644 --- a/app/hooks/useRequest.ts +++ b/app/hooks/useRequest.ts @@ -1,5 +1,4 @@ import { useState, useCallback, useEffect } from "react"; -import useIsMounted from "./useIsMounted"; type RequestResponse = { /** The return value of the request function. */ @@ -25,7 +24,6 @@ export default function useRequest( requestFn: () => Promise, makeRequestOnMount = false ): RequestResponse { - const isMounted = useIsMounted(); const [data, setData] = useState(); const [loading, setLoading] = useState(false); const [loaded, setLoaded] = useState(false); @@ -35,25 +33,18 @@ export default function useRequest( setLoading(true); try { const response = await requestFn(); - - if (isMounted()) { - setData(response); - setError(undefined); - setLoaded(true); - } + setData(response); + setError(undefined); + setLoaded(true); return response; } catch (err) { - if (isMounted()) { - setError(err); - } + setError(err); } finally { - if (isMounted()) { - setLoading(false); - } + setLoading(false); } return undefined; - }, [requestFn, isMounted]); + }, [requestFn]); useEffect(() => { if (makeRequestOnMount) { diff --git a/app/scenes/Document/components/MultiplayerEditor.tsx b/app/scenes/Document/components/MultiplayerEditor.tsx index 8f82ab5ad5..ef315dbb15 100644 --- a/app/scenes/Document/components/MultiplayerEditor.tsx +++ b/app/scenes/Document/components/MultiplayerEditor.tsx @@ -26,7 +26,6 @@ import MultiplayerExtension from "~/editor/extensions/Multiplayer"; import env from "~/env"; import useCurrentUser from "~/hooks/useCurrentUser"; import useIdle from "~/hooks/useIdle"; -import useIsMounted from "~/hooks/useIsMounted"; import usePageVisibility from "~/hooks/usePageVisibility"; import useStores from "~/hooks/useStores"; import type { AwarenessChangeEvent } from "~/types"; @@ -64,7 +63,6 @@ function MultiplayerEditor({ onSynced, ref, ...props }: Props) { const token = auth.collaborationToken; const isIdle = useIdle(); const isVisible = usePageVisibility(); - const isMounted = useIsMounted(); // Provider initialization must be within useLayoutEffect rather than useState // or useMemo as both of these are ran twice in React StrictMode resulting in @@ -157,9 +155,7 @@ function MultiplayerEditor({ onSynced, ref, ...props }: Props) { const showCursorNames = () => { setShowCursorNames(true); setTimeout(() => { - if (isMounted()) { - setShowCursorNames(false); - } + setShowCursorNames(false); }, 2000); provider.off("awarenessChange", showCursorNames); }; @@ -261,7 +257,6 @@ function MultiplayerEditor({ onSynced, ref, ...props }: Props) { presence, ydoc, currentUser.id, - isMounted, auth, ]); diff --git a/app/scenes/Document/hooks/useDocumentSave.ts b/app/scenes/Document/hooks/useDocumentSave.ts index 3cc7a94fe1..d32f2e64c9 100644 --- a/app/scenes/Document/hooks/useDocumentSave.ts +++ b/app/scenes/Document/hooks/useDocumentSave.ts @@ -12,7 +12,6 @@ import type Document from "~/models/Document"; import Template from "~/models/Template"; import type Revision from "~/models/Revision"; import type { Editor as TEditor } from "~/editor"; -import useIsMounted from "~/hooks/useIsMounted"; import { useLocationSidebarContext } from "~/hooks/useLocationSidebarContext"; import useStores from "~/hooks/useStores"; import { documentEditPath } from "~/utils/routeHelpers"; @@ -97,7 +96,6 @@ export function useDocumentSave({ const { auth, ui } = useStores(); const history = useHistory(); const sidebarContext = useLocationSidebarContext(); - const isMounted = useIsMounted(); // State const [isUploading, setIsUploading] = useState(false); @@ -169,9 +167,7 @@ export function useDocumentSave({ try { const savedDocument = await document.save(undefined, options); isEditorDirtyRef.current = false; - if (isMounted()) { - setIsEditorDirty(false); - } + setIsEditorDirty(false); if (options.done) { history.push({ @@ -189,13 +185,11 @@ export function useDocumentSave({ } catch (err) { toast.error(errToString(err)); } finally { - if (isMounted()) { - setIsSaving(false); - setIsPublishing(false); - } + setIsSaving(false); + setIsPublishing(false); } }, - [document, editorRef, history, sidebarContext, ui, isMounted] + [document, editorRef, history, sidebarContext, ui] ); const onSaveRef = useRef(onSave); diff --git a/shared/editor/components/Mentions.tsx b/shared/editor/components/Mentions.tsx index 22949e6963..0c86abebbc 100644 --- a/shared/editor/components/Mentions.tsx +++ b/shared/editor/components/Mentions.tsx @@ -22,7 +22,6 @@ import { IssueStatusIcon } from "../../components/IssueStatusIcon"; import { PullRequestIcon } from "../../components/PullRequestIcon"; import Spinner from "../../components/Spinner"; import Text from "../../components/Text"; -import useIsMounted from "../../hooks/useIsMounted"; import useStores from "../../hooks/useStores"; import theme from "../../styles/theme"; import { @@ -192,7 +191,6 @@ type IssueUrlProps = ComponentProps & { export const MentionURL = (props: IssueUrlProps) => { const { unfurls } = useStores(); - const isMounted = useIsMounted(); const [loaded, setLoaded] = React.useState(false); const onChangeUnfurl = React.useRef(props.onChangeUnfurl).current; // stable reference to callback function. @@ -212,11 +210,15 @@ export const MentionURL = (props: IssueUrlProps) => { return; } + // The node view may be destroyed before the fetch resolves, in which case + // the editor transaction in onChangeUnfurl must not be dispatched. + let cancelled = false; + const fetchUnfurl = async () => { try { const unfurlModel = await unfurls.fetchUnfurl({ url }); - if (!isMounted()) { + if (cancelled) { return; } @@ -245,14 +247,16 @@ export const MentionURL = (props: IssueUrlProps) => { data, }); } finally { - if (isMounted()) { - setLoaded(true); - } + setLoaded(true); } }; void fetchUnfurl(); - }, [unfurls, url, node, isMounted, onChangeUnfurl]); + + return () => { + cancelled = true; + }; + }, [unfurls, url, node, onChangeUnfurl]); if (!unfurl) { return !loaded ? ( @@ -286,7 +290,6 @@ export const MentionURL = (props: IssueUrlProps) => { export const MentionIssue = observer((props: IssuePrProps) => { const { unfurls } = useStores(); - const isMounted = useIsMounted(); const [loaded, setLoaded] = React.useState(false); const onChangeUnfurl = React.useRef(props.onChangeUnfurl).current; // stable reference to callback function. @@ -300,10 +303,14 @@ export const MentionIssue = observer((props: IssuePrProps) => { const unfurl = unfurls.get(attrs.href)?.data ?? unfurlAttr; React.useEffect(() => { + // The node view may be destroyed before the fetch resolves, in which case + // the editor transaction in onChangeUnfurl must not be dispatched. + let cancelled = false; + const fetchIssue = async () => { const unfurlModel = await unfurls.fetchUnfurl({ url: attrs.href }); - if (!isMounted()) { + if (cancelled) { return; } @@ -318,7 +325,11 @@ export const MentionIssue = observer((props: IssuePrProps) => { }; void fetchIssue(); - }, [unfurls, attrs.href, isMounted, onChangeUnfurl]); + + return () => { + cancelled = true; + }; + }, [unfurls, attrs.href, onChangeUnfurl]); if (!unfurl) { return !loaded ? ( @@ -372,7 +383,6 @@ type ProjectProps = ComponentProps & { export const MentionProject = observer((props: ProjectProps) => { const { unfurls } = useStores(); - const isMounted = useIsMounted(); const [loaded, setLoaded] = React.useState(false); const onChangeUnfurl = React.useRef(props.onChangeUnfurl).current; @@ -386,10 +396,14 @@ export const MentionProject = observer((props: ProjectProps) => { const unfurl = unfurls.get(attrs.href)?.data ?? unfurlAttr; React.useEffect(() => { + // The node view may be destroyed before the fetch resolves, in which case + // the editor transaction in onChangeUnfurl must not be dispatched. + let cancelled = false; + const fetchProject = async () => { const unfurlModel = await unfurls.fetchUnfurl({ url: attrs.href }); - if (!isMounted()) { + if (cancelled) { return; } @@ -404,7 +418,11 @@ export const MentionProject = observer((props: ProjectProps) => { }; void fetchProject(); - }, [unfurls, attrs.href, isMounted, onChangeUnfurl]); + + return () => { + cancelled = true; + }; + }, [unfurls, attrs.href, onChangeUnfurl]); if (!unfurl) { return !loaded ? ( @@ -449,7 +467,6 @@ export const MentionProject = observer((props: ProjectProps) => { export const MentionPullRequest = observer((props: IssuePrProps) => { const { unfurls } = useStores(); - const isMounted = useIsMounted(); const [loaded, setLoaded] = React.useState(false); const onChangeUnfurl = React.useRef(props.onChangeUnfurl).current; // stable reference to callback function. @@ -463,10 +480,14 @@ export const MentionPullRequest = observer((props: IssuePrProps) => { const unfurl = unfurls.get(attrs.href)?.data ?? unfurlAttr; React.useEffect(() => { + // The node view may be destroyed before the fetch resolves, in which case + // the editor transaction in onChangeUnfurl must not be dispatched. + let cancelled = false; + const fetchPR = async () => { const unfurlModel = await unfurls.fetchUnfurl({ url: attrs.href }); - if (!isMounted()) { + if (cancelled) { return; } @@ -481,7 +502,11 @@ export const MentionPullRequest = observer((props: IssuePrProps) => { }; void fetchPR(); - }, [unfurls, attrs.href, isMounted, onChangeUnfurl]); + + return () => { + cancelled = true; + }; + }, [unfurls, attrs.href, onChangeUnfurl]); const sharedProps = { className: cn(className, { diff --git a/shared/hooks/useIsMounted.ts b/shared/hooks/useIsMounted.ts deleted file mode 100644 index 196b7aa97b..0000000000 --- a/shared/hooks/useIsMounted.ts +++ /dev/null @@ -1,19 +0,0 @@ -import * as React from "react"; - -/** - * Hook to check if component is still mounted - * - * @returns true if the component is mounted, false otherwise - */ -export default function useIsMounted(): () => boolean { - const isMounted = React.useRef(false); - - React.useEffect(() => { - isMounted.current = true; - return () => { - isMounted.current = false; - }; - }, []); - - return React.useCallback(() => isMounted.current, []); -}