There are several alternatives to the pattern we're using.
Restricts D3 to rendering only the data-bound elements. For example, here's React creating all of the non-data-bound elements of a chart.
class MyChart extends React.Component {
componentDidMount() {
// ...
}
render() {
return (
<div>
<h1>My chart</h1>
<svg>
<g className="chart group"></g>
</svg>
</div>
);
}
};
... now d3 renders the data-bound stuff:
componentDidMount() {
d3.select('g.chart.group').selectAll('.datapoints')
.data([ /* ... */ ]);
// etc.
}
Let React render all DOM elements, but let D3 help calculate the properties of those elements.
class BarChart extends React.Component {
render() {
const { data } = this.props;
const scale = d3.scaleLinear()
.range([0, 100])
.domain(d3.extent(data, d => d.value));
const bars = data.map(d => {
const styles = { width: scale(d.value) + '%' };
return (
<div styles={styles} />
);
});
return (
<div className="bar-chart">
{bars}
</div>
)
}
}