-
Notifications
You must be signed in to change notification settings - Fork 24.5k
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 Switch ref forwarding #31629
Fix Switch ref forwarding #31629
Conversation
Base commit: 8ca18f0 |
This also breaks the integration tests in react-native-windows. |
@@ -147,6 +154,13 @@ export default function Switch(props: Props): React.Node { | |||
const nativeSwitchRef = React.useRef<?React.ElementRef< | |||
typeof SwitchNativeComponent | typeof AndroidSwitchNativeComponent, | |||
>>(null); | |||
const _setNativeRef = setAndForwardRef({ |
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.
Can this just be useImperativeHandle(nativeSwitchRef)?
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 don't think this would work since nativeSwitchRef is just the internal ref used by this component. We need to set the ref to both nativeSwitchRef and forwarded ref, which is what setAndForwardRef was made for if I understand correctly.
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.
Whoops, my api usage is wrong.
I think this
useImperativeHandle(forwardedRef, () => nativeSwitchRef, [nativeSwitchRef])
setAndForwardRef is needed if the component attaches a bunch of other methods to the ref too. But that doesn't seem to be the case here
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.
Interesting, haven't seen this pattern before, but yea it would work. Seems like it is used here also
useImperativeHandle(forwardedRef, () => viewRef.current); |
@lunaleaps has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
@lunaleaps merged this pull request in 1538fa4. |
Summary: Since moving Switch to a function component it is no longer possible to get the native switch ref. This adds forwardRef so it is possible again. ## Changelog [General] [Fixed] - Fix Switch ref forwarding Pull Request resolved: facebook#31629 Test Plan: Tested the change in an app using react-native-gesture-handler, which tries to set a ref on Switch. Also made sure flow still passes. Reviewed By: TheSavior Differential Revision: D28839605 Pulled By: lunaleaps fbshipit-source-id: 1fee86145caeabb60c0010bb9062dddca419f7ca
Summary
Since moving Switch to a function component it is no longer possible to get the native switch ref. This adds forwardRef so it is possible again.
Changelog
[General] [Fixed] - Fix Switch ref forwarding
Test Plan
Tested the change in an app using react-native-gesture-handler, which tries to set a ref on Switch. Also made sure flow still passes.