I might be missing something (almost certain of it), but when I "set" a state array, if the array only has one item, the state, is empty. If I "set" an array with more than one item, the state array has one less item.
Probably best shown with a sample. (watch console).
import React, {useState} from "react";
import "./styles.css";
import Select from "react-select";
export default function App() {
const [items, setItems] = useState([]);
const options = [
{value: 1, label: "Item 1"},
{value: 2, label: "Item 2"},
{value: 3, label: "Item 3"},
]
function hanldeItemChange(selectedItems) {
//This prints all object in console
console.log(`selectedItems = ${selectedItems}`);
var mappedItems = selectedItems.map(item => item.value);
//This prints just the values as an array (correctly)
console.log(`mappedItems = ${mappedItems}`);
setItems(mappedItems);
//This print the values from my state array, but it does not match the "MappedItems"
console.log(`items = ${items}`);
}
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
<Select
className="basic-multi-select"
classNamePrefix="select"
onChange={hanldeItemChange}
isMulti
name="Items"
options={options}
/>
</div>
);
}