Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove use of findDOMNode #67

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from 'react';
import findDOMNode from 'rc-util/lib/Dom/findDOMNode';
import toArray from 'rc-util/lib/Children/toArray';
import warning from 'rc-util/lib/warning';
import { composeRef, supportRef } from 'rc-util/lib/ref';
Expand Down Expand Up @@ -37,9 +36,9 @@ class ReactResizeObserver extends React.Component<ResizeObserverProps, ResizeObs

resizeObserver: ResizeObserver | null = null;

childNode: RefNode = null;
currentElement: RefNode | null = null;

currentElement: Element | null = null;
childRef = React.createRef<RefNode>();

state = {
width: 0,
Expand Down Expand Up @@ -70,7 +69,7 @@ class ReactResizeObserver extends React.Component<ResizeObserverProps, ResizeObs
}

// Unregister if element changed
const element = findDOMNode(this.childNode || this) as Element;
const element = this.childRef.current;
const elementChanged = element !== this.currentElement;
if (elementChanged) {
this.destroyObserver();
Expand All @@ -79,7 +78,7 @@ class ReactResizeObserver extends React.Component<ResizeObserverProps, ResizeObs

if (!this.resizeObserver && element) {
this.resizeObserver = new ResizeObserver(this.onResize);
this.resizeObserver.observe(element);
this.resizeObserver.observe(element as Element);
}
}

Expand Down Expand Up @@ -125,10 +124,6 @@ class ReactResizeObserver extends React.Component<ResizeObserverProps, ResizeObs
}
};

setChildNode = (node: RefNode) => {
this.childNode = node;
};

destroyObserver() {
if (this.resizeObserver) {
this.resizeObserver.disconnect();
Expand All @@ -153,11 +148,11 @@ class ReactResizeObserver extends React.Component<ResizeObserverProps, ResizeObs

const childNode = childNodes[0];

if (React.isValidElement(childNode) && supportRef(childNode)) {
if (React.isValidElement<any>(childNode) && supportRef(childNode)) {
const { ref } = childNode as any;

childNodes[0] = React.cloneElement(childNode as any, {
ref: composeRef(ref, this.setChildNode),
childNodes[0] = React.cloneElement(childNode, {
ref: composeRef(ref, this.childRef),
});
}

Expand Down