react-d3-cloud icon indicating copy to clipboard operation
react-d3-cloud copied to clipboard

Static width/height

Open antunespedro80 opened this issue 4 years ago • 1 comments

Hello ! Is it possible to pass width/height as string to make div fix to parent and not with a static width/height ?

antunespedro80 avatar Jan 17 '22 10:01 antunespedro80

This is how I solved this:

import { useMeasure } from "react-use";
...
  const [ready, setReady]  = useState(false);
   const [ref, { width, height }] = useMeasure();
...
  useEffect(()=>{
    if(width && height) setReady(true);
  },[width, height]);

Then use the ref in the DIV

<div className="parentdiv"  ref={ref}>
   {ready && 
          <WordCloud
            data={keywords}
            width={width}
            height={height}
            font="sans-serif"
            rotate={() => (Math.random() > 0.3 ? 0 : 90)}
            padding={5}
            fill={() => (Math.random() > 0.5 ? "#000" : "#fff")}
            fontWeight="bold"
          />
          }
</div>

robsilva avatar Jan 07 '24 23:01 robsilva