Skip to content

Commit

Permalink
fix(ComboBox): switch from onInputValueChange to onStateChange
Browse files Browse the repository at this point in the history
Downshift `v1`'s 'onInputValueChange' seems to be called from React
state updater function and thus calling `setState()` in
`onInputValueChange` handler causes React's "An update was scheduled
from inside an update function" warning.

Fixes #2543.
Fixes #3637.
  • Loading branch information
Akira Sudoh committed Aug 5, 2019
1 parent 9a07ade commit 3417836
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions packages/react/src/components/ComboBox/ComboBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,22 +204,25 @@ export default class ComboBox extends React.Component {
event.stopPropagation();
};

handleOnInputValueChange = (inputValue, { setHighlightedIndex }) => {
const { onInputChange } = this.props;

setHighlightedIndex(findHighlightedIndex(this.props, inputValue));

this.setState(
() => ({
// Default to empty string if we have a false-y `inputValue`
inputValue: inputValue || '',
}),
() => {
if (onInputChange) {
onInputChange(inputValue);
handleOnStateChange = (newState, { setHighlightedIndex }) => {
if (Object.prototype.hasOwnProperty.call(newState, 'inputValue')) {
const { inputValue } = newState;
const { onInputChange } = this.props;

setHighlightedIndex(findHighlightedIndex(this.props, inputValue));

this.setState(
() => ({
// Default to empty string if we have a false-y `inputValue`
inputValue: inputValue || '',
}),
() => {
if (onInputChange) {
onInputChange(inputValue);
}
}
}
);
);
}
};

onToggleClick = isOpen => event => {
Expand Down Expand Up @@ -276,7 +279,7 @@ export default class ComboBox extends React.Component {
<Downshift
{...downshiftProps}
onChange={this.handleOnChange}
onInputValueChange={this.handleOnInputValueChange}
onStateChange={this.handleOnStateChange}
inputValue={this.state.inputValue || ''}
itemToString={itemToString}
defaultSelectedItem={initialSelectedItem}>
Expand Down

0 comments on commit 3417836

Please sign in to comment.