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

Fix ref .current set by using a proxy #249

Merged
merged 3 commits into from
Jan 4, 2024

Conversation

hugo-vrijswijk
Copy link
Contributor

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.

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.
Copy link
Collaborator

@snelsi snelsi left a 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

}
},

const onRefChange: OnRefChangeType<T> = useMemo(
Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, fixed

get(target, prop) {
if (prop === 'current') {
return refElement;
} else {
Copy link
Collaborator

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

Copy link
Contributor Author

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

Copy link
Collaborator

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 😅

Copy link
Contributor Author

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

@snelsi snelsi merged commit 5996935 into maslianok:master Jan 4, 2024
@snelsi
Copy link
Collaborator

snelsi commented Jan 4, 2024

Published in v10.0.0-beta.1 and v9.1.2-beta.0

@hugo-vrijswijk hugo-vrijswijk deleted the fix-current-set branch January 4, 2024 16:06
@maslianok
Copy link
Owner

Nice! 🔥

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