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)}
/>
);
}
}
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.
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.
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.
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' }) ).
- 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.
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.
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.
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.
Have you tried defaultInputValue={this.state.inputValue}
? You can read more about it here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With