score:1

Accepted answer

the second parameter that you pass to useeffect tells react to skip the effect unless one of the items in the array has changed. you passed in an empty array, so other than the initial render, it will never update. thus, you set up the event listeners with functions that have the original state in their closure, and nothing else.

to get it to update as state changes, either remove the array, or fil it with variables that your code depends on:

  useeffect(() => {
    document.addeventlistener('copy', handlecopy);
    document.addeventlistener('paste', handlepaste);
    return () => {
      document.removeeventlistener('copy', handlecopy);
      document.removeeventlistener('paste', handlepaste);
    }
  }, [state])

Related Query

More Query from same tag