Skip to content
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

Closed
wants to merge 4 commits into from
Closed

[createReactClass] remove createReactClass from ScrollViewTestModule.js #21627

wants to merge 4 commits into from

Conversation

sopranolinist
Copy link

Related to issue #21581
Removed createReactClass from ReactAndroid/src/androidTest/js/ScrollViewTestModule.js

Test Plan:

  • npm run prettier
  • npm run flow-check-ios
  • npm run flow-check-android

Release Notes:

[GENERAL] [ENHANCEMENT] [ScrollViewTestModule.js] - rm createReactClass

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 10, 2018
@sopranolinist sopranolinist changed the title removed createReactClass from ScrollViewTestModule.js [createReactClass] remove createReactClass from ScrollViewTestModule.js Oct 10, 2018
Copy link
Contributor

@RSNara RSNara left a 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();
Copy link
Contributor

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();
Copy link
Contributor

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,
};
};
Copy link
Contributor

@RSNara RSNara Oct 10, 2018

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,
);
};
Copy link
Contributor

@RSNara RSNara Oct 10, 2018

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):

  1. You're using this inside the function.
  2. 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);
};
Copy link
Contributor

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}
Copy link
Contributor

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();
}
Copy link
Contributor

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,
);
};
Copy link
Contributor

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,
};
};
Copy link
Contributor

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}
Copy link
Contributor

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}

@RSNara
Copy link
Contributor

RSNara commented Oct 10, 2018

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);
Copy link
Contributor

@RSNara RSNara Oct 12, 2018

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.

Copy link
Contributor

@RSNara RSNara left a 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. 😁

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a 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.

@react-native-bot
Copy link
Collaborator

@JenLindsay merged commit bbfff90 into facebook:master.

@facebook facebook locked as resolved and limited conversation to collaborators Oct 16, 2018
@react-native-bot react-native-bot added the Merged This PR has been merged. label Oct 16, 2018
t-nanava pushed a commit to microsoft/react-native-macos that referenced this pull request Jun 17, 2019
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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants