Integrating the Hook with Your Graph


We have a hook that listens for changes in a div's dimensions and returns those dimensions.

Now, it's time to use this hook to inject the dimensions into our graph!

Wip
4 minutes read

Create a ref

Start by creating a ref using the React useRef() function.

A ref allows you to target and interact with a specific HTML element in the DOM of your application.

const chartRef = useRef(null);

Finally, pass the chartRef to the container you want to monitor.

return(
  <div ref={chartRef}>
    <MyChartComponent
      height={chartSize.height}
      width={chartSize.width}
  </div>
)

Use the hook

Then, pass this newly created chartRef to the hook:

const chartSize = useDimensions(chartRef);

You now have an object called chartSize with two properties: height and width. These properties can be used in your chart component. 🔥

Pro tip: Before building a responsive visualization, use console.log() to check the chartSize object and ensure everything is working correctly.

📊 Application

You’re all set! Just pass the values from chartSize to the visualization component, and it will become responsive.

Here is a full example with code: