0

There's a number of posts addressing some variant of the question - and amongst the most popular suggestion was to re-design the parent-child hierarchy to align with the react principle of having a unidirectional flow of data. I am currently working with a simple app designed to make some webplots using react-vis, a relatively large plotting package that's no longer being maintained.

My issue is that the legend component and plotting components are separate, and that ideally, one would like to access some variable inside the plotting component to set basic parameters of the legend. I've scrounged up a simple component based off one of the examples from the react-vis documentation. For this example, I would like to access a variable inside HexbinSeries and just console log it onto the main app.

import React, {useEffect} from 'react';
import {XYPlot, XAxis, YAxis, HexbinSeries} from 'react-vis'

  
export const EasyPlot = () => {

  const DATA = [
    {"eruptions": 3.6, "waiting": 79},
    {"eruptions": 3.6, "waiting": 79},
    {"eruptions": 3.6, "waiting": 79},
    {"eruptions": 1.8, "waiting": 54},
    {"eruptions": 4.533, "waiting": 85}
  ];

  useEffect(() => {
    //console.log('var from HexbinSeries logged here')
  })

  return (
    <div>
      <XYPlot
        width={400}
        getX={d => d.waiting}
        getY={d => d.eruptions}
        height={400}
      >
        <HexbinSeries
          colorRange={['orange', 'cyan']}
          radius={10}
          data={DATA}
        />
        <XAxis />
        <YAxis />
      </XYPlot>
    </div>
  );
};

The plot should be a hexbin histogram, with one hexagon with a count of 3, and the other hexagons with a count of 1 (from the data). The actual HexbinSeries component uses d3-hexbin to construct the paths for the plot, and one can easily derive the total counts per hexagon from this construction.

The naive solution is to reproduce this process on the main app (run d3-hexbin on my dataset), and determine the counts from there. But ideally, I would like to avoid having to re-run d3-hexbin on a large dataset twice for every render, especially if this plot was to be interactive and we needed to update the legend with count.

The second naive solution was to have a global variable or some DOM element that I would manually access inside the HexbinSeries.js to alter. But this is an obviously a single-use solution.

Edit:

Another method was to have the parent pass setState to HexbinSeries as a prop. However, HexbinSeries is written in a way beyond my comprehension, and currently it does not seem to have a constructor.

kevqnp
  • 153
  • 10
  • What variable do you need to access inside HexbinSeries and how much control do you have over HexbinSerie's code? I suppose what I'm asking is: can you modify HexbinSeries code? – Claudio Sep 03 '21 at 07:10
  • I can follow the file, but I'm not used to the way used to create the component. For now, I'm trying to get the variable called 'hexes' under 'render'. I could follow the same syntax and make a key, 'componentDidMount' and successfully use the setState hook to set some variable in the parent class, but the way it's written makes componentDidUpdate very difficult to implement (as all the variables are defined under render) – kevqnp Sep 03 '21 at 07:36
  • So you can write code in HexbinSeries? You’re not just importing it from a library? – Claudio Sep 04 '21 at 00:49
  • Yes. We've installed packages locally using npm. – kevqnp Sep 05 '21 at 15:16
  • You'll have to raise a PR to HexbinSeries repo if you want to apply changes. If you plan on doing that please tell me and I'll post the answer – Claudio Sep 05 '21 at 23:12
  • Thanks for your input - I've hacked my way to get it to work by moving where the calculation is done for the plotting. Calculations are now done prior to rendering, and I've also passed setState as a prop from the parent (sounds like bad practice tbh). I don't think I'll make any changes to the plotting repo. – kevqnp Sep 06 '21 at 04:31

0 Answers0