You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add support for using EventListener values in a ReactSpec with access to ReactThis #60
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::ReactClassUnit
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 ofSmall-> writeState this True
_ -> writeState this False
print b
Thanks for the report. I see two potential solutions here.
We could perhaps add wrappers for eventListener, addEventListener, and removeEventListener in order to provide the correct this value. However, I will have to dig into the details a bit more on this.
I believe it should be possible to achieve your use-case by storing the event handler (applied with this) in the state of your react component in the componentDidMount. Then in the componentWillUnmount, get the handler from the state and remove it. This is perhaps not ideal, but I think it would work.
Storing the handlers in the state works with one caveat– you generally want to be able to write to the state and props, so the tracked effects in the handler includes state :: ReactState (read :: Read, write :: Write). When you remove the event in componentWillUnmount, the effects from the handler are unified with the componentWillUnmount effects which only allows state to be read-only.
I had to write a coercion function to get types to unify since removing the handler doesn't actually trigger write effects:
Thanks for trying this out. Good point regarding the effects. The unsafeCoerce is not ideal, but perhaps this can suffice until I investigate option (1) a bit more. I think having support for adding/removing of event handlers would be a good addition.
@iand675 is this okay to close? I am thinking unsafeInterleaveEff might suffice here. I don't know if we need specific functionality in purescript-react, but I am open to look more into this.
I think the one other issue is that you have to put handlers in a Maybe since you can't provide set up a handler in some cases until you get to the componentDidMount callback. It's a bit awkward. Obviously there are workarounds to convince purescript-react to work, but I'm still of the opinion that having some wrappers would be the "correct" solution to this from a user-friendliness perspective.
Activity
ethul commentedon Feb 22, 2016
Thanks for the report. I see two potential solutions here.
eventListener,addEventListener, andremoveEventListenerin order to provide the correctthisvalue. However, I will have to dig into the details a bit more on this.this) in the state of your react component in thecomponentDidMount. Then in thecomponentWillUnmount, get the handler from the state and remove it. This is perhaps not ideal, but I think it would work.iand675 commentedon Feb 22, 2016
Storing the handlers in the state works with one caveat– you generally want to be able to write to the state and props, so the tracked effects in the handler includes
state :: ReactState (read :: Read, write :: Write). When you remove the event incomponentWillUnmount, the effects from the handler are unified with thecomponentWillUnmounteffects which only allows state to be read-only.I had to write a coercion function to get types to unify since removing the handler doesn't actually trigger write effects:
ethul commentedon Feb 23, 2016
Thanks for trying this out. Good point regarding the effects. The
unsafeCoerceis not ideal, but perhaps this can suffice until I investigate option (1) a bit more. I think having support for adding/removing of event handlers would be a good addition.PS - Would
unsafeInterleaveEffwork?iand675 commentedon Feb 23, 2016
Oh yeah, looks like
unsafeInterleaveEffis what I wanted. Thanks for the tip!ethul commentedon Feb 23, 2016
Welcome!
ethul commentedon Apr 9, 2016
@iand675 is this okay to close? I am thinking
unsafeInterleaveEffmight suffice here. I don't know if we need specific functionality inpurescript-react, but I am open to look more into this.iand675 commentedon Apr 9, 2016
I think the one other issue is that you have to put handlers in a
Maybesince you can't provide set up a handler in some cases until you get to thecomponentDidMountcallback. It's a bit awkward. Obviously there are workarounds to convince purescript-react to work, but I'm still of the opinion that having some wrappers would be the "correct" solution to this from a user-friendliness perspective.ethul commentedon Apr 9, 2016
Good point. I will keep this issue around. I might have some time soon to dig more into this. Thanks!
ethul commentedon Mar 4, 2018
As of #129, event handler references can be stored using purescript mechanism; e.g., #124 (comment)
Please reopen if this is still an issue.