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

export TS types #98

Closed
frolic opened this issue Nov 16, 2022 · 9 comments · Fixed by #99
Closed

export TS types #98

frolic opened this issue Nov 16, 2022 · 9 comments · Fixed by #99

Comments

@frolic
Copy link

frolic commented Nov 16, 2022

I have a throttled onResize handler. I found that unless I wrap in a useCallback, the throttling doesn't take effect because the resize handler triggers a dimension change, which triggers a rerender, which replaces the onResize handler with a new one (starting a fresh throttle timer).

However, moving out the onResize handler means I lose out on some type inference. I don't love copying over types and would prefer to import them. Looks like this library declares types but doesn't export them, so I can't use them directly.

image

image

Here's my current workaround:
image

@ZeeCoder
Copy link
Owner

Yeah I see no reason not to 🤔

@ZeeCoder ZeeCoder added the enhancement New feature or request label Nov 17, 2022
ZeeCoder added a commit that referenced this issue Nov 21, 2022
The following types were exported so that it's easier to compose hook
options:
- ObservedSize
- ResizeHandler
- ResizeObserverBoxOptions
- RoundingFunction

Tests were updated as well to guarantee the new API interface.

Resolves #98
github-actions bot pushed a commit that referenced this issue Nov 21, 2022
# [9.1.0-alpha.1](v9.0.2...v9.1.0-alpha.1) (2022-11-21)

### Features

* Exported some TS types ([e056e19](e056e19)), closes [#98](#98)
@github-actions
Copy link

🎉 This issue has been resolved in version 9.1.0-alpha.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@ZeeCoder
Copy link
Owner

@holic could you check if the above mentioned alpha release addresses this as expected?

@frolic
Copy link
Author

frolic commented Nov 21, 2022

image

Looks great, thank you!

On the original motivation/need for this, is it expected that the resize handler reference (when re-rendering) is not "stable"? Is it expected that we should wrap our onResize in a useCallback to make it stable?

image

(just passing in the throttle-wrapped function into onResize results in ~nothing being throttled, because the function is recreated each render, which causes useResizeObserver's useEffect to reevaluate.

@ZeeCoder
Copy link
Owner

Great!

On throttling, I'm not sure I understand the issue you're facing, can you share a codesandbox by any chance?
Also, is the throttling example in the docs not working for you?
(See here: https://codesandbox.io/s/use-resize-observer-throttle-and-debounce-8uvsg?file=/src/useThrottledResizeObserver.js)

On the callback:
uRO always calls the latest onResize instance.
But regardless of what the hook does though, in order for a throttle function to work you'd have to have a stable instance, so that it can internally register the individual calls and handle the throttling.

(I'll hold off on releasing until we wrap up this discussion in case there's anything else actionable here.)

@frolic
Copy link
Author

frolic commented Nov 22, 2022

I hadn't seen the example, but it's doing what I ended up with (I just happened to thread it through useCallback instead of useMemo). A stable instance is what I was wondering about!

All good to go here. Thank you!

ZeeCoder added a commit that referenced this issue Nov 22, 2022
The following types were exported so that it's easier to compose hook
options:
- ObservedSize
- ResizeHandler
- ResizeObserverBoxOptions
- RoundingFunction

Tests were updated as well to guarantee the new API interface.

Resolves #98
ZeeCoder pushed a commit that referenced this issue Nov 22, 2022
# [9.1.0-alpha.1](v9.0.2...v9.1.0-alpha.1) (2022-11-21)

### Features

* Exported some TS types ([e056e19](e056e19)), closes [#98](#98)
github-actions bot pushed a commit that referenced this issue Nov 22, 2022
# [9.1.0](v9.0.2...v9.1.0) (2022-11-22)

### Bug Fixes

* trigger release ([da154e1](da154e1))

### Features

* Exported some TS types ([3895bdf](3895bdf)), closes [#98](#98)
@github-actions
Copy link

🎉 This issue has been resolved in version 9.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@ZeeCoder
Copy link
Owner

Nice, one last question @holic if you had time: did you not find the throttle example in the docs, or did you just not think to check them at all?
I'm just wondering if there's anything I could improve on the docs in your opinion to maybe better surface examples like these?

@frolic
Copy link
Author

frolic commented Nov 22, 2022

I just missed that there was an example for throttling, just went straight to implementing it myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants