So I'm using the Creatable component of the react-select library. I want to be able to add the created option into my options array on keyDown. How it looks right now
My states:
const [value, setValue] = useState([]);
const [inputValue, setInputValue] = useState('');
const [selectedValues, setselectedValues] = useState([]);
const [current, setCurrent] = useState([]);
const [selection, setSelection] = useState(statusOptions);
My functions
const handleInputChange = (value) => {
setInputValue(value);
};
const handleOnChange = (haha) => {
setCurrent(haha);
};
const handleKeyDown = () => {
setTimeout(() => {
setNewValues();
}, [2700]);
};
const setNewValues = (event) => {
const newOption = { label: inputValue, inputValue };
inputValue !== '' && setSelection([...selection, newOption]);
setCurrent(current);
let difference = current.filter((x) => !value.includes(x));
setInputValue('');
setselectedValues(difference);
};
How the component looks
<CreatableSelect
options={selection}
isMulti
onChange={handleOnChange}
onInputChange={handleInputChange}
inputValue={inputValue}
value={selectedValues.selected}
inputValue={inputValue}
onKeyDown={handleKeyDown}
/>
The problem: Right now, the inputValue is added to the dropdown list as the user types and i don't know why. I am able to add and delete the tags on keyDown. But when the user types slow or doesn't write quick enough, the dropdown list ends up with two created options.

