I am trying to run an onChange
event on a select-react
component, but I cannot change the default values.
In addition to changing the options, I need to assign the selection values to a state
.
When I remove the state
, I am able to change the options normally.
I saved the code at codesandbox for verification.
const options = [
{
label: "Queue 1",
value: 0
},
{
label: "Queue 2",
value: 1
},
{
label: "Queue 3",
value: 2
}
];
const defaultOptions = [
{
label: "Queue 1",
value: 0
},
{
label: "Queue 3",
value: 2
}
];
const App = () => {
const [selectedQueue, setSelectedQueue] = useState([]);
const handleChange = async (e) => {
const value = e.map((x) => x.value);
console.log(value);
// if you comment the two lines, below I have no problems
setSelectedQueue(value);
console.log(selectedQueue);
};
const CustomSelect = (props) => (
<Select
options={options}
defaultValue={defaultOptions}
className="custom-select"
onChange={handleChange}
{...props}
/>
);
return (
<div className="App">
Multiselect:
<CustomSelect isMulti />
</div>
);
};
export default App;