Uh oh!
There was an error while loading. Please reload this page.
fix(switch): forward ref to the root view - #5065
Open
giaBaoJS wants to merge 1 commit into
Open
Conversation
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 freeto 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.
Motivation
Switchaccepts noref.Propsdoes not declare one, the component signature never destructures one, and the root<View style={[styles.wrapper, style]}>is not given one — soref.currentstaysnulland there is no way tomeasure()or otherwise reach the switch's host view.This looks like a leftover from the Reanimated/
Pressablerewrite: the surrounding components carry the prop over correctly (Appbar/AppbarBackAction.tsx,FAB/Extended.tsx,Surface.tsx,List/ListItem.tsx,TouchableRipple),Switchjust did not.Scope / correction to the issue
#3729 names both
AvatarandSwitch. I checkedAvatarand it is not affected:AvatarText,AvatarIconandAvatarImageall spread...restonto their rootView, so arefpassed to them does land on the host view (I verifiedref.currentis non-null for all three). TheirPropstypes just do not advertiseref, which is a typing gap rather than a runtime bug — happy to add the declarations in a follow-up if you want.So this PR only fixes
Switch. It does not close#3729 on its own, since that issue asks for the whole surface. One thing I did notice while looking:Banner.tsxdeclaresref?: React.RefObject<View>in itsPropsbut never uses the value, soBannerdrops refs too — again, happy to do that separately.Note on the ref type
Peers use
ref?: React.Ref<View>, but those all forward to a component whose props already carryReact.RefAttributes<View>(Animated.View,Pressable,TouchableRipple).Switch's root is a plain<View>, and putting aReact.Ref<View>on it failsyarn typecheck:tsc -balso buildsdocs/, which has its own copy of@types/react(same version, separate directory), and theunique symbolbehindVoidOrUndefinedOnlyin the callback-ref signature does not unify across the two copies.React.RefObject<View | null>is structural and unaffected, matches whatReact.createRef<View>()anduseRef<View>(null)produce, and follows the existing declaration inBanner.tsx. Glad to switch it toReact.Ref<View>if you'd rather fix the duplicate-types setup instead.Test plan
Two cases added to
src/components/__tests__/Switch.test.tsx:ref.currentis notnullref.current.measureis a function (i.e. it is the host view, not some other object)Both fail before the change (
Received: null/Received: "undefined") and pass after.yarn test: 55 suites, 737 passed / 1 skipped, 169 snapshots — no snapshot churn.yarn lintandyarn typecheckclean.Refs #3729.