Skip to content

explore handling all rendering w/ React #13

@jameslaneconkling

Description

@jameslaneconkling

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>
    );
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions