Call inherited host node methods on the host node in strict refs - #515
Open
adityasingh2400 wants to merge 1 commit into
Open
Call inherited host node methods on the host node in strict refs#515adityasingh2400 wants to merge 1 commit into
adityasingh2400 wants to merge 1 commit into
Conversation
The strict ref wrapper uses Object.create(node), so methods that live on the host node prototype chain are reached through the wrapper but invoked with the wrapper as the receiver. RN host node methods read internals off this, which the wrapper does not carry, so ref.focus() and ref.blur() stop working on native. Re-expose inherited methods as own properties that call through to the host node. Own methods of the node are untouched because RN assigns those as closures over the node.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #506.
getOrCreateStrictRefbuilds the strict ref withObject.create(node), so a method that lives on the host node prototype chain is still reachable from the wrapper but gets invoked with the wrapper as its receiver. React Native host node methods read internals offthis, and the wrapper does not carry those internals, soref.current.focus()andref.current.blur()silently do nothing on native whilegetNativeRef().focus()still works. Reads keep working because own properties of the node resolve through the prototype chain, which is why only the imperative methods broke.This walks the node's prototype chain once per node and re-exposes each inherited method as an own property that calls through to the node, so the receiver stays correct. Own methods of the node are left alone since React Native assigns those as closures over the node, and accessors are left on the prototype chain since they read plain instance fields the wrapper already inherits. The binding runs before the strict-dom overrides are defined so the viewport-scaled
getBoundingClientRectstill wins.Added a regression test in
html-refs-test.native.jsthat mounts anhtml.inputover a host node mock whosefocusandblurlive on the prototype, then asserts the receiver is the host node. It fails on main and passes with this change. Ran the full package suite (19 suites, 897 tests),flow check, eslint, and prettier, all clean.