您需要在您的
ComponentDSL
(如果您有
e
输入var currently)使其成为可能,然后可以使用
H.fromEff
举起
randomInt
:
module Main where
import Prelude
import Control.Monad.Aff (Aff)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Random (randomInt, RANDOM)
import Halogen as H
import Halogen.HTML.Events.Indexed as HE
import Halogen.HTML.Indexed as HH
import Halogen.Util (runHalogenAff, awaitBody)
type State = { n::Int }
initialState :: State
initialState = { n: 3}
data Query a = NewRandom a
ui :: forall eff. H.Component { n :: Int } Query (Aff (random :: RANDOM | eff))
ui =
H.component { render, eval }
where
render :: State -> H.ComponentHTML Query
render state =
HH.button
[ HE.onClick $ HE.input_ NewRandom ]
[ HH.text $ show state.n ]
eval :: Query ~> H.ComponentDSL State Query (Aff (random :: RANDOM | eff))
eval (NewRandom next) = do
r <- H.fromEff $ randomInt 1 10
H.modify (\state -> state { n=r } )
pure next
main :: forall eff. Eff (H.HalogenEffects (random :: RANDOM | eff)) Unit
main =
runHalogenAff do
body <- awaitBody
H.runUI ui initialState body
(旁白:如果你正在做有效的事情,即使你只需要
Eff
,最容易使用
Aff
作为
组件DSL
monad,当您使用
runUI
它希望它是
Aff公司
-可以使用以下命令更改monad
interpret
Halogen.Component
模块,但由于您只使用
interpret liftAff
无论如何,你最好直接去
Aff公司
.)
看看
"Non-state effects" section of the guide
,或
AJAX example
有关跑步效果的详细信息,请参见
eval
.