react-sizes
react-sizes copied to clipboard
Size props don't update when `resize` event is used in the child
React Sizes won't update the props when a child component is also using the resize
event.
Example - A child component of the component that I am using withSizes
on has a resize
handler defined.
window.addEventListener('resize', this.resize);
// using this.resize to re-render (child) component in case window resizes
When the above is defined, react sizes fails to update the props passed to the parent component.
When I remove the resize
handler from the child component and make no other changes, react-sizes
works as expected. That's why I think there could be a bug here.
Let me know if you need any more details.
Can you show me a minimal reproduction so I can take a look?
Thanks for the quick reply. I created a test sandbox to share here but the same doesn't seem to be happening in it. Looking into my original code, here is what is happening.
- window and document is at 1300px width.
- Using dev tools, we change the window to mobile width (<480px) instantly.
-
resize
handlers fire. The child has an absolute positioned element that takes some time to adjust to the new width because the width is based on a state value which is updated on resize. The absolute position element is initially taking 1200px width. - Because the child will take some time, viewport width is still at 1200px as the absolute element is still in its original size. So the parent with
withSizesHOC
doesn't updateisMobile
prop since breakpoint is at 480px. - The child finally adjusts the absolute element thus decreasing viewport width to be < 480px.
-
resize
event isn't fired now or maybe it is throttled causingwithSizes
to not update theisMobile
prop.
If it is a bug, it could be related to #32
However, since I am not able to replicate it on the test code, you can close this issue. I am thinking of using a different approach for what I am doing anyway.
You can find the test code here. https://codesandbox.io/embed/react-sizes-issue-r706q