Module
Elmish.Hooks.UseEffect
- Package
- purescript-elmish-hooks
- Repository
- collegevine/purescript-elmish-hooks
#useEffect Source
useEffect :: Aff Unit -> Hook (UseEffect Unit) Unit
The useEffect
hook takes an effect (Aff
) to run and runs it in the
init
of the resulting component. E.g.:
todos :: ReactElement
todos = Hooks.component Hooks.do
todos /\ setTodos <- useState []
useEffect do
todos <- API.fetchTodos
liftEffect $ setTodos todos
Hooks.pure $ H.fragment $ todoView <$> todos
#useEffect' Source
useEffect' :: forall @a. Eq a => a -> (a -> Aff Unit) -> Hook (UseEffect a) Unit
This is like useEffect
, but allows passing a value which, when it
changes, will trigger the effect to run again. E.g.:
view :: ReactElement
view = Hooks.component Hooks.do
count /\ setCount <- useState 0
useEffect' count \c -> liftEffect $
HTMLDocument.setTitle ("You clicked " <> show c <> " times") =<< document =<< window
Hooks.pure H.button_ "" { onClick: setCount $ count + 1 } "Click me"