-
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
[NativeMethodsMixin] CheckBox: Convert NativeMethodsMixin to forwardedRef, convert to class #21585
Conversation
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.
Code analysis results:
eslint
found some issues.
const React = require('React'); | ||
const ReactNative = require('ReactNative'); |
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.
no-unused-vars: 'ReactNative' is assigned a value but never used.
6001e2b
to
44a48dd
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.
Code analysis results:
eslint
found some issues.
const React = require('React'); | ||
const ReactNative = require('ReactNative'); |
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.
no-unused-vars: 'ReactNative' is assigned a value but never used.
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.
Code analysis results:
eslint
found some issues.
const React = require('React'); | ||
const ReactNative = require('ReactNative'); |
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.
no-unused-vars: 'ReactNative' is assigned a value but never used.
|}>; | ||
|
||
type NativeProps = $ReadOnly<{| | ||
...Props, |
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 is true. The native component doesn't take disabled
or value
for example.
/** | ||
* Used to get the ref for the native checkbox | ||
*/ | ||
ref?: ?Object, |
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 we have a type for this yet in Flow but I'd rather it be $FlowIssue
here than Object.
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 mostly good, but I'm not sure if the ref forwarding is set up correctly.
* Used to get the ref for the native checkbox | ||
*/ | ||
// $FlowFixMe: Properly type the ref prop type | ||
ref?: $FlowIssue, |
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.
Hmm... ref
like key
isn't a prop, so my first instinct would be to omit it from the flow CommonProps
type. Do we really need this here?
}; | ||
|
||
_setAndForwardRef = nativeRef => { | ||
const {ref} = this.props; |
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.
As far as I'm aware, this won't work because ref
isn't a prop (i.e: this.props.ref
will be undefined
). What we really need is something like this:
class CheckBox extends React.Component<Props> {
_setAndForwardRef = nativeRef => {
const {forwardedRef} = this.props;
// ...
}
}
CheckBox2 = React.forwardRef((props, ref) => <CheckBox {...props} forwardedRef={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.
Yeah, it has to be passed in as a prop of a different name than ref (like forwardedRef)
@empyrical, you did a bunch of work to validate that this ref set and forwarding work. Do you think you could add some unit tests for this component that check that behavior? It would be super valuable to set a good example for other components we will want to be updating as well. |
Do you think a new section to the RNTester page that uses its ref would cover it? (like a I have a PR coming up that includes a better way to handle these forwarded refs in classes, and would like to rebase this and address the code review points once it makes its way in. (Will also rename the ref prop to The react-native/Libraries/Components/RefreshControl/RefreshControl.js Lines 159 to 161 in 95174d4
And I didn't do |
I think you can test these things using a Jest test which would be run fast and in an automated way. I think this would be better here than an RNTester example. |
40cd9b1
to
70d3553
Compare
Rebased this and changed it to properly forward refs using |
}, | ||
class CheckBox extends React.Component<Props> { | ||
_nativeRef: ?React.ElementRef<CheckBoxNativeType> = null; | ||
_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.
We should flow type setAndForwardRef
to ensure that getForwardedRef
and setLocalRef
operate on the same ref type.
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.
Should that be done in this PR, or a separate PR? (in case this one needs to be reverted)
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.
Separate is best. I just noticed as part of some weirdness in how it is being used here.
96a4b94
to
61d3aca
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.
Looks good! 😁
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.
RSNara has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
@empyrical merged commit 28de61e into |
facebook#21585) Summary: This PR converts the use of `NativeMethodsMixin` in `CheckBox.android.js`, and converts it to an ES6-style class. Pull Request resolved: facebook#21585 Reviewed By: TheSavior Differential Revision: D12827768 Pulled By: RSNara fbshipit-source-id: c113c221335e61e015a20bbb6bcff5f9837f9022
This PR converts the use of
NativeMethodsMixin
inCheckBox.android.js
, and converts it to an ES6-style class.Test Plan:
I have tested it in RNTester, and I ran in to no issues.
Release Notes:
[GENERAL] [ENHANCEMENT] [Libraries/Components/CheckBox/CheckBox.android.js] - Convert NativeMethodsMixin to forwardedRef, converted to ES6 class