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

[ResizeSensor] try/catch findDOMNode to handle possible error state #3236

Merged
merged 1 commit into from
Jan 9, 2019
Merged
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
24 changes: 17 additions & 7 deletions packages/core/src/components/resize-sensor/resizeSensor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,24 @@ export class ResizeSensor extends React.PureComponent<IResizeSensorProps> {
}

public componentDidMount() {
// using findDOMNode for two reasons:
// 1. cloning to insert a ref is unwieldy and not performant.
// 2. ensure that we get an actual DOM node for observing.
this.observeElement(findDOMNode(this));
this.observeElement();
}

public componentDidUpdate(prevProps: IResizeSensorProps) {
this.observeElement(findDOMNode(this), this.props.observeParents !== prevProps.observeParents);
this.observeElement(this.props.observeParents !== prevProps.observeParents);
}

public componentWillUnmount() {
this.observer.disconnect();
}

/**
* Observe the given element, if defined and different from the currently
* Observe the DOM element, if defined and different from the currently
* observed element. Pass `force` argument to skip element checks and always
* re-observe.
*/
private observeElement(element: Element | Text | null, force = false) {
private observeElement(force = false) {
const element = this.getElement();
if (!(element instanceof Element)) {
// stop everything if not defined
this.observer.disconnect();
Expand Down Expand Up @@ -106,4 +104,16 @@ export class ResizeSensor extends React.PureComponent<IResizeSensorProps> {
}
}
}

private getElement() {
try {
// using findDOMNode for two reasons:
// 1. cloning to insert a ref is unwieldy and not performant.
// 2. ensure that we resolve to an actual DOM node (instead of any JSX ref instance).
return findDOMNode(this);
} catch {
// swallow error if findDOMNode is run on unmounted component.
return null;
}
}
}