Skip to content

Commit a94c08a

Browse files
committed
fix
1 parent a78723c commit a94c08a

2 files changed

Lines changed: 19 additions & 14 deletions

File tree

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
{
2-
"name": "@rc-component/portal",
3-
"version": "1.0.3",
4-
"description": "React Portal Component",
2+
"name": "@rc-component/mutate-observer",
3+
"version": "0.0.1",
4+
"description": "React MutateObserver Component",
55
"keywords": [
66
"react",
77
"react-component",
8-
"react-portal"
8+
"mutate-portal"
99
],
10-
"homepage": "https://github.com/react-component/portal",
10+
"homepage": "https://github.com/react-component/mutate-observer",
1111
"bugs": {
12-
"url": "https://github.com/react-component/portal/issues"
12+
"url": "https://github.com/react-component/mutate-observer/issues"
1313
},
1414
"repository": {
1515
"type": "git",
16-
"url": "https://github.com/react-component/portal.git"
16+
"url": "https://github.com/react-component/mutate-observer.git"
1717
},
1818
"license": "MIT",
19-
"author": "smith3816@gmail.com",
19+
"author": "574980606@qq.com",
2020
"main": "./lib/index",
2121
"module": "./es/index",
2222
"typings": "es/index.d.ts",

src/index.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useEffect, useRef } from 'react';
2-
import { supportRef } from 'rc-util/lib/ref';
2+
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';
@@ -16,11 +16,16 @@ const MutateObserver: React.FC<MutationObserverProps> = props => {
1616

1717
const wrapperRef = useRef<DomWrapper>(null);
1818

19+
const elementRef = React.useRef<HTMLElement>(null);
20+
1921
const canRef = React.isValidElement(children) && supportRef(children);
2022

21-
const originRef: React.RefObject<HTMLElement> = canRef
22-
? (children as any)?.ref
23-
: null;
23+
const originRef: React.Ref<Element> = canRef ? (children as any)?.ref : null;
24+
25+
const mergedRef = React.useMemo<React.Ref<Element>>(
26+
() => composeRef<Element>(originRef, elementRef),
27+
[originRef, elementRef],
28+
);
2429

2530
useEffect(() => {
2631
if (!canUseDom()) {
@@ -30,7 +35,7 @@ const MutateObserver: React.FC<MutationObserverProps> = props => {
3035
let instance: MutationObserver;
3136

3237
const currentElement = findDOMNode(
33-
originRef?.current || wrapperRef?.current,
38+
(originRef as any)?.current || wrapperRef?.current,
3439
);
3540

3641
if (currentElement && 'MutationObserver' in window) {
@@ -53,7 +58,7 @@ const MutateObserver: React.FC<MutationObserverProps> = props => {
5358
return (
5459
<DomWrapper ref={wrapperRef}>
5560
{canRef
56-
? React.cloneElement(children as any, { ref: (children as any).ref })
61+
? React.cloneElement(children as any, { ref: mergedRef })
5762
: children}
5863
</DomWrapper>
5964
);

0 commit comments

Comments
 (0)