souporserious / react-measure

📏 Compute measurements of a React component.
https://souporserious.github.io/react-measure/
MIT License
1.94k stars 109 forks source link

onResize fires with empty object #138

Open vineetit1991 opened 5 years ago

vineetit1991 commented 5 years ago

Error:- Uncaught TypeError: Cannot read property 'width' of undefined

<Measure
    onResize={({ entry: { width, height } }) => dispatch({
        type: XXXX_XXXXX,
        payload: {
            width: Math.ceil(width),
            height: Math.ceil(height)
        }
    })}
>

Version:- 2.3.0

souporserious commented 5 years ago

Ah, sorry I should have made this more clear in the 2.3.0 release that this could be a breaking change. It would probably be best to just bail out of onResize if no measurements were found on mount.

souporserious commented 5 years ago

This should fix the issue for now:

<Measure
    onResize={({ entry }) => entry && dispatch({
        type: XXXX_XXXXX,
        payload: {
            width: Math.ceil(entry.width),
            height: Math.ceil(entry.height)
        }
    })}
>

I'll think about a better solution here. Do you think the existence of entry is enough? The unfortunate part is it needs to fire onResize both in componentDidMount and the resize observer, that's why there are two calls to onResize on initial render 😞.