Skip to content

Commit

Permalink
[ResizeSensor] try/catch findDOMNode to handle possible error state
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilad Gray committed Dec 13, 2018
1 parent cde3dc2 commit e080f2e
Showing 1 changed file with 17 additions and 7 deletions.
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;
}
}
}

1 comment on commit e080f2e

@blueprint-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Previews: documentation | landing | table

Please sign in to comment.