I have 2 outside events, input and keydown for an input element, when input fired, it will call setInput in React component. When keydown event for arrowDown fired, ideally the input value should be the one set inside handleInput, but it's empty, do I miss something?
const SearchBox : React.FunctionComponent<ISearchProps> = React.memo((props: ISearchProps) => {
const [input, setInput] = React.useState({value: '', redirect: false});
useEffect(() => {
const container = document.querySelector('.xxx');
container.addEventListener('input', handleInput);
container.addEventListener('keydown', handleKeyboardEvent);
}, []);
const handleInput = (e) => {
setInput({value: e.currentTarget.value, redirect: false});
}
const handleKeyboardEvent = (e) => {
switch (e.key) {
case 'ArrowDown':
console.log(input);
setActiveSuggestionIndex(activeSuggestionIndex === suggestions.length - 1 ? 0 : activeSuggestionIndex + 1);
e.preventDefault();
break;
default:
break;
}
}
}