maslianok / react-resize-detector

A Cross-Browser, Event-based, Element Resize Detection for React
http://maslianok.github.io/react-resize-detector/
MIT License
1.25k stars 91 forks source link

Fail with version 5.0.0 #91

Closed shenenya closed 4 years ago

shenenya commented 4 years ago

I got error as follows when upgrade from 4.2.1 to 5.0.0. I used Child Function Pattern and the height returned is 0. Thanks.

截屏2020-06-15 上午9 27 13

maslianok commented 4 years ago

@eeyshen sorry for console.log. It's removed in v5.0.3

Re. height 0 - please show the code so I can reproduce the error.

shenenya commented 4 years ago

I use code as follows. The father element is a component of flexlayout-react. This works with v4.2.1, but failed with v5.0.0. Thanks.

<ReactResizeDetector handleWidth handleHeight>
  {({ width, height }) => <div>{`${width}x${height}`}</div>}
</ReactResizeDetector>;
maslianok commented 4 years ago

It's possible that 4.2.1 took the parent's width&height. This was the wrong behavior.

The current version listens to the first child changes. In the example above it will show the inner div dimensions. So please make sure they are not 0.

As you can see, the example above works fine https://codesandbox.io/s/wonderful-tdd-8dwnd

maslianok commented 4 years ago

To make it work please set the inner div height to 100% or move the ReactResizeDetector wrapper one level above.

For example:

Before:

<Parent>
  <ReactResizeDetector handleWidth handleHeight>
    {({ width, height }) => <div>{`${width}x${height}`}</div>}
  </ReactResizeDetector>
</Parent>

After: (in this case it will measure Parent dimensions)

<ReactResizeDetector handleWidth handleHeight>
  {({ width, height }) => <Parent><div>{`${width}x${height}`}</div></Parent>}
</ReactResizeDetector>

or (the inner div takes 100% height)

<Parent>
  <ReactResizeDetector handleWidth handleHeight>
    {({ width, height }) => <div style={{ height: '100%' }}>{`${width}x${height}`}</div>}
  </ReactResizeDetector>
</Parent>
shenenya commented 4 years ago

Solved. Thanks.