scrolladdeventlistenerreasonbucklescriptbs-webapi

How to add a listener for all scroll events?


I need to add a listener for all scroll events in Reason React or all scroll affects affecting the main window (either would work).

Trying this but the event does not trigger:

open Webapi.Dom;

Document.addEventListener("scroll", onScroll);

P.S. Also, there's a mention that addEventListener is a partial application, so I'm worried that this is potentially a 3-argument function expecting a target object rather than a window-wide function.


Solution

  • Your suspicion that this is a three-argument function is correct. It expects a reference to the document it should attach the event listener to. The document that is currently in scope is conveniently accessible through Webapi.Dom.document, so you should only need to add that:

    Document.addEventListener("scroll", onScroll, document);
    

    This convention is used everywhere in bs-webapi. Functions are not hard-coded to the document or window currently in scope because they aren't the only one you can use, even if they usually are the ones you want to use.