Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #237 - Do not call onResize during setSize updater function in createNotifier #239

Merged
merged 2 commits into from
Apr 9, 2023

Conversation

tylerprice1
Copy link

@tylerprice1 tylerprice1 commented Mar 29, 2023

Updated createNotifier to conditionally call setSize based on the current size. Also reference my comment here: #237 (comment)

Things work okay in the local example, and I didn't see any unit tests, but I'm happy to do any specific testing that you guys need.

@snelsi snelsi requested a review from maslianok March 29, 2023 17:56
@tylerprice1 tylerprice1 marked this pull request as ready for review March 29, 2023 18:26
@tylerprice1
Copy link
Author

This has the enhancement label, but it's fixing a React error. Should it be labelled a bug instead?

src/useResizeDetector.ts Outdated Show resolved Hide resolved
@tylerprice1 tylerprice1 marked this pull request as draft April 7, 2023 19:09
…ater function in \`createNotifier\`. The updater function must be pure.
@tylerprice1 tylerprice1 marked this pull request as ready for review April 8, 2023 14:32
@@ -31,22 +31,27 @@ export const isDOMElement = (element: unknown): boolean =>

export const createNotifier =
(
onResize: Props['onResize'],
sizeRef: Readonly<React.MutableRefObject<ReactResizeDetectorDimensions>>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this additional variable? Can't we simply compare width and height to the current state?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stored the current state inside a ref so that we can access its value within the effect without having to declare it as a dependency and recreate the ResizeObserver every time it changes. It's kind of a hacky approach to avoid stale values or triggering the effect too often

Or did you mean using the setState((prev) => { ... }); pattern?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. That's what you initially did and then refactored to the current solution...

Thank you! I'm going to merge the PR

@maslianok maslianok merged commit 2c13b62 into maslianok:master Apr 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants