In the below sandbox project, I was trying to use a custom hook to attach event listener.
In the useEventListener.js file, If I pass the savedHandler.current function directly to the event listener (line no: 21) I do not get the updated state value during state update. Only when I use the savedHandler.current function inside an anonymous function(line no: 19) I am getting the updated state value.
Could someone help me understand how this works ? Is this because of this reference?
https://codesandbox.io/s/gracious-cloud-kjw7dk?file=/src/useEventListener.js
Thanks