react-select icon indicating copy to clipboard operation
react-select copied to clipboard

onChange?: (ValueType, ActionMeta) => void, is incompatible with OptionType

Open gyto opened this issue 7 years ago • 4 comments

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;

gyto avatar Apr 17 '19 16:04 gyto

+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}
        ...
      />

hanna-kalosha avatar May 27 '19 22:05 hanna-kalosha

@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).

Rall3n avatar May 28 '19 06:05 Rall3n

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!

bladey avatar May 28 '20 03:05 bladey

handleChange = (selectedOptions: LabelValueObject[] | null): void => { this.setState({ selectedOptions }); };

you have to pass null value where you mention the types

alitunaarel avatar Mar 10 '22 12:03 alitunaarel