Updating Render to use hooks

This commit is contained in:
Jeff Avallone 2019-03-28 06:56:31 -04:00
parent 14c1e14f51
commit 97878c3881

View File

@ -1,4 +1,4 @@
import React from 'react'; import React, { useRef, useCallback, useEffect } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import nodeTypes from 'rendering/types'; import nodeTypes from 'rendering/types';
@ -40,48 +40,40 @@ const render = (data, key) => {
</React.Fragment>; </React.Fragment>;
}; };
class Render extends React.PureComponent { const Render = ({ data, onRender }) => {
static propTypes = { const svgContainer = useRef();
data: PropTypes.object.isRequired,
onRender: PropTypes.func.isRequired
}
svgContainer = React.createRef() const provideSVGData = useCallback(() => {
if (!svgContainer.current) {
componentDidMount() {
this.provideSVGData();
}
componentDidUpdate() {
this.provideSVGData();
}
provideSVGData() {
if (!this.svgContainer.current) {
return; return;
} }
const svg = this.svgContainer.current.querySelector('svg'); const svg = svgContainer.current.querySelector('svg');
this.props.onRender({ onRender({
svg: svg.outerHTML, svg: svg.outerHTML,
width: Number(svg.getAttribute('width')), width: Number(svg.getAttribute('width')),
height: Number(svg.getAttribute('height')) height: Number(svg.getAttribute('height'))
}); });
} }, [svgContainer, onRender]);
render() { useEffect(() => {
const { data } = this.props; provideSVGData();
}, [provideSVGData]);
return <div className={ style.render } ref={ this.svgContainer }> return <div className={ style.render } ref={ svgContainer }>
{ render({ { render({
...data, ...data,
props: { props: {
...data.props, ...data.props,
onReflow: this.provideSVGData onReflow: provideSVGData
} }
}) } }) }
</div>; </div>;
} };
}
Render.propTypes = {
data: PropTypes.object.isRequired,
onRender: PropTypes.func.isRequired
};
export default Render; export default Render;