Make react-select 2.0.0 <Async> work with redux-form <Field>

Viewed 985

react-select just upgraded to 2.0.0 so google results on the first three pages are all about older versions, even the official document, and none of them helped.

My select box can show all options correctly, but redux form won't pick up the value, with the warning: Warning: A component is changing a controlled input of type hidden to be uncontrolled.

I wonder what have I missed here...

Form component:

<Field
  name="residentialAddress"
  label = "Residential Address"
  type="select"
  component={AddressField}
  validate={required}
/>

Component

export class AddressField extends Component {

    searchAddress = input => {
        let options = []

        return myPromise(input)
        .then(suggestions => {
                options = suggestions.map(suggestion => 
                    ({
                        label: suggestion.label,
                        data: suggestion.value
                    })
                )
                return options;             
            }
        ).catch(
            error => { 
                return options = [{ label: "Auto fetching failed, please enter your address manually", value: "", isDisabled: true }];
            }
        );
    };

    render() {
        const {
            input,
            label,
            meta: { touched, error },
            type
        } = this.props;

        return(
            <FormGroup>
                <ControlLabel>{label}</ControlLabel>
                <Async
                    {...input}
                    placeholder={label}
                    isClearable={true}   
                    getOptionValue={(option) => option.residentialAddress}
                    onChange = { value => input.onChange(value.data) }
                    loadOptions={this.searchAddress}                   
                />
                { touched && error && <span>{error}</span> }
            </FormGroup>
        )
    }
1 Answers

Solution: Simply remove the {...input} in <Async>.

Unlike regular custom Field component where we need to pass in {input}, the react-select Async component seems to take care of itself very well and doesn't require any intervene. Someone may explain it in a more professional way perhaps...

Also worth mention for those who come across this question: loadOptions with promise used to require object {options: options} as return type. Now it changes to just array (options as in my code). But I didn't find any document that mentions this one.

Hope this could help.

Related