-
-
Notifications
You must be signed in to change notification settings - Fork 137
Remove deprecated react methods; refactor #128
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,11 +61,10 @@ export default function plotComponentFactory(Plotly) { | |
this.getRef = this.getRef.bind(this); | ||
this.handleUpdate = this.handleUpdate.bind(this); | ||
this.figureCallback = this.figureCallback.bind(this); | ||
this.updatePlotly = this.updatePlotly.bind(this); | ||
} | ||
|
||
componentDidMount() { | ||
this.unmounting = false; | ||
|
||
updatePlotly(shouldInvokeResizeHandler, figureCallbackFunction, shouldAttachUpdateEvents) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for whatever reason, aesthetically it seems nicer to me to have the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean the lifecycle methods? Yeah, I was on the fence here. I decided to do |
||
this.p = this.p | ||
.then(() => { | ||
if (!this.el) { | ||
|
@@ -78,17 +77,17 @@ export default function plotComponentFactory(Plotly) { | |
} | ||
throw error; | ||
} | ||
return Plotly.newPlot(this.el, { | ||
return Plotly.react(this.el, { | ||
data: this.props.data, | ||
layout: this.props.layout, | ||
config: this.props.config, | ||
frames: this.props.frames, | ||
}); | ||
}) | ||
.then(() => this.syncWindowResize(null, true)) | ||
.then(() => this.syncWindowResize(shouldInvokeResizeHandler)) | ||
.then(this.syncEventHandlers) | ||
.then(this.attachUpdateEvents) | ||
.then(() => this.figureCallback(this.props.onInitialized)) | ||
.then(() => this.figureCallback(figureCallbackFunction)) | ||
nicolaskruchten marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.then(shouldAttachUpdateEvents ? this.attachUpdateEvents : () => {}) | ||
.catch(err => { | ||
if (err.reason === 'unmounting') { | ||
return; | ||
|
@@ -100,59 +99,35 @@ export default function plotComponentFactory(Plotly) { | |
}); | ||
} | ||
|
||
UNSAFE_componentWillUpdate(nextProps) { | ||
componentDidMount() { | ||
this.unmounting = false; | ||
|
||
this.updatePlotly(true, this.props.onInitialized, true); | ||
} | ||
|
||
componentDidUpdate(prevProps) { | ||
nicolaskruchten marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.unmounting = false; | ||
|
||
// frames *always* changes identity so fall back to check length only :( | ||
const numPrevFrames = | ||
this.props.frames && this.props.frames.length ? this.props.frames.length : 0; | ||
prevProps.frames && prevProps.frames.length ? prevProps.frames.length : 0; | ||
const numNextFrames = | ||
nextProps.frames && nextProps.frames.length ? nextProps.frames.length : 0; | ||
this.props.frames && this.props.frames.length ? this.props.frames.length : 0; | ||
|
||
const figureChanged = !( | ||
nextProps.layout === this.props.layout && | ||
nextProps.data === this.props.data && | ||
nextProps.config === this.props.config && | ||
prevProps.layout === this.props.layout && | ||
prevProps.data === this.props.data && | ||
prevProps.config === this.props.config && | ||
numNextFrames === numPrevFrames | ||
); | ||
const revisionDefined = nextProps.revision !== void 0; | ||
const revisionChanged = nextProps.revision !== this.props.revision; | ||
const revisionDefined = prevProps.revision !== void 0; | ||
const revisionChanged = prevProps.revision !== this.props.revision; | ||
|
||
if (!figureChanged && (!revisionDefined || (revisionDefined && !revisionChanged))) { | ||
return; | ||
} | ||
|
||
this.p = this.p | ||
.then(() => { | ||
if (!this.el) { | ||
let error; | ||
if (this.unmounting) { | ||
error = new Error('Component is unmounting'); | ||
error.reason = 'unmounting'; | ||
} else { | ||
error = new Error('Missing element reference'); | ||
} | ||
throw error; | ||
} | ||
return Plotly.react(this.el, { | ||
data: nextProps.data, | ||
layout: nextProps.layout, | ||
config: nextProps.config, | ||
frames: nextProps.frames, | ||
}); | ||
}) | ||
.then(() => this.syncEventHandlers(nextProps)) | ||
.then(() => this.syncWindowResize(nextProps)) | ||
.then(() => this.figureCallback(nextProps.onUpdate)) | ||
.catch(err => { | ||
if (err.reason === 'unmounting') { | ||
return; | ||
} | ||
console.error('Error while plotting:', err); // eslint-disable-line no-console | ||
if (this.props.onError) { | ||
this.props.onError(err); | ||
} | ||
}); | ||
this.updatePlotly(false, this.props.onUpdate, false); | ||
} | ||
|
||
componentWillUnmount() { | ||
|
@@ -175,19 +150,19 @@ export default function plotComponentFactory(Plotly) { | |
return; | ||
} | ||
|
||
for (let i = 0; i < updateEvents.length; i++) { | ||
this.el.on(updateEvents[i], this.handleUpdate); | ||
} | ||
updateEvents.forEach(updateEvent => { | ||
this.el.on(updateEvent, this.handleUpdate); | ||
}); | ||
} | ||
|
||
removeUpdateEvents() { | ||
if (!this.el || !this.el.removeListener) { | ||
return; | ||
} | ||
|
||
for (let i = 0; i < updateEvents.length; i++) { | ||
this.el.removeListener(updateEvents[i], this.handleUpdate); | ||
} | ||
updateEvents.forEach(updateEvent => { | ||
this.el.removeListener(updateEvent, this.handleUpdate); | ||
}); | ||
} | ||
|
||
handleUpdate() { | ||
|
@@ -203,21 +178,18 @@ export default function plotComponentFactory(Plotly) { | |
} | ||
} | ||
|
||
syncWindowResize(propsIn, invoke) { | ||
const props = propsIn || this.props; | ||
syncWindowResize(invoke) { | ||
if (!isBrowser) { | ||
return; | ||
} | ||
|
||
if (props.useResizeHandler && !this.resizeHandler) { | ||
this.resizeHandler = () => { | ||
return Plotly.Plots.resize(this.el); | ||
}; | ||
if (this.props.useResizeHandler && !this.resizeHandler) { | ||
this.resizeHandler = () => Plotly.Plots.resize(this.el); | ||
window.addEventListener('resize', this.resizeHandler); | ||
if (invoke) { | ||
this.resizeHandler(); | ||
} | ||
} else if (!props.useResizeHandler && this.resizeHandler) { | ||
} else if (!this.props.useResizeHandler && this.resizeHandler) { | ||
window.removeEventListener('resize', this.resizeHandler); | ||
this.resizeHandler = null; | ||
} | ||
|
@@ -232,13 +204,9 @@ export default function plotComponentFactory(Plotly) { | |
} | ||
|
||
// Attach and remove event handlers as they're added or removed from props: | ||
syncEventHandlers(propsIn) { | ||
// Allow use of nextProps if passed explicitly: | ||
const props = propsIn || this.props; | ||
|
||
for (let i = 0; i < eventNames.length; i++) { | ||
const eventName = eventNames[i]; | ||
const prop = props['on' + eventName]; | ||
syncEventHandlers() { | ||
eventNames.forEach(eventName => { | ||
const prop = this.props['on' + eventName]; | ||
const hasHandler = Boolean(this.handlers[eventName]); | ||
|
||
if (prop && !hasHandler) { | ||
|
@@ -249,7 +217,7 @@ export default function plotComponentFactory(Plotly) { | |
this.el.removeListener('plotly_' + eventName.toLowerCase(), this.handlers[eventName]); | ||
delete this.handlers[eventName]; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
render() { | ||
|
@@ -281,9 +249,9 @@ export default function plotComponentFactory(Plotly) { | |
divId: PropTypes.string, | ||
}; | ||
|
||
for (let i = 0; i < eventNames.length; i++) { | ||
PlotlyComponent.propTypes['on' + eventNames[i]] = PropTypes.func; | ||
} | ||
eventNames.forEach(eventName => { | ||
PlotlyComponent.propTypes['on' + eventName] = PropTypes.func; | ||
}); | ||
|
||
PlotlyComponent.defaultProps = { | ||
debug: false, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you know, I forgot we have minimal tests for this thing... Could you please add some tests for the logic in #126 please? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(you can add the tests in a separate PR, but we should get them in)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done