visx icon indicating copy to clipboard operation
visx copied to clipboard

ReactDOM.findDOMNode() in @vx/bounds

Open sosloow opened this issue 4 years ago • 14 comments

Hi, we encountered this warning

Warning: findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of withBoundingRects() which is inside StrictMode. Instead, add a ref directly to the element you want to reference. Learn more about using refs safely here: https://fb.me/react-strict-mode-find-node
    in div (created by Tooltip)
    in Tooltip (created by TooltipWithBounds)
    in TooltipWithBounds (created by withBoundingRects())

Is it possible to replace findDOMNode with a ref in WrappedComponent.componentDidMount here?

sosloow avatar Jun 02 '20 09:06 sosloow

Previous discussion: https://github.com/hshoff/vx/pull/509#discussion_r331679227

hshoff avatar Jun 02 '20 13:06 hshoff

Hey @sosloow, I would love to help. Can you please explain the scope of the changes like have to update this accross the repo or just few files?

dhairyanadapara avatar Oct 02 '20 14:10 dhairyanadapara

@hshoff I have made some changes to replace the findDOMNode with ref. I tried to test with jest but doesn't get any idea. Can you please help me how I can test that changes doesn't breaking things? thanks :)

dhairyanadapara avatar Oct 03 '20 07:10 dhairyanadapara

Came across this same issue when using <TooltipWithBounds />: https://codesandbox.io/s/discontinuous-dates-visx-3ve9k?file=/src/App.js If I had a suggestion about how to overcome this it would be to pass a ref from the bounding element:

const ref = useRef()
const {} = useTooltip({containerRef: ref})

return (
  <div>
    <svg ref={ref}>
      ...
    </svg>

    <TooltipWithBounds />
  </div>
)

peterwiebe avatar Oct 20 '20 15:10 peterwiebe

Using a ref is definitely the right fix here (and this is now what we do for useTooltipInPortal), but we'd like to do it in a way that isn't a breaking change just yet. This probably means that we need a way to support both (in both @visx/bounds and in @visx/tooltip where it's used).

One option would be to leave the withBoundingRects as is, and introduce a new hook useBoundingRects + a new NewTooltipWithBounds (with better name) which could require a ref/ResizeObserver polyfill (see below) and not be a breaking change. In a future breaking change these would then become the defaults:

@visx/bounds changes

// @visx/bounds
import useMeaure from 'react-use-measure';

// `useMeasureOptions` may include `ResizeObserver` polyfill else it must be
// defined globally
function useBoundingRects(useMeasureOptions) {
  const [privateContainerRef, containerBounds] = useMeasure(useMeasureOptions);
  const [privateParentRef, parentBounds] = useMeasure(useMeasureOptions);

  // set both container + parent container in public ref
  const containerRef = useCallback(
    (container: HTMLElement | SVGElement | null) => {
      privateContainerRef(container);
      privateParentRef(container?.parentElement ?? null);
    },
    [privateContainerRef, privateParentRef],
  ); 

  return { containerRef, containerBounds, parentBounds };
}

@visx/tooltip changes

// @visx/tooltip
import { useBoundingRects } from `@visx/bounds';

// a new component avoids a breaking change because `useBoundingRects` / `react-use-measure`
// requires ResizeObserver polyfill to be passed or defined globally
function NewTooltipWithBounds(useMeasureOptions) {
  const { containerRef, containerBounds, parentBounds } = useBoundingRects(useMeasureOptions);
  
  // ...bounds detection logic in existing `TooltipWithBounds`

  return (
    <Tooltip ref={containerRef} {...}>{...}</Tooltip>
  );
}

and finally usage

// your app
function MyChart() {
  // ...
  return (
    <div>
      <svg>{...}</svg>
      <NewTooltipWithBounds  />
    </div>
  );
}

williaster avatar Oct 21 '20 19:10 williaster

hey, can we help somehow to fix this issue? Is there any blocker or does a solution already exist?

pfried avatar Dec 16 '21 12:12 pfried

I'm happy to review a PR if someone is interested in creating the new version (ideally done in a way that is not breaking, and then in the next major release we will deprecate the findDOMNode() version. Heads up I am on leave all of Dec so probably won't review until Jan.

williaster avatar Dec 16 '21 21:12 williaster

Hi, what's the current status on this? I see this for example when I run the example at: https://airbnb.io/visx/areas

Is it recommended that we use a Portal for everything and use useTooltipInPortal even if we aren't running into any z-index issues?

rminami avatar Feb 09 '22 08:02 rminami

Any updates on this?

o-az avatar Mar 01 '22 03:03 o-az

Bump, still seeing findDOMNode was passed an instance of withBoundingRects() console warnings

nicksiscoe avatar Apr 23 '22 13:04 nicksiscoe

Any update with this by chance?

dwootton avatar Jun 01 '22 21:06 dwootton

Still finding this issue, any updates?

Pranay0302 avatar Jun 08 '22 06:06 Pranay0302

Still experiencing this issue too

nvme0 avatar Jun 13 '22 03:06 nvme0

Still running into this issue!

steven-tey avatar Sep 01 '22 14:09 steven-tey

Hi, this was a blocking issue for me as well. I have submitted a PR with possible solution here

0xmax avatar Oct 17 '22 16:10 0xmax

fixed by https://github.com/airbnb/visx/pull/1583

williaster avatar Oct 19 '22 19:10 williaster