Skip to content

Commit 9c8f40f

Browse files
committed
fix
1 parent 6fe3959 commit 9c8f40f

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

src/index.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { composeRef, supportRef } from 'rc-util/lib/ref';
33
import findDOMNode from 'rc-util/lib/Dom/findDOMNode';
44
import canUseDom from 'rc-util/lib/Dom/canUseDom';
55
import DomWrapper from './wapper';
6+
import useEvent from 'rc-util/es/hooks/useEvent';
67
import type { MutationObserverProps } from './interface';
78

89
const defOptions: MutationObserverInit = {
@@ -14,6 +15,8 @@ const defOptions: MutationObserverInit = {
1415
const MutateObserver: React.FC<MutationObserverProps> = props => {
1516
const { children, options = defOptions, onMutate = () => {} } = props;
1617

18+
const callback = useEvent(onMutate);
19+
1720
const wrapperRef = useRef<DomWrapper>(null);
1821

1922
const elementRef = React.useRef<HTMLElement>(null);
@@ -34,19 +37,20 @@ const MutateObserver: React.FC<MutationObserverProps> = props => {
3437

3538
let instance: MutationObserver;
3639

37-
const currentElement = findDOMNode(
38-
(originRef as any)?.current || wrapperRef?.current,
39-
);
40+
const currentElement =
41+
findDOMNode((originRef as any)?.current) ||
42+
findDOMNode(wrapperRef?.current);
4043

4144
if (currentElement && 'MutationObserver' in window) {
42-
instance = new MutationObserver(onMutate);
45+
instance = new MutationObserver(callback);
4346
instance.observe(currentElement, options);
4447
}
4548
return () => {
4649
instance?.takeRecords();
4750
instance?.disconnect();
4851
};
49-
}, [options, originRef, onMutate]);
52+
// eslint-disable-next-line react-hooks/exhaustive-deps
53+
}, [options, originRef]);
5054

5155
if (!children) {
5256
if (process.env.NODE_ENV !== 'production') {

0 commit comments

Comments
 (0)