Skip to content

Commit 78d212b

Browse files
committed
fix
1 parent fb0bac1 commit 78d212b

1 file changed

Lines changed: 8 additions & 13 deletions

File tree

src/index.tsx

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import findDOMNode from 'rc-util/lib/Dom/findDOMNode';
44
import canUseDom from 'rc-util/lib/Dom/canUseDom';
55
import DomWrapper from './wapper';
66

7-
const INTERNAL_PREFIX_KEY = 'rc-mutation-observer-key';
8-
97
interface MutationObserverProps {
108
children: React.ReactElement;
119
options?: MutationObserverInit;
@@ -21,27 +19,24 @@ const defOptions: MutationObserverInit = {
2119
const MutateObserver: React.FC<MutationObserverProps> = props => {
2220
const { children, options = defOptions, onMutate = () => {} } = props;
2321

24-
const instance = useRef<MutationObserver>();
25-
2622
const wrapperRef = useRef<DomWrapper>(null);
2723

2824
const canRef = isValidElement(children) && supportRef(children);
2925

30-
const destroyObserver = () => {
31-
instance.current?.takeRecords();
32-
instance.current?.disconnect();
33-
};
34-
3526
useEffect(() => {
3627
if (!canUseDom()) {
3728
return;
3829
}
30+
let instance: MutationObserver;
3931
const currentElement = findDOMNode(wrapperRef.current!);
4032
if (currentElement && 'MutationObserver' in window) {
41-
instance.current = new MutationObserver(onMutate);
42-
instance.current.observe(currentElement, options);
33+
instance = new MutationObserver(onMutate);
34+
instance.observe(currentElement, options);
4335
}
44-
return destroyObserver;
36+
return () => {
37+
instance?.takeRecords();
38+
instance?.disconnect();
39+
};
4540
}, [options, onMutate]);
4641

4742
if (!children) {
@@ -52,7 +47,7 @@ const MutateObserver: React.FC<MutationObserverProps> = props => {
5247
}
5348

5449
return (
55-
<DomWrapper key={INTERNAL_PREFIX_KEY} ref={wrapperRef}>
50+
<DomWrapper ref={wrapperRef}>
5651
{canRef
5752
? cloneElement(children as any, { ref: (children as any).ref })
5853
: children}

0 commit comments

Comments
 (0)