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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 41 additions & 48 deletions ReactAndroid/src/androidTest/js/ScrollViewTestModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@

'use strict';

var BatchedBridge = require('BatchedBridge');
var React = require('React');
var createReactClass = require('create-react-class');
var View = require('View');
var ScrollView = require('ScrollView');
var Text = require('Text');
var StyleSheet = require('StyleSheet');
var TouchableWithoutFeedback = require('TouchableWithoutFeedback');
var ScrollListener = require('NativeModules').ScrollListener;

var NUM_ITEMS = 100;
const BatchedBridge = require('BatchedBridge');
const React = require('React');
const View = require('View');
const ScrollView = require('ScrollView');
const Text = require('Text');
const StyleSheet = require('StyleSheet');
const TouchableWithoutFeedback = require('TouchableWithoutFeedback');
const ScrollListener = require('NativeModules').ScrollListener;

const NUM_ITEMS = 100;

// Shared by integration tests for ScrollView and HorizontalScrollView

var scrollViewApp;
let scrollViewApp;

class Item extends React.Component {
render() {
Expand All @@ -37,7 +36,7 @@ class Item extends React.Component {
}
}

var getInitialState = function() {
const getInitialState = function() {
var data = [];
for (var i = 0; i < NUM_ITEMS; i++) {
data[i] = {text: 'Item ' + i + '!'};
Expand All @@ -47,90 +46,84 @@ var getInitialState = function() {
};
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();

};

var onScroll = function(e) {
const onScroll = function(e) {
ScrollListener.onScroll(
e.nativeEvent.contentOffset.x,
e.nativeEvent.contentOffset.y,
);
};

var onScrollBeginDrag = function(e) {
const onScrollBeginDrag = function(e) {
ScrollListener.onScrollBeginDrag(
e.nativeEvent.contentOffset.x,
e.nativeEvent.contentOffset.y,
);
};

var onScrollEndDrag = function(e) {
const onScrollEndDrag = function(e) {
ScrollListener.onScrollEndDrag(
e.nativeEvent.contentOffset.x,
e.nativeEvent.contentOffset.y,
);
};

var onItemPress = function(itemNumber) {
const onItemPress = function(itemNumber) {
ScrollListener.onItemPress(itemNumber);
};

var ScrollViewTestApp = createReactClass({
displayName: 'ScrollViewTestApp',
getInitialState: getInitialState,
onScroll: onScroll,
onItemPress: onItemPress,
onScrollBeginDrag: onScrollBeginDrag,
onScrollEndDrag: onScrollEndDrag,
class ScrollViewTestApp extends React.Component {
scrollView = React.createRef();
state = getInitialState();

scrollTo: function(destX, destY) {
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.

};

render: function() {
render() {
scrollViewApp = this;
var children = this.state.data.map((item, index) => (
<Item
key={index}
text={item.text}
onPress={this.onItemPress.bind(this, index)}
onPress={onItemPress.bind(this, index)}
/>
));
return (
<ScrollView
onScroll={this.onScroll}
onScrollBeginDrag={this.onScrollBeginDrag}
onScrollEndDrag={this.onScrollEndDrag}
ref="scrollView">
onScroll={onScroll}
onScrollBeginDrag={onScrollBeginDrag}
onScrollEndDrag={onScrollEndDrag}
ref={this.scrollView}>
{children}
</ScrollView>
);
},
});
}
}

var HorizontalScrollViewTestApp = createReactClass({
displayName: 'HorizontalScrollViewTestApp',
getInitialState: getInitialState,
onScroll: onScroll,
onItemPress: onItemPress,
class HorizontalScrollViewTestApp extends React.Component {
scrollView = React.createRef();
state = getInitialState();

scrollTo: function(destX, destY) {
this.refs.scrollView.scrollTo(destY, destX);
},
scrollTo = (destX, destY) => {
this.scrollView.scrollTo(destY, destX);
};

render: function() {
render() {
scrollViewApp = this;
var children = this.state.data.map((item, index) => (
<Item
key={index}
text={item.text}
onPress={this.onItemPress.bind(this, index)}
onPress={onItemPress.bind(this, index)}
/>
));
return (
<ScrollView horizontal={true} onScroll={this.onScroll} ref="scrollView">
<ScrollView horizontal={true} onScroll={onScroll} ref={this.scrollView}>
{children}
</ScrollView>
);
},
});
}
}

var styles = StyleSheet.create({
item_container: {
Expand Down