Skip to content
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
28 changes: 7 additions & 21 deletions polydodo/src/components/d3component.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
import React from "react";
import PropTypes from "prop-types";
import React, { useEffect, useRef } from "react";

class D3Component extends React.Component {
static propTypes = {
callback: PropTypes.func.isRequired,
}
const D3Component = React.memo(({ callback }) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Use React.memo so we don't rerender when the parent does rerender with that same callback prop. We want that since D3 will handle DOM updates inside this svg.

const ref = useRef();
useEffect(() => callback(ref.current));
return <svg ref={ref} />;
});

componentDidMount() {
const { callback } = this.props;
callback(this.svg);
}

shouldComponentUpdate() {
return false;
}

render() {
return (<svg ref={ref => this.svg = ref } />);
}
}

export default D3Component;
export default D3Component;