From f995108f746b37f7d52a745a596b61569e1e767c Mon Sep 17 00:00:00 2001 From: mikaello Date: Mon, 5 Feb 2018 08:37:54 +0100 Subject: [PATCH] Use arrow instead of bind (#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 --- BaseComponent.js | 9 --------- index.js | 44 +++++++++++++++----------------------------- 2 files changed, 15 insertions(+), 38 deletions(-) delete mode 100644 BaseComponent.js diff --git a/BaseComponent.js b/BaseComponent.js deleted file mode 100644 index 5b581100..00000000 --- a/BaseComponent.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict'; - -import React from 'react'; - -export default class BaseComponent extends React.Component { - _bind(...methods) { - methods.forEach(method => this[method] = this[method].bind(this)); - } -} diff --git a/index.js b/index.js index 143af121..5ecf0aba 100644 --- a/index.js +++ b/index.js @@ -15,7 +15,6 @@ import { } from 'react-native'; import styles from './style'; -import BaseComponent from './BaseComponent'; const ViewPropTypes = RNViewPropTypes || View.propTypes; @@ -40,7 +39,7 @@ const propTypes = { overlayStyle: ViewPropTypes.style, cancelText: PropTypes.string, disabled: PropTypes.bool, - supportedOrientations: PropTypes.arrayOf(PropTypes.oneOf(['portrait', 'landscape', 'portrait-upside-down', 'landscape-left', 'landscape-right'])), + supportedOrientations: Modal.propTypes.supportedOrientations, keyboardShouldPersistTaps: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), backdropPressToClose: PropTypes.bool, }; @@ -69,39 +68,26 @@ const defaultProps = { backdropPressToClose: false, }; -export default class ModalSelector extends BaseComponent { +export default class ModalSelector extends React.Component { - constructor() { - - super(); - - this._bind( - 'onChange', - 'open', - 'close', - 'renderChildren' - ); + constructor(props) { + super(props); this.state = { - modalVisible: false, - transparent: false, - selected: 'please select', - changedItem: undefined, + modalVisible: false, + selected: props.initValue, + cancelText: props.cancelText, + changedItem: undefined, }; } - componentDidMount() { - this.setState({selected: this.props.initValue}); - this.setState({cancelText: this.props.cancelText}); - } - componentWillReceiveProps(nextProps) { if (nextProps.initValue !== this.props.initValue) { this.setState({selected: nextProps.initValue}); } } - onChange(item) { + onChange = (item) => { if (Platform.OS === 'android' || !Modal.propTypes.onDismiss) { // RN >= 0.50 on iOS comes with the onDismiss prop for Modal which solves RN issue #10471 this.props.onChange(item); @@ -110,20 +96,20 @@ export default class ModalSelector extends BaseComponent { this.close(); } - close() { + close = () => { this.setState({ modalVisible: false, }); } - open() { + open = () => { this.setState({ modalVisible: true, changedItem: undefined, }); } - renderSection(section) { + renderSection = (section) => { return ( {section.label} @@ -131,7 +117,7 @@ export default class ModalSelector extends BaseComponent { ); } - renderOption(option, isLastItem) { + renderOption = (option, isLastItem) => { return ( this.onChange(option)}> ); } - renderOptionList() { + renderOptionList = () => { let options = this.props.data.map((item, index) => { if (item.section) { @@ -175,7 +161,7 @@ export default class ModalSelector extends BaseComponent { ); } - renderChildren() { + renderChildren = () => { if(this.props.children) { return this.props.children;