Skip to content

Commit f496b65

Browse files
Deckstarlfkwtz
andauthored
Replaced getDerivedStateFromProps with componentDidUpdate to prevent onValueChanged being called twice; (lawnstarter#368)
Co-authored-by: Michael Lefkowitz <[email protected]>
1 parent c49085f commit f496b65

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/index.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,35 +110,31 @@ export default class RNPickerSelect extends PureComponent {
110110
};
111111
}
112112

113-
static getDerivedStateFromProps(nextProps, prevState) {
113+
componentDidUpdate = (prevProps, prevState) => {
114114
// update items if items or placeholder prop changes
115115
const items = RNPickerSelect.handlePlaceholder({
116-
placeholder: nextProps.placeholder,
117-
}).concat(nextProps.items);
116+
placeholder: this.props.placeholder,
117+
}).concat(this.props.items);
118118
const itemsChanged = !isEqual(prevState.items, items);
119119

120120
// update selectedItem if value prop is defined and differs from currently selected item
121121
const { selectedItem, idx } = RNPickerSelect.getSelectedItem({
122122
items,
123-
key: nextProps.itemKey,
124-
value: nextProps.value,
123+
key: this.props.itemKey,
124+
value: this.props.value,
125125
});
126126
const selectedItemChanged =
127-
!isEqual(nextProps.value, undefined) && !isEqual(prevState.selectedItem, selectedItem);
127+
!isEqual(this.props.value, undefined) && !isEqual(prevState.selectedItem, selectedItem);
128128

129129
if (itemsChanged || selectedItemChanged) {
130-
if (selectedItemChanged) {
131-
nextProps.onValueChange(selectedItem.value, idx);
132-
}
130+
this.props.onValueChange(selectedItem.value, idx);
133131

134-
return {
132+
this.setState({
135133
...(itemsChanged ? { items } : {}),
136134
...(selectedItemChanged ? { selectedItem } : {}),
137-
};
135+
});
138136
}
139-
140-
return null;
141-
}
137+
};
142138

143139
constructor(props) {
144140
super(props);

0 commit comments

Comments
 (0)