-
Notifications
You must be signed in to change notification settings - Fork 91
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
Fix ref .current
set by using a proxy
#249
Conversation
e51036b
to
81b0246
Compare
Since 9.0.0, when `.current` is set, this does not trigger a rerender. This is because the `setRefElement` function is only called when the ref set is called as a function. Certain libraries will set `.current` directly instead of calling the ref function, which React normally handles with a Ref, but the ref-like function in this hook introduced in 9.0.0 does not handle this case. This PR fixes that by using a proxy to properly call `setRefElement` when `.current` is set.
81b0246
to
8ba4897
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've never seen anyone using Proxy
to handle a ref change in React before.
But I guess that works. 🤔
Need to do some testing to make sure it works, though
src/useResizeDetector.ts
Outdated
} | ||
}, | ||
|
||
const onRefChange: OnRefChangeType<T> = useMemo( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should update onRefChange
property name to better represent it's purpose.
Consider renaming it to refProxy
or ref
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, fixed
src/useResizeDetector.ts
Outdated
get(target, prop) { | ||
if (prop === 'current') { | ||
return refElement; | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like you don't need this else
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the proxy not to break anything. If I remove the else it becomes impossible to set other properties on the ref. Not sure if anyone is doing that or should be supported, but this at least keeps the behaviour the same. I could change it to not set the prop and return false
if you think that fits better
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant that you don't need the "else" word itself 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see 😅. Changed it
Published in v10.0.0-beta.1 and v9.1.2-beta.0 |
Nice! 🔥 |
## [0.5.7](https://github.com/equinor/webviz-subsurface-components/compare/[email protected]@0.5.7) (2024-02-28) ### Bug Fixes * bump react-resize-detector from 9.1.0 to 10.0.1 in /typescript ([#1935](#1935)) ([df5632d](df5632d)), closes [maslianok/react-resize-detector#249](maslianok/react-resize-detector#249) [maslianok/react-resize-detector#249](maslianok/react-resize-detector#249) [maslianok/react-resize-detector#249](maslianok/react-resize-detector#249) [maslianok/react-resize-detector#249](maslianok/react-resize-detector#249) [#247](#247)
## [1.2.7](https://github.com/equinor/webviz-subsurface-components/compare/[email protected]@1.2.7) (2024-02-28) ### Bug Fixes * bump react-resize-detector from 9.1.0 to 10.0.1 in /typescript ([#1935](#1935)) ([df5632d](df5632d)), closes [maslianok/react-resize-detector#249](maslianok/react-resize-detector#249) [maslianok/react-resize-detector#249](maslianok/react-resize-detector#249) [maslianok/react-resize-detector#249](maslianok/react-resize-detector#249) [maslianok/react-resize-detector#249](maslianok/react-resize-detector#249) [#247](#247)
## [0.18.1](https://github.com/equinor/webviz-subsurface-components/compare/[email protected]@0.18.1) (2024-02-28) ### Bug Fixes * bump react-resize-detector from 9.1.0 to 10.0.1 in /typescript ([#1935](#1935)) ([df5632d](df5632d)), closes [maslianok/react-resize-detector#249](maslianok/react-resize-detector#249) [maslianok/react-resize-detector#249](maslianok/react-resize-detector#249) [maslianok/react-resize-detector#249](maslianok/react-resize-detector#249) [maslianok/react-resize-detector#249](maslianok/react-resize-detector#249) [#247](#247)
## [1.4.8](https://github.com/equinor/webviz-subsurface-components/compare/[email protected]@1.4.8) (2024-02-28) ### Bug Fixes * bump react-resize-detector from 9.1.0 to 10.0.1 in /typescript ([#1935](#1935)) ([df5632d](df5632d)), closes [maslianok/react-resize-detector#249](maslianok/react-resize-detector#249) [maslianok/react-resize-detector#249](maslianok/react-resize-detector#249) [maslianok/react-resize-detector#249](maslianok/react-resize-detector#249) [maslianok/react-resize-detector#249](maslianok/react-resize-detector#249) [#247](#247)
## [1.1.7](https://github.com/equinor/webviz-subsurface-components/compare/[email protected]@1.1.7) (2024-02-28) ### Bug Fixes * bump react-resize-detector from 9.1.0 to 10.0.1 in /typescript ([#1935](#1935)) ([df5632d](df5632d)), closes [maslianok/react-resize-detector#249](maslianok/react-resize-detector#249) [maslianok/react-resize-detector#249](maslianok/react-resize-detector#249) [maslianok/react-resize-detector#249](maslianok/react-resize-detector#249) [maslianok/react-resize-detector#249](maslianok/react-resize-detector#249) [#247](#247)
Since 9.0.0, when
.current
is set, this does not trigger a rerender. This is because thesetRefElement
function is only called when the ref set is called as a function. Certain libraries will set.current
directly instead of calling the ref function, which React normally handles with a Ref, but the ref-like function in this hook introduced in 9.0.0 does not handle this case.This PR fixes that by using a proxy to properly call
setRefElement
when.current
is set.