Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set defaultValue for react-select Async and not reset when onBlur at v2.0.0?

I try to apply react-select to create async selection like the demo, And want to add default value for the input.
As below, the default value will be clear when onBlur because onBlur will default trigger handleInputChange event with empty value.
I guess this might case by some default setting like 'onBlurResetsInput', but 'onBlurResetsInput' props has be removed at v2.0.0. So want to know how to prevent onBlur reset, thanks.

import Async from 'react-select/lib/Async';
export class AsyncSelect extends React.PureComponent {
  state = {
    inputValue: this.props.defaultValue,
  }

  handleInputChange = (value) => {
      this.setState({
        inputValue: value,
      });
      this.props.syncOptions(value);
  }

  render() {
    return (
        <Async
          inputValue={this.state.inputValue}
          onInputChange={this.handleInputChange}
          options={this.props.options}
          onChange={(option) => this.props.selectOption(option.value)}
        />
    );
  }
}
like image 937
Andrew - oahehc Avatar asked May 09 '18 03:05

Andrew - oahehc


People also ask

How do you make an option selected by default in React?

To select a default option in React, the selected attribute is used in the option element. In React, though, instead of using the selected attribute, the value prop is used on the root select element. So, you can set a default value by passing the value of the option in the value prop of the select input element.

How do I change the placeholder in React select?

Set the first option element of the select tag to disabled and give it an empty string value. Initialize the state for the select tag to an empty string.

How do you clear the selected value in React select?

You can clear the value of react select using the ref. Just store the value in the state, and change the state programmatically using componentDidUpdate etc... Note: 'value' should be an object. A simple option would be to pass null to the value prop.

How do I clear default value in React?

The solution is to use the reset() function from the React Hook Form library, if you execute the function without any parameters ( reset() ) the form is reset to its default values, if you pass an object to the function it will set the form with the values from the object (e.g. reset({ firstName: 'Bob' }) ).

How to set default value in select using ReactJS?

- GeeksforGeeks How to set default value in select using ReactJS ? In HTML, the ‘ selected’ attribute in the option tag is used to set the default value in the select menu. The same approach can also be used with React to set a default value if we are not using React Select Component.

What is the default value of fruit state in react select?

Since we set the initial value of the fruit state to 'apple', that’s what we display when we load the select element, which means that it’s the default value. Now we can have default values for our React select elements.

Why does the default value of onblur reset when the input is clear?

As below, the default value will be clear when onBlur because onBlur will default trigger handleInputChange event with empty value. I guess this might case by some default setting like 'onBlurResetsInput', but 'onBlurResetsInput' props has be removed at v2.0.0 . So want to know how to prevent onBlur reset, thanks.

Does react-select V2 now only accept objects as values?

If you've come here for react-select v2, and still having trouble - version 2 now only accepts an object as value, defaultValue, etc. That is, try using value= { {value: 'one', label: 'One'}}, instead of just value= {'one'}. I was having a similar error.


1 Answers

Have you tried defaultInputValue={this.state.inputValue}? You can read more about it here.

like image 118
Diego Castillo Avatar answered Sep 26 '22 01:09

Diego Castillo