haskell - How can I branch on the value inside a Reflex Dynamic? -
in simplest case, have dynamic t bool
, , when value true, want single empty div exist, , when value false, don't want there dom element.
slightly more generally, if have dynamic t (either mya myb)
, , have functions know how render given dynamic t mya
or dynamic t myb
, how call appropriate function render?
if need switch widget need 1 of:
dyn :: monadwidget t m => dynamic t (m a) -> m (event t a) source
or
widgethold :: monadwidget t m => m -> event t (m a) -> m (dynamic t a)
since you've mentioned you've got dynamic @ hand, we're gonna use dyn
:
app = switched <- button "alternate!" flag <- folddyn ($) false (not <$ switched) -- have dynamic t bool w <- mapdyn mywidget flag -- in latest reflex can 'fmap' dyn w return () mywidget :: monadwidget t m => bool -> m () mywidget false = blank mywidget true = el "div" $ blank
the basic rule that, due higer-order nature of reflex, if want swap-out something, need have event/dynamic yields widget value. that's why dyn
takes dynamic t (m a)
parameter (and appropriately, widgethold
takes event t (m a)
. , that's why we've mapped on dynamic t bool
have dynamic has our widget building action value.
it's worth mentioning, neither dynamic/widgethold virtual dom/diffing speed rendering. reflex can more explicit updates (a dynamic/event t text can affect node text directly, without rerendering whole node) , should take advantage of that. if not large portions of page swapped , can yield significant performance hit.
Comments
Post a Comment