In the below code I have a simple react component which has a form containing a react-select component. It initially displays the defaultValue and then gives two options to select. Once selected the submit button would send a post request. Every thing works fine point the issue is that I want to revert the Select component to display the default value after on submit is clicked.
import Select from 'react-select';
export class Test extends Component {
this.setState = {
selection: 0,
};
onSelectChange = (e) => {
this.setState({ selection: e.value });
}
onSubmit = (e) => {
e.preventDefault();
const { selection } = this.state;
// Post request logic comes here
// Reset Select component to original default state
}
render() {
const defaultValue = { label: 'Choose something', value: 0 };
const options = [
{ label: 'Item 1', value: 1 },
{ label: 'Item 2', value: 2 },
];
return (
<form onSubmit={this.onSubmit}>
<Select
options={options}
defaultValue={defaultValue}
onChange={this.onSelectChange}
/>
//submit button here
</form>
);
}
}