-
Notifications
You must be signed in to change notification settings - Fork 3
explore handling all rendering w/ React #13
Copy link
Copy link
Open
Description
e.g.
this:
export default class ScatterPlot extends React.Component {
componentDidMount() {
this.update();
}
componentDidUpdate() {
this.update();
}
update() {
const { data, x, y, containerWidth, containerHeight, yScale, xScale } = this.props;
const $chart = d3.select(this.$chart);
xScale.rangeRound([0, containerWidth]);
yScale.rangeRound([containerHeight, 0]);
const update = $chart.selectAll('.bar')
.data(data);
const enter = update.enter();
const exit = update.exit();
enter
.append('circle')
.attr('class', 'dot')
.merge(update)
.attr('r', 3.5)
.attr('cx', d => xScale(x(d)))
.attr('cy', d => yScale(y(d)));
exit
.remove();
}
render() {
const { containerHeight, containerWidth, children } = this.props;
return (
<g
ref={el => this.$chart = el}
>
{ React.Children.map(children, child => React.cloneElement(child, {containerWidth, containerHeight})) }
</g>
);
}
}vs
const ScatterPlot = ({ data, xScale, yScale, x, y, containerHeight, containerWidth, children }) => {
xScale.rangeRound([0, containerWidth]);
yScale.rangeRound([containerHeight, 0]);
return (
<g
ref={el => this.$chart = el}
>
{data.map(d => (
<circle
key={x(d)}
className='dot'
r='3.5'
cx={xScale(x(d))}
cy={yScale(y(d))}
/>
))}
{ React.Children.map(children, child => React.cloneElement(child, {containerWidth, containerHeight})) }
</g>
);
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels