refactor: rm useDestroy hook (#34418)

This commit is contained in:
二货机器人 2022-03-10 18:54:32 +08:00 committed by GitHub
parent 78d2b3f063
commit b51c900847
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 10 additions and 55 deletions

View File

@ -1,7 +1,7 @@
import * as React from 'react'; import * as React from 'react';
import useState from 'rc-util/lib/hooks/useState';
import Button from '../button'; import Button from '../button';
import { LegacyButtonType, ButtonProps, convertLegacyProps } from '../button/button'; import { LegacyButtonType, ButtonProps, convertLegacyProps } from '../button/button';
import useDestroyed from './hooks/useDestroyed';
export interface ActionButtonProps { export interface ActionButtonProps {
type?: LegacyButtonType; type?: LegacyButtonType;
@ -21,8 +21,7 @@ function isThenable(thing?: PromiseLike<any>): boolean {
const ActionButton: React.FC<ActionButtonProps> = props => { const ActionButton: React.FC<ActionButtonProps> = props => {
const clickedRef = React.useRef<boolean>(false); const clickedRef = React.useRef<boolean>(false);
const ref = React.useRef<any>(); const ref = React.useRef<any>();
const isDestroyed = useDestroyed(); const [loading, setLoading] = useState<ButtonProps['loading']>(false);
const [loading, setLoading] = React.useState<ButtonProps['loading']>(false);
React.useEffect(() => { React.useEffect(() => {
let timeoutId: any; let timeoutId: any;
@ -45,9 +44,7 @@ const ActionButton: React.FC<ActionButtonProps> = props => {
setLoading(true); setLoading(true);
returnValueOfOnOk!.then( returnValueOfOnOk!.then(
(...args: any[]) => { (...args: any[]) => {
if (!isDestroyed()) { setLoading(false, true);
setLoading(false);
}
close(...args); close(...args);
clickedRef.current = false; clickedRef.current = false;
}, },
@ -56,9 +53,7 @@ const ActionButton: React.FC<ActionButtonProps> = props => {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.error(e); console.error(e);
// See: https://github.com/ant-design/ant-design/issues/6183 // See: https://github.com/ant-design/ant-design/issues/6183
if (!isDestroyed()) { setLoading(false, true);
setLoading(false);
}
clickedRef.current = false; clickedRef.current = false;
}, },
); );

View File

@ -1,20 +0,0 @@
import { mount } from 'enzyme';
import React from 'react';
import useDestroyed from '../hooks/useDestroyed';
describe('useMounted', () => {
it('should work properly', () => {
let isDestroyed = null;
const AutoUnmounted = () => {
isDestroyed = useDestroyed();
return <div>Mounted</div>;
};
const wrapper = mount(<AutoUnmounted />);
expect(isDestroyed()).toBeFalsy();
wrapper.unmount();
expect(isDestroyed()).toBeTruthy();
});
});

View File

@ -1,14 +0,0 @@
import * as React from 'react';
export default function useDestroyed() {
const mountedRef = React.useRef<boolean>(true);
React.useEffect(
() => () => {
mountedRef.current = false;
},
[],
);
return () => !mountedRef.current;
}

View File

@ -1,5 +1,6 @@
import * as React from 'react'; import * as React from 'react';
import classNames from 'classnames'; import classNames from 'classnames';
import useState from 'rc-util/lib/hooks/useState';
import ArrowLeftOutlined from '@ant-design/icons/ArrowLeftOutlined'; import ArrowLeftOutlined from '@ant-design/icons/ArrowLeftOutlined';
import ArrowRightOutlined from '@ant-design/icons/ArrowRightOutlined'; import ArrowRightOutlined from '@ant-design/icons/ArrowRightOutlined';
import ResizeObserver from 'rc-resize-observer'; import ResizeObserver from 'rc-resize-observer';
@ -9,7 +10,6 @@ import Breadcrumb, { BreadcrumbProps } from '../breadcrumb';
import Avatar, { AvatarProps } from '../avatar'; import Avatar, { AvatarProps } from '../avatar';
import TransButton from '../_util/transButton'; import TransButton from '../_util/transButton';
import LocaleReceiver from '../locale-provider/LocaleReceiver'; import LocaleReceiver from '../locale-provider/LocaleReceiver';
import useDestroyed from '../_util/hooks/useDestroyed';
export interface PageHeaderProps { export interface PageHeaderProps {
backIcon?: React.ReactNode; backIcon?: React.ReactNode;
@ -121,12 +121,9 @@ const renderChildren = (prefixCls: string, children: React.ReactNode) => (
); );
const PageHeader: React.FC<PageHeaderProps> = props => { const PageHeader: React.FC<PageHeaderProps> = props => {
const [compact, updateCompact] = React.useState(false); const [compact, updateCompact] = useState(false);
const isDestroyed = useDestroyed();
const onResize = ({ width }: { width: number }) => { const onResize = ({ width }: { width: number }) => {
if (!isDestroyed()) { updateCompact(width < 768, true);
updateCompact(width < 768);
}
}; };
return ( return (
<ConfigConsumer> <ConfigConsumer>

View File

@ -13,7 +13,6 @@ import { getRenderPropValue, RenderFunction } from '../_util/getRenderPropValue'
import { cloneElement } from '../_util/reactNode'; import { cloneElement } from '../_util/reactNode';
import { getTransitionName } from '../_util/motion'; import { getTransitionName } from '../_util/motion';
import ActionButton from '../_util/ActionButton'; import ActionButton from '../_util/ActionButton';
import useDestroyed from '../_util/hooks/useDestroyed';
export interface PopconfirmProps extends AbstractTooltipProps { export interface PopconfirmProps extends AbstractTooltipProps {
title: React.ReactNode | RenderFunction; title: React.ReactNode | RenderFunction;
@ -49,15 +48,13 @@ const Popconfirm = React.forwardRef<unknown, PopconfirmProps>((props, ref) => {
defaultValue: props.defaultVisible, defaultValue: props.defaultVisible,
}); });
const isDestroyed = useDestroyed(); // const isDestroyed = useDestroyed();
const settingVisible = ( const settingVisible = (
value: boolean, value: boolean,
e?: React.MouseEvent<HTMLButtonElement> | React.KeyboardEvent<HTMLDivElement>, e?: React.MouseEvent<HTMLButtonElement> | React.KeyboardEvent<HTMLDivElement>,
) => { ) => {
if (!isDestroyed()) { setVisible(value, true);
setVisible(value);
}
props.onVisibleChange?.(value, e); props.onVisibleChange?.(value, e);
}; };

View File

@ -152,7 +152,7 @@
"rc-tree-select": "~5.1.1", "rc-tree-select": "~5.1.1",
"rc-trigger": "^5.2.10", "rc-trigger": "^5.2.10",
"rc-upload": "~4.3.0", "rc-upload": "~4.3.0",
"rc-util": "^5.19.2", "rc-util": "^5.19.3",
"scroll-into-view-if-needed": "^2.2.25" "scroll-into-view-if-needed": "^2.2.25"
}, },
"devDependencies": { "devDependencies": {