-
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
[createReactClass] remove createReactClass from ScrollViewTestModule.js #21627
[createReactClass] remove createReactClass from ScrollViewTestModule.js #21627
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.
The PR looks mostly good, but I think we should change a few things. 🤔
class ScrollViewTestApp extends React.Component { | ||
constructor() { | ||
super(); | ||
this.scrollView = React.createRef(); |
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.
nit: We should just use class properties for this.
class ScrollViewTestApp extends React.Component {
scrollView = React.createRef();
//...
This way, we won't have to implement our own constructor.
data[i] = {text: 'Item ' + i + '!'}; | ||
class ScrollViewTestApp extends React.Component { | ||
constructor() { | ||
super(); |
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.
React components are instantiated with arguments (the first of which is just props
). To avoid potential problems, we should just forward them all to React.Component
constructor.
class ScrollViewTestApp extends React.Component {
constructor(...args) {
super(...args);
// ...
}
// ...
}
return { | ||
data: data, | ||
}; | ||
}; |
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.
Classes that extend React.Component
do not call getInitialState
to get their initial state. With the current configuration, this.state
will be null
inside the render()
method, which will raise an exception given the current implementation of that method. Instead of implementing getInitialState
, you should create the state and assign it to this.state
inside the constructor. Since we support class property assignment, we should just do this:
class ScrollViewTestApp extends React.Component {
scrollView = React.createRef();
state = getInitialState();
e.nativeEvent.contentOffset.x, | ||
e.nativeEvent.contentOffset.y, | ||
); | ||
}; |
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.
All functions need to meet two conditions before you should consider binding them (i.e: using this syntax):
- You're using
this
inside the function. - You're passing the function by reference to component's prop or as a callback to some function.
In this case, since we're not using this
inside onScroll
, we don't actually need to bind this function. In fact, since we're not using this
, we could leave onScroll
as is (i.e: Implemented outside the ScrollViewTestApp
class).
Let's just move this back to where it was before.
}; | ||
onItemPress = itemNumber => { | ||
ScrollListener.onItemPress(itemNumber); | ||
}; |
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.
Let's move this back to where it was before.
@@ -98,24 +95,45 @@ var ScrollViewTestApp = createReactClass({ | |||
onScroll={this.onScroll} | |||
onScrollBeginDrag={this.onScrollBeginDrag} | |||
onScrollEndDrag={this.onScrollEndDrag} |
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 there's a reason for these functions to be attached to the component instance. So, after we move the functions out of the ScrollViewTestApp
class, lets change this to:
<Item
// ...
onScroll={onScroll}
onScrollBeginDrag={onScrollBeginDrag}
onScrollEndDrag={onScrollEndDrag}
// ...
constructor() { | ||
super(); | ||
this.scrollView = React.createRef(); | ||
} |
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.
class HorizontalScrollViewTestApp extends React.Component {
scrollView = React.createRef();
e.nativeEvent.contentOffset.x, | ||
e.nativeEvent.contentOffset.y, | ||
); | ||
}; |
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.
There's no need to duplicate this function.
return { | ||
data: data, | ||
}; | ||
}; |
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.
class HorizontalScrollViewTestApp extends React.Component {
scrollView = React.createRef();
state = getInitialState()
<ScrollView horizontal={true} onScroll={this.onScroll} ref="scrollView"> | ||
<ScrollView | ||
horizontal={true} | ||
onScroll={this.onScroll} |
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.
<ScrollView
horizontal={true}
onScroll={onScroll}
This file should also be flow-typed. But it's up to you if you want to tackle that here, or if you want to tackle that at all. 😛 |
this.refs.scrollView.scrollTo(destY, destX); | ||
}, | ||
scrollTo = (destX, destY) => { | ||
this.scrollView.scrollTo(destY, destX); |
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 should have caught this earlier, but when you create a ref using React.createRef
, you have to access what it's referring to using ref.current
. So, this should be something like:
const { scrollView: { current: scrollView } } = this;
if (scrollView == null) {
return;
}
scrollView.scrollTo(destY, destX);
But don't worry about it. 😄 I'll make the fix on my end.
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! Thanks for making all those changes. 😁
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.
@JenLindsay merged commit bbfff90 into |
Summary: Related to issue facebook#21581 Removed createReactClass from ReactAndroid/src/androidTest/js/ScrollViewTestModule.js Pull Request resolved: facebook#21627 Reviewed By: TheSavior Differential Revision: D10363828 Pulled By: RSNara fbshipit-source-id: 08c9776060cfdfde3b69f310bca0353d393b67c4
Related to issue #21581
Removed createReactClass from ReactAndroid/src/androidTest/js/ScrollViewTestModule.js
Test Plan:
Release Notes:
[GENERAL] [ENHANCEMENT] [ScrollViewTestModule.js] - rm createReactClass