I know how functions are closure in Javascript and how they can access variables declared in their parent scope. Here I have defined a variable named socket in a functional component
function someName() {
    const ENDPOINT = "localhost:5000";
    let socket
    useEffect(() => {
          socket = io(ENDPOINT)
      // and some code
    }, [])
   const handleClick = () => {
         socket.emit('someEvent', someMessage)  // error: cannot read property emit of undefined
         }
   }
   return (
        <input />
        <button onClick={handleClick}
         )
when the component mounts, useEffect is called and defines the socket variable and when the function runs the variable is already defined. why am I getting that error on the handleClick function?