mirror of
https://github.com/outline/outline.git
synced 2026-11-25 23:24:06 +03:00
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
This commit is contained in:
1 parent
8db64ddb04
commit
4a2d36e2fa
8 files changed
+58
-83
No files matched your search
@@ -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
|
||||
|
||||
+5
-11
@@ -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<ReturnType<typeof setTimeout> | 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;
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
import useIsMounted from "@shared/hooks/useIsMounted";
|
||||
|
||||
export default useIsMounted;
|
||||
+6
-15
@@ -1,5 +1,4 @@
|
||||
import { useState, useCallback, useEffect } from "react";
|
||||
import useIsMounted from "./useIsMounted";
|
||||
|
||||
type RequestResponse<T> = {
|
||||
/** The return value of the request function. */
|
||||
@@ -25,7 +24,6 @@ export default function useRequest<T = unknown>(
|
||||
requestFn: () => Promise<T>,
|
||||
makeRequestOnMount = false
|
||||
): RequestResponse<T> {
|
||||
const isMounted = useIsMounted();
|
||||
const [data, setData] = useState<T>();
|
||||
const [loading, setLoading] = useState<boolean>(false);
|
||||
const [loaded, setLoaded] = useState<boolean>(false);
|
||||
@@ -35,25 +33,18 @@ export default function useRequest<T = unknown>(
|
||||
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) {
|
||||
|
||||
@@ -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,
|
||||
]);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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, {
|
||||
|
||||
@@ -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, []);
|
||||
}
|
||||
Reference in new issue
Block a user