mikesol / purescript-deku

A PureScript web UI framework
https://deku-documentation.vercel.app/
Apache License 2.0
135 stars 12 forks source link

How to run a function when component has mounted (and pass a setX function to it) ? #112

Closed jerbaroo closed 8 months ago

jerbaroo commented 8 months ago

You can see in the example below what I am trying to accomplish, in short:

foreign import initMap :: EffectFn1 (Location -> Effect Unit) Unit

myMap :: Nut
myMap = Deku.do
  setLocation /\ location <- useState initialLocation
  onDidMount (runEffectFn1 initMap setLocation) $
    D.div [ DA.id_ "map-row" ]
      [ D.div [ DA.id_ "map-container" ] []
      , location <#~> locationInfo
      ]
jerbaroo commented 8 months ago

onDidMount is in the Pursuit documentation, but does need seem to be in the create-deku-app-generated project.

jerbaroo commented 8 months ago

The answer was to use Self.

The Pursuit documentation and the README is out of date (as they both mention Deku.Lifecycle which is a solution in an old version of Deku).

myMap :: Nut
myMap = Deku.do
  setLocation /\ location <- useState initialLocation
  D.div
    [ DA.id_ "map-row"
    , Self.self_ \_ -> liftEffect $ runEffectFn2
      initLeaflet allLocations setLocation
    ]
    [ D.div [ DA.id_ "map-container" ] []
    , location <#~> locationInfo
    ]