diff --git a/src/NativePicker.android.tsx b/src/NativePicker.android.tsx index a984b18..265331f 100644 --- a/src/NativePicker.android.tsx +++ b/src/NativePicker.android.tsx @@ -44,38 +44,49 @@ class Picker extends React.Component { contentRef: any; indicatorRef: any; - onItemLayout = (e) => { + onItemLayout = e => { const { height, width } = e.nativeEvent.layout; // console.log('onItemLayout', height); if (this.itemHeight !== height || this.itemWidth !== width) { this.itemWidth = width; - this.indicatorRef.setNativeProps({ - style: [ - styles.indicator, - { - top: height * 3, - height, - width, - }, - ], - }); + if (this.indicatorRef) { + this.indicatorRef.setNativeProps({ + style: [ + styles.indicator, + { + top: height * 3, + height, + width, + }, + ], + }); + } } if (this.itemHeight !== height) { this.itemHeight = height; - this.scrollerRef.setNativeProps({ - style: { - height: height * 7, - }, - }); - this.contentRef.setNativeProps({ - style: { - paddingTop: height * 3, - paddingBottom: height * 3, - }, - }); + if (this.scrollerRef) { + this.scrollerRef.setNativeProps({ + style: { + height: height * 7, + }, + }); + } + if (this.contentRef) { + this.contentRef.setNativeProps({ + style: { + paddingTop: height * 3, + paddingBottom: height * 3, + }, + }); + } + // i do no know why!... setTimeout(() => { - this.props.select(this.props.selectedValue, this.itemHeight, this.scrollTo); + this.props.select( + this.props.selectedValue, + this.itemHeight, + this.scrollTo, + ); }, 0); } } @@ -94,20 +105,25 @@ class Picker extends React.Component { } } - scrollTo = (y) => { - this.scrollerRef.scrollTo({ - y, - animated: false, - }); + scrollTo = y => { + if (this.scrollerRef) { + this.scrollerRef.scrollTo({ + y, + animated: false, + }); + } } - fireValueChange = (selectedValue) => { - if (this.props.selectedValue !== selectedValue && this.props.onValueChange) { + fireValueChange = selectedValue => { + if ( + this.props.selectedValue !== selectedValue && + this.props.onValueChange + ) { this.props.onValueChange(selectedValue); } } - onScroll = (e) => { + onScroll = e => { const { y } = e.nativeEvent.contentOffset; this.clearScrollBuffer(); this.scrollBuffer = setTimeout(() => { @@ -126,11 +142,13 @@ class Picker extends React.Component { totalStyle.push(itemStyle); return ( this[`item${index}`] = el} + ref={el => (this[`item${index}`] = el)} onLayout={index === 0 ? this.onItemLayout : undefined} key={item.key} > - {item.props.label} + + {item.props.label} + ); }); @@ -138,17 +156,15 @@ class Picker extends React.Component { this.scrollerRef = el} + ref={el => (this.scrollerRef = el)} onScroll={this.onScroll} scrollEventThrottle={16} showsVerticalScrollIndicator={false} overScrollMode="never" > - this.contentRef = el}> - {items} - + (this.contentRef = el)}>{items} - this.indicatorRef = el} style={styles.indicator}/> + (this.indicatorRef = el)} style={styles.indicator} /> ); }