Skip to content

Add support for using EventListener values in a ReactSpec with access to ReactThis #60

Closed
@iand675

Description

@iand675

I am trying to implement a component that listens to the resize event. Essentially it's just a variation on the cookbook example here. Here's the code that I'm trying to write, although it sort of seems to not be possible. The basic issue is that I can create an EventListener, but I don't have a way to get access to the ReactThis inside of it. If I create a lambda that takes a this argument instead of a fully saturated value, then it creates a different event listener when it tries to remove the event listener on componentWillUnmount.

topBar :: ReactClass Unit
topBar = createClass barSpec
  where
    barSpec = (spec unit render)
      { displayName = "top-bar"
      , componentDidMount = \this -> do
          w <- window
          addEventListener resize handler false $ windowToEventTarget w
          return unit
      , componentWillUnmount = \this -> do
          w <- window
          removeEventListener resize (removeEventWriteAccess handler) false $ windowToEventTarget w
          return unit
      }

    handler = eventListener $ \this event -> do
      b <- getCurrentBreakpoint
      case b of
        Small -> writeState this True
        _ -> writeState this False
      print b

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions