I a setState function being passed down from a parent component, I want to setTheState of the parent setter if the enterKey is pressed. Though, when I set the state nothing happens and I'm still left with an empty array
Here's the code snippet
const { check, setCheck } = props // receive from props
const callApi = async (onEnter) => {
const res = await callFunction('', data)
if (res) {
setResults(res)
if (onEnter) {
setCheck(res)
console.log('check', check) // returns []
}
}
Returns [] when I log in parent as well
Instead of console logging it immediately, wait for it to change, because it's an async operation. You can wrap your log in a useEffect:
useEffect(() => {
console.log(check);
}, [check]);