79418109

Date: 2025-02-06 13:35:26
Score: 1
Natty:
Report link

This is how javascript closure works.

Your useEffect runs only once ([] dependency array). And the function inside addEventListener is a closure and captures the state when useEffect runs. Since offsets is initialized as null (or its initial state), this function always logs the initial state, eventhough when the component re-renders and offsets changes.Because of the ([] dependency array).

Reasons:
  • No code block (0.5):
  • Low reputation (0.5):
Posted by: Rajesh