onChange?: (ValueType, ActionMeta) => void, is incompatible with OptionType
I constantly getting an error from flow that react-select. This is happened after updating to v2.4.2. This is clear that I am passing a right types to the handle change which expect an array with objects + OptionType is accepting any string [string]: any. Can any one explain why it is happening or this is potential bug in the react-select flow type?
error from flow
Error:Error:line (104)Cannot create `Select` element because array type [1] is incompatible with `OptionType` [2] in the first argument of property `onChange`.
Error:Error:line (104)Cannot create `Select` element because array type [1] is incompatible with null [2] in the first argument of property `onChange`.
Error:Error:line (104)Cannot create `Select` element because array type [1] is incompatible with undefined [2] in the first argument of property `onChange`.
component example
// @flow
import * as React from 'react';
import Select from 'react-select';
type LabelValueObject = Object & {
value: string,
label: string
}
type State = {
options: LabelValueObject[],
selectedOptions: LabelValueObject[],
}
export class ServiceDropdown extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
options: [],
selectedOptions: null,
};
}
handleChange = (selectedOptions: LabelValueObject[]): void => {
this.setState({ selectedOptions });
};
render() {
const { selectedOptions } = this.state;
return (
<>
<Select
isMulti
isSearchable
onChange={this.handleChange} <=== flow error
value={selectedOptions}
options={this.state.options}
/>
</>
);
}
}
export default ServiceDropdown;
+1
I am experiencing a similar error after an upgrade to v2.4.2
Cannot create Select element because:
- OptionType [1] is incompatible with OptionsType [2] in the first argument of property onChange.
- OptionType [1] is incompatible with null [2] in the first argument of property onChange.
- OptionType [1] is incompatible with undefined [2] in the first argument of property onChange.
My reduced use case:
import type { OptionType, ActionMeta } from from 'react-select/src/types.js'
onChange = (newValue: OptionType, actionMeta: ActionMeta) => {
this.setState({ value: [newValue] })
// etc.
}
<Select
value={value}
inputValue={inputValue}
options={options}
...
onChange={onChange}
...
/>
@gyto The error tells you clearly what you have to change. Just add the missing types in your error message to the type of the option:
type LabelValueObject = Object & {
value: string,
label: string
}
type SelectValue = LabelValueObject | LabelValueObject[] | null | undefined;
@hanna-kalosha With you the same. But if you are using the types provided by the library, then I recommend you use ValueType, which has the same structure as SelectValue in the example above.
import type { ValueType, ActionMeta } from 'react-select/src/types.js';
onChange = (newValue: ValueType, actionMeta: ActionMeta) => {};
Why is that? Because the onChange prop has to handle the selected option, a list of selected options in isMulti configuration, and the case where you deselect an option or all options in isMulti configuration (resulting in null or possibly undefined).
Hello -
In an effort to sustain the react-select project going forward, we're closing old issues.
We understand this might be inconvenient but in the best interest of supporting the broader community we have to direct our efforts towards the current major version.
If you aren't using the latest version of react-select please consider upgrading to see if it resolves any issues you're having.
However, if you feel this issue is still relevant and you'd like us to review it - please leave a comment and we'll do our best to get back to you!
handleChange = (selectedOptions: LabelValueObject[] | null): void => { this.setState({ selectedOptions }); };
you have to pass null value where you mention the types