Skip to content

Commit f995108

Browse files
author
mikaello
authored
Use arrow instead of bind (d-a-n#40)
* Use arrow functions instead of manually bind methods * Remove unused state variable * Use proptypes from Modal-library instead of creating own ones * Set initial state in constructor, not in componentDidMount
1 parent 1eacde0 commit f995108

File tree

2 files changed

+15
-38
lines changed

2 files changed

+15
-38
lines changed

BaseComponent.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

index.js

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
} from 'react-native';
1616

1717
import styles from './style';
18-
import BaseComponent from './BaseComponent';
1918

2019
const ViewPropTypes = RNViewPropTypes || View.propTypes;
2120

@@ -40,7 +39,7 @@ const propTypes = {
4039
overlayStyle: ViewPropTypes.style,
4140
cancelText: PropTypes.string,
4241
disabled: PropTypes.bool,
43-
supportedOrientations: PropTypes.arrayOf(PropTypes.oneOf(['portrait', 'landscape', 'portrait-upside-down', 'landscape-left', 'landscape-right'])),
42+
supportedOrientations: Modal.propTypes.supportedOrientations,
4443
keyboardShouldPersistTaps: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
4544
backdropPressToClose: PropTypes.bool,
4645
};
@@ -69,39 +68,26 @@ const defaultProps = {
6968
backdropPressToClose: false,
7069
};
7170

72-
export default class ModalSelector extends BaseComponent {
71+
export default class ModalSelector extends React.Component {
7372

74-
constructor() {
75-
76-
super();
77-
78-
this._bind(
79-
'onChange',
80-
'open',
81-
'close',
82-
'renderChildren'
83-
);
73+
constructor(props) {
74+
super(props);
8475

8576
this.state = {
86-
modalVisible: false,
87-
transparent: false,
88-
selected: 'please select',
89-
changedItem: undefined,
77+
modalVisible: false,
78+
selected: props.initValue,
79+
cancelText: props.cancelText,
80+
changedItem: undefined,
9081
};
9182
}
9283

93-
componentDidMount() {
94-
this.setState({selected: this.props.initValue});
95-
this.setState({cancelText: this.props.cancelText});
96-
}
97-
9884
componentWillReceiveProps(nextProps) {
9985
if (nextProps.initValue !== this.props.initValue) {
10086
this.setState({selected: nextProps.initValue});
10187
}
10288
}
10389

104-
onChange(item) {
90+
onChange = (item) => {
10591
if (Platform.OS === 'android' || !Modal.propTypes.onDismiss) {
10692
// RN >= 0.50 on iOS comes with the onDismiss prop for Modal which solves RN issue #10471
10793
this.props.onChange(item);
@@ -110,28 +96,28 @@ export default class ModalSelector extends BaseComponent {
11096
this.close();
11197
}
11298

113-
close() {
99+
close = () => {
114100
this.setState({
115101
modalVisible: false,
116102
});
117103
}
118104

119-
open() {
105+
open = () => {
120106
this.setState({
121107
modalVisible: true,
122108
changedItem: undefined,
123109
});
124110
}
125111

126-
renderSection(section) {
112+
renderSection = (section) => {
127113
return (
128114
<View key={section.key} style={[styles.sectionStyle,this.props.sectionStyle]}>
129115
<Text style={[styles.sectionTextStyle,this.props.sectionTextStyle]}>{section.label}</Text>
130116
</View>
131117
);
132118
}
133119

134-
renderOption(option, isLastItem) {
120+
renderOption = (option, isLastItem) => {
135121
return (
136122
<TouchableOpacity key={option.key} onPress={() => this.onChange(option)}>
137123
<View style={[styles.optionStyle, this.props.optionStyle, isLastItem &&
@@ -141,7 +127,7 @@ export default class ModalSelector extends BaseComponent {
141127
</TouchableOpacity>);
142128
}
143129

144-
renderOptionList() {
130+
renderOptionList = () => {
145131

146132
let options = this.props.data.map((item, index) => {
147133
if (item.section) {
@@ -175,7 +161,7 @@ export default class ModalSelector extends BaseComponent {
175161
</TouchableWithoutFeedback>);
176162
}
177163

178-
renderChildren() {
164+
renderChildren = () => {
179165

180166
if(this.props.children) {
181167
return this.props.children;

0 commit comments

Comments
 (0)