Skip to content

Commit

Permalink
Update TimeReactHooks
Browse files Browse the repository at this point in the history
  • Loading branch information
andys8 committed Aug 10, 2022
1 parent 94fae1a commit 7325132
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions recipes/TimeReactHooks/spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
, "prelude"
, "react-basic-dom"
, "react-basic-hooks"
, "web-dom"
, "web-html"
]
, packages = ../../packages.dhall
Expand Down
26 changes: 14 additions & 12 deletions recipes/TimeReactHooks/src/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,30 @@ import Data.Newtype (class Newtype)
import Effect (Effect)
import Effect.Exception (throw)
import Effect.Timer (clearInterval, setInterval)
import React.Basic.DOM (render)
import React.Basic.DOM as R
import React.Basic.DOM.Client (createRoot, renderRoot)
import React.Basic.Hooks (Component, Hook, UseEffect, UseState, coerceHook, component, useEffectOnce, useState', (/\))
import React.Basic.Hooks as React
import Web.DOM.NonElementParentNode (getElementById)
import Web.HTML (window)
import Web.HTML.HTMLDocument (body)
import Web.HTML.HTMLElement (toElement)
import Web.HTML.HTMLDocument (toNonElementParentNode)
import Web.HTML.Window (document)

type Time = { hours :: Int, minutes :: Int, seconds :: Int }

main :: Effect Unit
main = do
body <- body =<< document =<< window
case body of
Nothing -> throw "Could not find body."
Just b -> do
time <- mkTime
render (time {}) (toElement b)
doc <- document =<< window
root <- getElementById "root" $ toNonElementParentNode doc
case root of
Nothing -> throw "Could not find root."
Just container -> do
reactRoot <- createRoot container
app <- mkApp
renderRoot reactRoot (app {})

mkTime :: Component {}
mkTime = do
mkApp :: Component {}
mkApp = do
now <- JSDate.now >>= getTime
component "Time" \_ -> React.do
{ hours, minutes, seconds } <- useCurrentTime now
Expand All @@ -48,7 +50,7 @@ mkTime = do

newtype UseCurrentTime hooks = UseCurrentTime (UseEffect Unit (UseState Time hooks))

derive instance ntUseCurrentTime :: Newtype (UseCurrentTime hooks) _
derive instance Newtype (UseCurrentTime hooks) _

useCurrentTime :: Time -> Hook UseCurrentTime Time
useCurrentTime initialTime =
Expand Down

0 comments on commit 7325132

Please sign in to comment.