Getting undefined from fetched data on 1st onChange event from an input, don't know why.
My Hook
My Component
The onChange event passes the allUsersData to the handleSearchOnChange function, I set it to the state, and I console log the state to see if the data was set correctly. The 1st onChange event sets the state to an undefined, then afterwards it sets the correct data. I thought isLoading should prevent this from happening.
1 Reply
solid-orange•3y ago
Your issue is not with
react-query, but React itself. You should be aware that React state updates aren't immediate (https://blog.logrocket.com/why-react-doesnt-update-state-immediately/#:~:text=State%20updates%20in%20React%20are,components%20in%20a%20single%20pass.), so when you call setTempAllUsersData, tempAllUsersData isn't immediately set to allUsersData, but is still at its initial state: undefined. That's why your console.log is displaying undefined.Chiamaka Umeh
LogRocket Blog
Why React doesn't update state immediately - LogRocket Blog
When developing React applications, you may have noticed that state updates don’t immediately reflect new values after being changed.